def get_backup_auth_from_node(self, base_url, auth_token, hive_did): rt, status_code, err = self.post(base_url + '/api/v1/did/backup_auth', {"jwt": auth_token}) if err != None: return None, "Post backup_auth error: " + err token = rt["backup_token"] if token is None: return None, "Token is none." jws = lib.DefaultJWSParser_Parse(token.encode()) if not jws: return None, "Backup token DefaultJWSParser_Parse error: " + ffi.string( lib.DIDError_GetMessage()).decode() aud = ffi.string(lib.JWT_GetAudience(jws)).decode() if aud != self.get_did_string(): lib.JWT_Destroy(jws) return None, "Audience is error." issuer = ffi.string(lib.JWT_GetIssuer(jws)).decode() lib.JWT_Destroy(jws) if issuer is None: return None, "Issuer is none." if issuer != hive_did: return None, "Issuer is error." return token, None
def get_did_string_from_did(self, did): if not did: return None method = lib.DID_GetMethod(did) if not method: return None method = ffi.string(method).decode() sep_did = lib.DID_GetMethodSpecificId(did) if not sep_did: return None sep_did = ffi.string(sep_did).decode() return "did:" + method + ":" + sep_did
def get_auth_token_by_sign_in(self, base_url, vc_str, subject): vc = lib.Credential_FromJson(vc_str.encode(), ffi.NULL) if not vc: return None, None, "The credential string is error, unable to rebuild to a credential object." #sign_in doc = lib.DIDStore_LoadDID(self.store, self.did) doc_str = ffi.string(lib.DIDDocument_ToJson(doc, True)).decode() doc = json.loads(doc_str) rt, status_code, err = self.post(base_url + '/api/v1/did/sign_in', {"document": doc}) if err != None: return None, None, "Post sign_in error: " + err jwt = rt["challenge"] if jwt is None: return None, None, "Challenge is none." # print(jwt) jws = lib.DefaultJWSParser_Parse(jwt.encode()) if not jws: return None, None, "Challenge DefaultJWSParser_Parse error: " + ffi.string( lib.DIDError_GetMessage()).decode() aud = ffi.string(lib.JWT_GetAudience(jws)).decode() if aud != self.get_did_string(): lib.JWT_Destroy(jws) return None, None, "Audience is error." nonce = ffi.string(lib.JWT_GetClaim(jws, "nonce".encode())).decode() if nonce is None: lib.JWT_Destroy(jws) return None, None, "Nonce is none." hive_did = ffi.string(lib.JWT_GetIssuer(jws)).decode() lib.JWT_Destroy(jws) if hive_did is None: return None, None, "Issuer is none." #auth_token vp_json = self.create_presentation(vc, nonce, hive_did) if vp_json is None: return None, None, "create_presentation error." auth_token = self.create_vp_token(vp_json, subject, hive_did, hive_setting.AUTH_CHALLENGE_EXPIRED) if auth_token is None: return None, None, "create_vp_token error." return auth_token, hive_did, None
def export_current_mnemonic(store, storepass): if (not store) or (not storepass): return ffi.NULL mnemonic_str = ffi.new("char[" + str(lib.ELA_MAX_MNEMONIC_LEN + 1) + "]") lib.DIDStore_ExportMnemonic(store, storepass, mnemonic_str, lib.ELA_MAX_MNEMONIC_LEN + 1) return ffi.string(mnemonic_str).decode()
def create_presentation(self, vc, nonce, realm): vp = lib.Presentation_Create(self.did, ffi.NULL, self.store, self.storepass, nonce.encode(), realm.encode(), 1, vc) # print_err() vp_json = ffi.string(lib.Presentation_ToJson(vp, True)).decode() # print(vp_json) logging.debug(f"vp_json: {vp_json}") return vp_json
def MyDIDLocalResovleHandle(did): spec_did_str = ffi.string(lib.DID_GetMethodSpecificId(did)).decode() doc = ffi.NULL file_path = hive_setting.DID_DATA_LOCAL_DIDS + os.sep + spec_did_str is_exist = os.path.exists(file_path) if is_exist: f = open(file_path, "r") try: doc_str = f.read() doc = lib.DIDDocument_FromJson(doc_str.encode()) finally: f.close() return doc
def create_vp_token(self, vp_json, subject, hive_did, expire): doc = lib.DIDStore_LoadDID(self.store, self.did) builder = lib.DIDDocument_GetJwtBuilder(doc) ticks = int(datetime.now().timestamp()) iat = ticks nbf = ticks exp = ticks + expire lib.JWTBuilder_SetHeader(builder, "type".encode(), "JWT".encode()) lib.JWTBuilder_SetHeader(builder, "version".encode(), "1.0".encode()) lib.JWTBuilder_SetSubject(builder, subject.encode()) lib.JWTBuilder_SetAudience(builder, hive_did.encode()) lib.JWTBuilder_SetIssuedAt(builder, iat) lib.JWTBuilder_SetExpiration(builder, exp) lib.JWTBuilder_SetNotBefore(builder, nbf) lib.JWTBuilder_SetClaimWithJson(builder, "presentation".encode(), vp_json.encode()) lib.JWTBuilder_Sign(builder, ffi.NULL, self.storepass) token = ffi.string(lib.JWTBuilder_Compact(builder)).decode() lib.JWTBuilder_Destroy(builder) # print(token) return token
def get_error_message(self, prompt): err_message = ffi.string(lib.DIDError_GetMessage()).decode() if not prompt is None: err_message = prompt + " error: " + err_message return err_message
def get_error_message(): return str(ffi.string(lib.DIDError_GetMessage()), encoding='utf-8')