Skip to content

Commit

Permalink
Support AK/SK authing
Browse files Browse the repository at this point in the history
  • Loading branch information
yongyue.sl committed Mar 14, 2022
1 parent f482097 commit 7ca33e8
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions nacos/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import platform
import time
import hmac

try:
import ssl
Expand Down Expand Up @@ -40,7 +41,7 @@
logger = logging.getLogger(__name__)

DEBUG = False
VERSION = "0.1.6"
VERSION = "0.1.7"

DEFAULT_GROUP_NAME = "DEFAULT_GROUP"
DEFAULT_NAMESPACE = ""
Expand Down Expand Up @@ -771,7 +772,33 @@ def _process_polling_result(self):
watcher.last_md5 = md5

def _get_common_headers(self, params, data):
return {}
headers = {}
if self.auth_enabled:
ts = str(int(time.time() * 1000))
ak, sk = self.ak, self.sk

headers.update({
"Spas-AccessKey": ak,
"timeStamp": ts,
})
sign_str = ""
# in case tenant or group is null
if not params and not data:
return headers

tenant = (params and params.get("tenant")) or (data and data.get("tenant"))
group = (params and params.get("group")) or (data and data.get("group"))

if tenant:
sign_str = tenant + "+"
if group:
sign_str = sign_str + group + "+"

if sign_str:
sign_str += ts
headers["Spas-Signature"] = base64.encodebytes(
hmac.new(sk.encode(), sign_str.encode(), digestmod=hashlib.sha1).digest()).decode().strip()
return headers

def _build_metadata(self, metadata, params):
if metadata:
Expand Down

0 comments on commit 7ca33e8

Please sign in to comment.