diff --git a/src/iden3comm/handlers/auth.ts b/src/iden3comm/handlers/auth.ts index 52986342..dfdf5002 100644 --- a/src/iden3comm/handlers/auth.ts +++ b/src/iden3comm/handlers/auth.ts @@ -139,72 +139,64 @@ export class AuthHandler implements IAuthHandler { authRequest: AuthorizationRequestMessage; authResponse: AuthorizationResponseMessage; }> { - try { - const { - did, - request, - packer: { mediaType, ...packerParams } - } = options; - - const { unpackedMessage: message } = await this._packerMgr.unpack(request); - const authRequest = message as unknown as AuthorizationRequestMessage; - if (message.type !== PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE) { - throw new Error('Invalid media type'); - } - const authRequestBody = message.body as unknown as AuthorizationRequestMessageBody; - - const guid = uuid.v4(); - const authResponse: AuthorizationResponseMessage = { - id: guid, - typ: mediaType, - type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE, - thid: message.thid ?? guid, - body: { - did_doc: undefined, // slipped for now, todo: get did doc for id - message: authRequestBody.message, - scope: [] - }, - from: options.did.string(), - to: message.from - }; + const { + did, + request, + packer: { mediaType, ...packerParams } + } = options; - for (const proofReq of authRequestBody.scope) { - const zkpReq: ZeroKnowledgeProofRequest = { - id: proofReq.id, - circuitId: proofReq.circuitId as CircuitId, - query: proofReq.query - }; + const { unpackedMessage: message } = await this._packerMgr.unpack(request); + const authRequest = message as unknown as AuthorizationRequestMessage; + if (message.type !== PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE) { + throw new Error('Invalid media type'); + } + const authRequestBody = message.body as unknown as AuthorizationRequestMessageBody; - const creds = await this._credentialWallet.findByQuery(proofReq.query); + const guid = uuid.v4(); + const authResponse: AuthorizationResponseMessage = { + id: guid, + typ: mediaType, + type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE, + thid: message.thid ?? guid, + body: { + did_doc: undefined, // slipped for now, todo: get did doc for id + message: authRequestBody.message, + scope: [] + }, + from: options.did.string(), + to: message.from + }; - const credsForGenesisDID = await this._credentialWallet.filterByCredentialSubject( - creds, - did - ); - if (credsForGenesisDID.length == 0) { - throw new Error(`no credential were issued on the given id ${did.string()}`); - } + for (const proofReq of authRequestBody.scope) { + const zkpReq: ZeroKnowledgeProofRequest = { + id: proofReq.id, + circuitId: proofReq.circuitId as CircuitId, + query: proofReq.query + }; - const zkpRes: ZeroKnowledgeProofResponse = await this._proofService.generateProof( - zkpReq, - did, - credsForGenesisDID[0] - ); + const creds = await this._credentialWallet.findByQuery(proofReq.query); - authResponse.body.scope.push(zkpRes); + const credsForGenesisDID = await this._credentialWallet.filterByCredentialSubject(creds, did); + if (credsForGenesisDID.length == 0) { + throw new Error(`no credential were issued on the given id ${did.string()}`); } - const msgBytes = byteEncoder.encode(JSON.stringify(authResponse)); - const token = byteDecoder.decode( - await this._packerMgr.pack(mediaType, msgBytes, { - senderDID: did, - ...packerParams - }) + + const zkpRes: ZeroKnowledgeProofResponse = await this._proofService.generateProof( + zkpReq, + did, + credsForGenesisDID[0] ); - return { authRequest, authResponse, token }; - } catch (e) { - console.trace(e); - throw e; + + authResponse.body.scope.push(zkpRes); } + const msgBytes = byteEncoder.encode(JSON.stringify(authResponse)); + const token = byteDecoder.decode( + await this._packerMgr.pack(mediaType, msgBytes, { + senderDID: did, + ...packerParams + }) + ); + return { authRequest, authResponse, token }; } /**