def __init__(self, account_management_url, account_management_username, account_management_password, timeout): self.username = account_management_username self.password = account_management_password # possibly we don't need this here, does it matter? self.url = account_management_url self.timeout = timeout self.endpoint = { "token": "api/auth/sdk/", "surrogate": "api/account/{account_id}/service/{service_id}/surrogate/", "sign_slr": "api/account/{account_id}/servicelink/", "verify_slr": "api/account/{account_id}/servicelink/verify/", "sign_consent": "api/account/consent/sign/", "consent": "api/account/{account_id}/servicelink/{source_slr_id}/{sink_slr_id}/consent/", "auth_token": "api/consent/{sink_cr_id}/authorizationtoken/", "last_csr": "api/consent/{cr_id}/status/last/", "new_csr": "api/consent/{cr_id}/status/"} # Works as path to GET missing csr and POST new ones req = get(self.url + self.endpoint["token"], auth=(self.username, self.password), timeout=timeout) # check if the request for token succeeded debug_log.debug("{} {} {}".format(req.status_code, req.reason, req.text)) if req.ok: self.token = loads(req.text)["api_key"] else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting account management token failed.", "content": req.content}, title=req.reason)
def signAndstore(self, sink_cr, sink_csr, source_cr, source_csr, account_id): structure = {"sink": { "cr": sink_cr["cr"], "csr": sink_csr }, "source": { "cr": source_cr["cr"], "csr": source_csr } } template = { "data": { "source": { "consentRecordPayload": { "type": "ConsentRecord", "attributes": source_cr["cr"] }, "consentStatusRecordPayload": { "type": "ConsentStatusRecord", "attributes": source_csr, } }, "sink": { "consentRecordPayload": { "type": "ConsentRecord", "attributes": sink_cr["cr"], }, "consentStatusRecordPayload": { "type": "ConsentStatusRecord", "attributes": sink_csr, }, }, }, } slr_id_sink = template["data"]["sink"]["consentRecordPayload"]["attributes"]["common_part"]["slr_id"] slr_id_source = template["data"]["source"]["consentRecordPayload"]["attributes"]["common_part"]["slr_id"] # print(type(slr_id_source), type(slr_id_sink), account_id) debug_log.debug(dumps(template, indent=2)) req = post(self.url + self.endpoint["consent"].replace("{account_id}", account_id) .replace("{source_slr_id}", slr_id_source). replace("{sink_slr_id}", slr_id_sink), json=template, headers={'Api-Key': self.token}, timeout=self.timeout) debug_log.debug("{} {} {} {}".format(req.status_code, req.reason, req.text, req.content)) if req.ok: debug_log.debug(dumps(loads(req.text), indent=2)) else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting surrogate_id from account management failed.", "content": loads(req.text)}, title=req.reason) return loads(req.text)
def get_AuthTokenInfo(self, cr_id): req = get(self.url + self.endpoint["auth_token"] .replace("{sink_cr_id}", cr_id), headers={'Api-Key': self.token}, timeout=self.timeout) if req.ok: templ = loads(req.text) else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting AuthToken info from account management failed.","content": req.content}, title=req.reason) return templ
def getSUR_ID(self, service_id, account_id): debug_log.debug(""+self.url+self.endpoint["surrogate"].replace("{account_id}",account_id).replace("{service_id}", service_id)) req = get(self.url+self.endpoint["surrogate"].replace("{account_id}",account_id).replace("{service_id}", service_id), headers={'Api-Key': self.token}, timeout=self.timeout) if req.ok: templ = loads(req.text) else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting surrogate_id from account management failed.", "content": req.content}, title=req.reason) return templ
def sign_slr(self, template, account_id): templu =template req = post(self.url+self.endpoint["sign_slr"].replace("{account_id}", account_id), json=templu, headers={'Api-Key': self.token}, timeout=self.timeout) debug_log.debug("API token: {}".format(self.token)) debug_log.debug("{} {} {} {}".format(req.status_code, req.reason, req.text, req.content)) if req.ok: templ = loads(req.text) else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting surrogate_id from account management failed.","content": loads(req.text)}, title=req.reason) debug_log.debug(templ) return templ
def get_last_csr(self, cr_id): endpoint_url = self.url + self.endpoint["last_csr"].replace("{cr_id}", cr_id) debug_log.debug("" + endpoint_url) req = get(endpoint_url, headers={'Api-Key': self.token}, timeout=self.timeout) if req.ok: templ = loads(req.text) tool = SLR_tool() payload = tool.decrypt_payload(templ["data"]["attributes"]["csr"]["payload"]) debug_log.info("Got CSR payload from account:\n{}".format(dumps(payload, indent=2))) csr_id = payload["record_id"] return {"csr_id": csr_id} else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Getting last csr from account management failed.", "content": req.content}, title=req.reason)
def get_missing_csr(self, cr_id, csr_id): endpoint_url = self.url + self.endpoint["new_csr"].replace("{cr_id}", cr_id) debug_log.debug("" + endpoint_url) payload = {"csr_id": csr_id} req = get(endpoint_url, params=payload, headers={'Api-Key': self.token}, timeout=self.timeout) if req.ok: templ = loads(req.text) #tool = SLR_tool() #payload = tool.decrypt_payload(templ["data"]["attributes"]["csr"]["payload"]) debug_log.info("Fetched missing CSR:\n{}".format(dumps(templ, indent=2))) #csr_id = payload["record_id"] return {"missing_csr": templ} else: raise DetailedHTTPException(status=req.status_code, detail={"msg": "Creating new csr at account management failed.", "content": req.content}, title=req.reason)