Skip to content

Commit

Permalink
update js-sdk to 1.26.0 and verify response typ (#101)
Browse files Browse the repository at this point in the history
* update js-sdk to 1.26.0 and verify response typ

* parse accept and compare on env field

* sync messages

* add unit test
  • Loading branch information
volodymyr-basiuk authored Jan 17, 2025
1 parent a197115 commit e5a891e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 26 deletions.
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"url": "https://github.com/iden3/js-iden3-auth"
},
"dependencies": {
"@0xpolygonid/js-sdk": "1.23.0",
"@0xpolygonid/js-sdk": "1.26.0",
"@iden3/js-crypto": "1.1.0",
"@iden3/js-iden3-core": "1.4.1",
"@iden3/js-jsonld-merklization": "1.4.1",
Expand Down
23 changes: 12 additions & 11 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function createAuthorizationRequestWithMessage(
typ: PROTOCOL_CONSTANTS.MediaType.PlainMessage,
type: PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE,
body: {
accept: opts?.accept,
reason: reason,
message: message,
callbackUrl: callbackUrl,
Expand Down Expand Up @@ -419,6 +420,16 @@ export class Verifier {
opts?: VerifyOpts
): Promise<AuthorizationResponseMessage> {
const msg = await this.packageManager.unpack(byteEncoder.encode(tokenStr));

if (request.body.accept?.length) {
const acceptedMediaTypes = request.body.accept.map(
(accept) => parseAcceptProfile(accept).env
);
if (!acceptedMediaTypes.includes(msg.unpackedMediaType)) {
throw new Error('response type is not in accept profiles of the request');
}
}

const response = msg.unpackedMessage as AuthorizationResponseMessage;
await this.verifyAuthResponse(response, request, opts);
return response;
Expand All @@ -438,17 +449,7 @@ export class Verifier {
}
const supportedMediaTypes: PROTOCOL_CONSTANTS.MediaType[] = [];
for (const acceptProfile of profile) {
// 1. check protocol version
const { protocolVersion, env } = parseAcceptProfile(acceptProfile);
const messageTypeVersion = Number(messageType.split('/').at(-2));
if (
protocolVersion !== PROTOCOL_CONSTANTS.ProtocolVersion.V1 ||
(protocolVersion === PROTOCOL_CONSTANTS.ProtocolVersion.V1 &&
(messageTypeVersion < 1 || messageTypeVersion >= 2))
) {
continue;
}
// 2. check packer support
const { env } = parseAcceptProfile(acceptProfile);
if (this.packageManager.isProfileSupported(env, acceptProfile)) {
supportedMediaTypes.push(env);
}
Expand Down
86 changes: 76 additions & 10 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NativeProver,
PROTOCOL_CONSTANTS,
PackageManager,
PlainPacker,
ZeroKnowledgeProofRequest,
buildAccept
} from '@0xpolygonid/js-sdk';
Expand All @@ -25,13 +26,31 @@ import { getDateFromUnixTimestamp, getUnixTimestamp } from '@iden3/js-iden3-core

describe('auth tests', () => {
const connectionString = process.env.IPFS_URL ?? 'https://ipfs.io';
const acceptProfile = buildAccept([
const acceptProfileZKP = buildAccept([
{
protocolVersion: PROTOCOL_CONSTANTS.ProtocolVersion.V1,
env: PROTOCOL_CONSTANTS.MediaType.ZKPMessage,
circuits: [PROTOCOL_CONSTANTS.AcceptAuthCircuits.AuthV2]
}
]);

const acceptProfileJWS = buildAccept([
{
protocolVersion: PROTOCOL_CONSTANTS.ProtocolVersion.V1,
env: PROTOCOL_CONSTANTS.MediaType.SignedMessage,
alg: [
PROTOCOL_CONSTANTS.AcceptJwsAlgorithms.ES256K,
PROTOCOL_CONSTANTS.AcceptJwsAlgorithms.ES256K
]
}
]);

const acceptProfilePlain = buildAccept([
{
protocolVersion: PROTOCOL_CONSTANTS.ProtocolVersion.V1,
env: PROTOCOL_CONSTANTS.MediaType.PlainMessage
}
]);
const expiresTime = getDateFromUnixTimestamp(getUnixTimestamp(new Date()) + 5 * 60);
it('createAuthorizationRequest', () => {
const sender = 'did:iden3:polygon:amoy:xCRp75DgAdS63W65fmXHz6p9DwdonuRU9e46DifhX';
Expand Down Expand Up @@ -76,7 +95,7 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
accept: acceptProfilePlain,
expires_time: expiresTime
}
);
Expand All @@ -94,12 +113,15 @@ describe('auth tests', () => {
}
};

const packageManager = new PackageManager();
packageManager.registerPackers([new PlainPacker()]);

const verifier = await Verifier.newVerifier({
stateResolver: resolvers,
suite: {
prover: new NativeProver(new FSCircuitStorage({ dirname: '' })),
circuitStorage: new FSCircuitStorage({ dirname: '../' }),
packageManager: new PackageManager(),
packageManager: packageManager,
documentLoader: schemaLoader
}
});
Expand All @@ -118,7 +140,6 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
expires_time: expiresTime
}
);
Expand Down Expand Up @@ -150,7 +171,7 @@ describe('auth tests', () => {

const response: AuthorizationResponseMessage = {
id: 'd61ca0e1-0fb4-42e1-9baf-10926d76588a',
typ: PROTOCOL_CONSTANTS.MediaType.ZKPMessage,
typ: PROTOCOL_CONSTANTS.MediaType.PlainMessage,
type: PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE,
thid: '3bfc628a-6d16-4af7-8358-59656ca30600',
body: {
Expand Down Expand Up @@ -287,7 +308,6 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
expires_time: expiresTime
}
);
Expand Down Expand Up @@ -469,7 +489,7 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
accept: acceptProfileZKP,
expires_time: expiresTime
}
);
Expand Down Expand Up @@ -521,7 +541,7 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
accept: acceptProfileJWS,
expires_time: expiresTime
}
);
Expand Down Expand Up @@ -563,6 +583,52 @@ describe('auth tests', () => {
await expect(verifier.fullVerify(token, request, testOpts)).resolves.not.toThrow();
});

it('TestFullVerify invalid accept', async () => {
const token =
'eyJhbGciOiJFUzI1NkstUiI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DUyNTkwNTExOTE3MzY3NDk4NTg0MzkxNTQ0NTYzNDcyNjE2NzQ1MDk4OTYzMCIsIjIiLCIyIiwiMjAwMDAxMDEiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiLCIwIiwiMCIsIjAiXX1dfSwiZnJvbSI6ImRpZDpwa2g6cG9seToweDcxNDFFNGQyMEY3NjQ0REM4YzBBZENBOGE1MjBFQzgzQzZjQUJENjUiLCJ0byI6ImRpZDpwb2x5Z29uaWQ6cG9seWdvbjptdW1iYWk6MnFMUHF2YXlOUXo5VEEycjVWUHhVdWdvRjE4dGVHVTU4M3pKODU5d2Z5In0.bWc2ECABj7nvHatD8AXWNJM2VtfhkIjNwz5BBIK9zBMsP0-UWLEWdAWcosiLkYoL0KWwZpgEOrPPepl6T5gC-AA';
const sender = 'did:polygonid:polygon:mumbai:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy';
const callback = 'https://test.com/callback';
const reason = 'reason';
const request: AuthorizationRequestMessage = createAuthorizationRequest(
reason,
sender,
callback,
{
accept: acceptProfileZKP,
expires_time: expiresTime
}
);

const proofRequest: ZeroKnowledgeProofRequest = {
id: 1,
circuitId: CircuitId.AtomicQuerySigV2,
query: {
allowedIssuers: ['*'],
context:
'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v4.jsonld',
type: 'KYCAgeCredential',
credentialSubject: {
birthday: {
$lt: 20000101
}
}
}
};
request.body.scope.push(proofRequest);

const verifier = await Verifier.newVerifier({
stateResolver: resolvers,
documentLoader: schemaLoader,
circuitsDir: path.join(__dirname, './testdata'),
didDocumentResolver: resolveDIDDocument
});
verifier.setupJWSPacker(new KMS(), resolveDIDDocument);

await expect(verifier.fullVerify(token, request, testOpts)).rejects.toThrow(
'response type is not in accept profiles of the request'
);
});

it('registry: get existing circuit', () => {
const type = Circuits.getCircuitPubSignals('authV2');
const instance = new type([
Expand Down Expand Up @@ -594,7 +660,7 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
accept: acceptProfileZKP,
expires_time: expiresTime
}
);
Expand Down Expand Up @@ -644,7 +710,7 @@ describe('auth tests', () => {
sender,
callback,
{
accept: acceptProfile,
accept: acceptProfileZKP,
expires_time: expiresTime
}
);
Expand Down

0 comments on commit e5a891e

Please sign in to comment.