Skip to content

Commit

Permalink
add opts to createAuthorizationRequest (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk authored Nov 22, 2024
1 parent faf713f commit d55d5e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
22 changes: 18 additions & 4 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,46 @@ import { CircuitId } from '../../circuits';
import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
import { parseAcceptProfile } from '../utils';

/**
* Options to pass to createAuthorizationRequest function
* @public
*/
export type AuthorizationRequestCreateOptions = {
accept?: string[];
scope?: ZeroKnowledgeProofRequest[];
};

/**
* createAuthorizationRequest is a function to create protocol authorization request
* @param {string} reason - reason to request proof
* @param {string} sender - sender did
* @param {string} callbackUrl - callback that user should use to send response
* @param {AuthorizationRequestCreateOptions} opts - authorization request options
* @returns `Promise<AuthorizationRequestMessage>`
*/
export function createAuthorizationRequest(
reason: string,
sender: string,
callbackUrl: string
callbackUrl: string,
opts?: AuthorizationRequestCreateOptions
): AuthorizationRequestMessage {
return createAuthorizationRequestWithMessage(reason, '', sender, callbackUrl);
return createAuthorizationRequestWithMessage(reason, '', sender, callbackUrl, opts);
}
/**
* createAuthorizationRequestWithMessage is a function to create protocol authorization request with explicit message to sign
* @param {string} reason - reason to request proof
* @param {string} message - message to sign in the response
* @param {string} sender - sender did
* @param {string} callbackUrl - callback that user should use to send response
* @param {AuthorizationRequestCreateOptions} opts - authorization request options
* @returns `Promise<AuthorizationRequestMessage>`
*/
export function createAuthorizationRequestWithMessage(
reason: string,
message: string,
sender: string,
callbackUrl: string
callbackUrl: string,
opts?: AuthorizationRequestCreateOptions
): AuthorizationRequestMessage {
const uuidv4 = uuid.v4();
const request: AuthorizationRequestMessage = {
Expand All @@ -59,10 +72,11 @@ export function createAuthorizationRequestWithMessage(
typ: MediaType.PlainMessage,
type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE,
body: {
accept: opts?.accept,
reason: reason,
message: message,
callbackUrl: callbackUrl,
scope: []
scope: opts?.scope ?? []
}
};
return request;
Expand Down
58 changes: 24 additions & 34 deletions tests/handlers/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
NativeProver,
VerifiableConstants,
buildAccept,
AcceptProfile
AcceptProfile,
createAuthorizationRequest
} from '../../src';
import { ProvingMethodAlg, Token } from '@iden3/js-jwz';
import { Blockchain, DID, DidMethod, NetworkId } from '@iden3/js-iden3-core';
Expand Down Expand Up @@ -172,23 +173,15 @@ describe('auth', () => {
circuits: [AcceptAuthCircuits.AuthV2]
};

const authReqBody: AuthorizationRequestMessageBody = {
callbackUrl: 'http://localhost:8080/callback?id=1234442-123123-123123',
reason: 'reason',
message: 'message',
accept: buildAccept([profile]),
scope: [proofReq]
};

const id = uuid.v4();
const authReq: AuthorizationRequestMessage = {
id,
typ: PROTOCOL_CONSTANTS.MediaType.PlainMessage,
type: PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE,
thid: id,
body: authReqBody,
from: issuerDID.string()
};
const authReq = createAuthorizationRequest(
'reason',
issuerDID.string(),
'http://localhost:8080/callback?id=1234442-123123-123123',
{
scope: [proofReq],
accept: buildAccept([profile])
}
);

const msgBytes = byteEncoder.encode(JSON.stringify(authReq));
const authRes = await authHandler.handleAuthorizationRequest(userDID, msgBytes);
Expand Down Expand Up @@ -836,19 +829,6 @@ describe('auth', () => {
const userId = 'did:polygonid:polygon:mumbai:2qPDLXDaU1xa1ERTb1XKBfPCB3o2wA46q49neiXWwY';
const reason = 'test';
const message = 'message to sign';
const request: AuthorizationRequestMessage = createAuthorizationRequestWithMessage(
reason,
message,
sender,
callback
);
expect(request.body.scope.length).to.be.eq(0);
expect(request.body.callbackUrl).to.be.eq(callback);
expect(request.body.reason).to.be.eq(reason);
expect(request.from).to.be.eq(sender);

request.thid = '7f38a193-0918-4a48-9fac-36adfdb8b542';

const proofRequest: ZeroKnowledgeProofRequest = {
id: 23,
circuitId: CircuitId.AtomicQueryMTPV2,
Expand All @@ -864,10 +844,20 @@ describe('auth', () => {
}
}
};
request.body.scope.push(proofRequest);

const request: AuthorizationRequestMessage = createAuthorizationRequestWithMessage(
reason,
message,
sender,
callback,
{
scope: [proofRequest]
}
);
expect(request.body.scope.length).to.be.eq(1);

expect(request.body.callbackUrl).to.be.eq(callback);
expect(request.body.reason).to.be.eq(reason);
expect(request.from).to.be.eq(sender);
request.thid = '7f38a193-0918-4a48-9fac-36adfdb8b542';
const mtpProof: ZeroKnowledgeProofResponse = {
id: proofRequest.id,
circuitId: 'credentialAtomicQueryMTPV2',
Expand Down

0 comments on commit d55d5e7

Please sign in to comment.