def _get_response(self, methed, host, uri, header, query=None, body=None): sig = signer.Signer() # Set the AK/SK to sign and authenticate the request. sig.Key = self.__access sig.Secret = self.__secret # The following example shows how to set the request URL and parameters to query a VPC list. r = signer.HttpRequest() r.scheme = "https" # Set request Endpoint. r.host = host # Specify a request method, such as GET, PUT, POST, DELETE, HEAD, and PATCH. r.method = methed # Set request URI. r.uri = uri # Set parameters for the request URL. r.query = query # Add header parameters, for example, x-domain-id for invoking a global service and x-project-id for invoking a project-level service. r.headers = header if body is not None: r.body = body sig.Sign(r) if self.__securitytoken is not None: r.headers['X-Security-Token'] = self.__securitytoken httpclient = HttpClient(host=r.host, url=r.uri, method=r.method, headers=r.headers, body=r.body, timeout=self._connect_timeout) return httpclient.get_https_response()
def __init__(self,ak,sk,httpendpoint): if ak == "" or sk == "" or httpendpoint == "": raise ValueError('The parameter for the HWOcrClientAKSK constructor cannot be empty') self.endpoint=httpendpoint self.sig=signer.Signer() self.sig.AppKey=ak self.sig.AppSecret=sk self.httpschema="https" #currently only support https self.httpmethod="POST" #currently only support post
def __init__(self, ak, sk, region): if ak == "" or sk == "" or region == "": raise ValueError("The parameter for the HWOcrClientAKSK constructor cannot be empty.") self.endpoint = "ocr." + region + ".myhuaweicloud.com" self.sig = signer.Signer() self.sig.AppKey = ak self.sig.AppSecret = sk self.httpschema = "https" # Only HTTPS is supported. self.httpmethod = "POST" # Only POST is supported.
def wrapped(*args, **kwargs): if "authorization" not in request.headers: return 'Authorization not found.', 401 authorization = request.headers['authorization'] m = authorizationPattern.match(authorization) if m is None: return 'Authorization format incorrect.', 401 signingKey = m.group(1) if signingKey not in secrets: return 'Signing key not found.', 401 signingSecret = secrets[signingKey] signedHeaders = m.group(2).split(";") r = signer.HttpRequest() r.method = request.method r.uri = request.path r.query = {} for k in request.query_string.decode('utf-8').split('&'): spl = k.split("=", 1) if spl[0] != "": if len(spl) < 2: r.query[spl[0]] = "" else: r.query[spl[0]] = spl[1] r.headers = {} needbody = True dateHeader = None for k in signedHeaders: if k not in request.headers: return 'Signed header ' + k + ' not found', 401 v = request.headers[k] if k.lower( ) == 'x-sdk-content-sha256' and v == 'UNSIGNED-PAYLOAD': needbody = False if k.lower() == 'x-sdk-date': dateHeader = v r.headers[k] = v if needbody: r.body = request.get_data() if dateHeader is None: return 'Header x-sdk-date not found.', 401 t = datetime.strptime(dateHeader, BasicDateFormat) if abs(t - datetime.utcnow()) > timedelta(minutes=15): return 'Signature expired.', 401 sig = signer.Signer() sig.Key = signingKey sig.Secret = signingSecret if not sig.Verify(r, m.group(3)): return 'Verify authroization failed.', 401 return f(*args, **kwargs)
def httpSign(): sig = signer.Signer() sig.Key = '0CJLRMLEUJXDGMCQPOYK' sig.Secret = 'hfpIkt4ZZFqYlpqnV7H1BiEFSNDsSUel1CFsc8GQ' url = "https://cbs-ext.cn-north-4.myhuaweicloud.com/v1/06e34d38048026b52f94c01d8ff43e1c/qabots/4c0a16c6-5176-4938-bfb5-1d5546ece167/chat" headers = { 'Content-Type': 'application/json', 'X-Sdk-Content-Sha256' : 'UNSIGNED-PAYLOAD' } r = signer.HttpRequest("POST", url=url, headers = headers) sig.Sign(r) return r
def _create_sig(self) -> signer.Signer: sig = signer.Signer() sig.Key = self._key sig.Secret = self._secret return sig
# coding=utf-8 import requests from apig_sdk import signer if __name__ == '__main__': sig = signer.Signer() sig.Key = "bbeed2b5-8b7f-44d2-8ae1-3d7a78b5b249" sig.Secret = "52cf34a05b4c3cfd467fc60c6a9571bd3a3b19d5d12d93b3860f72b582551567" r = signer.HttpRequest( "GET", #"https://29c8bf7004354c28a8ece1e9db45d710.apic.cn-east-2.huaweicloudapis.com/test", "https://3d98f804db0e4fb4bb32677f973c3d44.apic.cn-east-2.huaweicloudapis.com/testA", {"x-stage": "RELEASE"}, "body") sig.Sign(r) print(r.headers["X-Sdk-Date"]) print(r.headers["Authorization"]) resp = requests.request(r.method, r.scheme + "://" + r.host + r.uri, headers=r.headers, data=r.body) print(resp.status_code, resp.reason) print(resp.content)