Skip to content

Commit d55d5e7

Browse files
add opts to createAuthorizationRequest (#289)
1 parent faf713f commit d55d5e7

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

src/iden3comm/handlers/auth.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,46 @@ import { CircuitId } from '../../circuits';
2323
import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
2424
import { parseAcceptProfile } from '../utils';
2525

26+
/**
27+
* Options to pass to createAuthorizationRequest function
28+
* @public
29+
*/
30+
export type AuthorizationRequestCreateOptions = {
31+
accept?: string[];
32+
scope?: ZeroKnowledgeProofRequest[];
33+
};
34+
2635
/**
2736
* createAuthorizationRequest is a function to create protocol authorization request
2837
* @param {string} reason - reason to request proof
2938
* @param {string} sender - sender did
3039
* @param {string} callbackUrl - callback that user should use to send response
40+
* @param {AuthorizationRequestCreateOptions} opts - authorization request options
3141
* @returns `Promise<AuthorizationRequestMessage>`
3242
*/
3343
export function createAuthorizationRequest(
3444
reason: string,
3545
sender: string,
36-
callbackUrl: string
46+
callbackUrl: string,
47+
opts?: AuthorizationRequestCreateOptions
3748
): AuthorizationRequestMessage {
38-
return createAuthorizationRequestWithMessage(reason, '', sender, callbackUrl);
49+
return createAuthorizationRequestWithMessage(reason, '', sender, callbackUrl, opts);
3950
}
4051
/**
4152
* createAuthorizationRequestWithMessage is a function to create protocol authorization request with explicit message to sign
4253
* @param {string} reason - reason to request proof
4354
* @param {string} message - message to sign in the response
4455
* @param {string} sender - sender did
4556
* @param {string} callbackUrl - callback that user should use to send response
57+
* @param {AuthorizationRequestCreateOptions} opts - authorization request options
4658
* @returns `Promise<AuthorizationRequestMessage>`
4759
*/
4860
export function createAuthorizationRequestWithMessage(
4961
reason: string,
5062
message: string,
5163
sender: string,
52-
callbackUrl: string
64+
callbackUrl: string,
65+
opts?: AuthorizationRequestCreateOptions
5366
): AuthorizationRequestMessage {
5467
const uuidv4 = uuid.v4();
5568
const request: AuthorizationRequestMessage = {
@@ -59,10 +72,11 @@ export function createAuthorizationRequestWithMessage(
5972
typ: MediaType.PlainMessage,
6073
type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE,
6174
body: {
75+
accept: opts?.accept,
6276
reason: reason,
6377
message: message,
6478
callbackUrl: callbackUrl,
65-
scope: []
79+
scope: opts?.scope ?? []
6680
}
6781
};
6882
return request;

tests/handlers/auth.test.ts

+24-34
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import {
4343
NativeProver,
4444
VerifiableConstants,
4545
buildAccept,
46-
AcceptProfile
46+
AcceptProfile,
47+
createAuthorizationRequest
4748
} from '../../src';
4849
import { ProvingMethodAlg, Token } from '@iden3/js-jwz';
4950
import { Blockchain, DID, DidMethod, NetworkId } from '@iden3/js-iden3-core';
@@ -172,23 +173,15 @@ describe('auth', () => {
172173
circuits: [AcceptAuthCircuits.AuthV2]
173174
};
174175

175-
const authReqBody: AuthorizationRequestMessageBody = {
176-
callbackUrl: 'http://localhost:8080/callback?id=1234442-123123-123123',
177-
reason: 'reason',
178-
message: 'message',
179-
accept: buildAccept([profile]),
180-
scope: [proofReq]
181-
};
182-
183-
const id = uuid.v4();
184-
const authReq: AuthorizationRequestMessage = {
185-
id,
186-
typ: PROTOCOL_CONSTANTS.MediaType.PlainMessage,
187-
type: PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE,
188-
thid: id,
189-
body: authReqBody,
190-
from: issuerDID.string()
191-
};
176+
const authReq = createAuthorizationRequest(
177+
'reason',
178+
issuerDID.string(),
179+
'http://localhost:8080/callback?id=1234442-123123-123123',
180+
{
181+
scope: [proofReq],
182+
accept: buildAccept([profile])
183+
}
184+
);
192185

193186
const msgBytes = byteEncoder.encode(JSON.stringify(authReq));
194187
const authRes = await authHandler.handleAuthorizationRequest(userDID, msgBytes);
@@ -836,19 +829,6 @@ describe('auth', () => {
836829
const userId = 'did:polygonid:polygon:mumbai:2qPDLXDaU1xa1ERTb1XKBfPCB3o2wA46q49neiXWwY';
837830
const reason = 'test';
838831
const message = 'message to sign';
839-
const request: AuthorizationRequestMessage = createAuthorizationRequestWithMessage(
840-
reason,
841-
message,
842-
sender,
843-
callback
844-
);
845-
expect(request.body.scope.length).to.be.eq(0);
846-
expect(request.body.callbackUrl).to.be.eq(callback);
847-
expect(request.body.reason).to.be.eq(reason);
848-
expect(request.from).to.be.eq(sender);
849-
850-
request.thid = '7f38a193-0918-4a48-9fac-36adfdb8b542';
851-
852832
const proofRequest: ZeroKnowledgeProofRequest = {
853833
id: 23,
854834
circuitId: CircuitId.AtomicQueryMTPV2,
@@ -864,10 +844,20 @@ describe('auth', () => {
864844
}
865845
}
866846
};
867-
request.body.scope.push(proofRequest);
868-
847+
const request: AuthorizationRequestMessage = createAuthorizationRequestWithMessage(
848+
reason,
849+
message,
850+
sender,
851+
callback,
852+
{
853+
scope: [proofRequest]
854+
}
855+
);
869856
expect(request.body.scope.length).to.be.eq(1);
870-
857+
expect(request.body.callbackUrl).to.be.eq(callback);
858+
expect(request.body.reason).to.be.eq(reason);
859+
expect(request.from).to.be.eq(sender);
860+
request.thid = '7f38a193-0918-4a48-9fac-36adfdb8b542';
871861
const mtpProof: ZeroKnowledgeProofResponse = {
872862
id: proofRequest.id,
873863
circuitId: 'credentialAtomicQueryMTPV2',

0 commit comments

Comments
 (0)