Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept header #253

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8635d5a
accept-profile auth/jwz modes
volodymyr-basiuk Aug 8, 2024
d9b5ee9
final accept profile format
volodymyr-basiuk Aug 9, 2024
60b1dbd
fix no accept header
volodymyr-basiuk Aug 9, 2024
bf19cb8
add accept in req unit tests
volodymyr-basiuk Aug 12, 2024
e0882d4
getEnvelop and isSupported on Packer
volodymyr-basiuk Aug 20, 2024
ec1dc46
fix protocol version check
volodymyr-basiuk Aug 20, 2024
939ffb2
allow no alg specified
volodymyr-basiuk Aug 20, 2024
23673b8
algSupported refactor
volodymyr-basiuk Aug 20, 2024
8d673ff
Merge branch 'main' into feat/accept-header
volodymyr-basiuk Aug 25, 2024
3dbf2ca
bump patch version
volodymyr-basiuk Aug 25, 2024
1f6d6cd
getEnvelope and isProfileSupported
volodymyr-basiuk Aug 29, 2024
babd7d3
Merge branch 'main' into feat/accept-header
volodymyr-basiuk Aug 29, 2024
245260e
feat: add support of submitZKPResponseCrossChain (#254)
volodymyr-basiuk Sep 4, 2024
b982bc6
bump version
volodymyr-basiuk Sep 4, 2024
7d8667b
resolve conflict
volodymyr-basiuk Sep 4, 2024
e775a41
resolve comments
volodymyr-basiuk Sep 19, 2024
fb82622
format
volodymyr-basiuk Sep 19, 2024
0bed0ba
fix submit typo in supported enum (#262)
volodymyr-basiuk Sep 5, 2024
b6d7084
feat: add optional displayMethod to CredentialRequest (#263)
volodymyr-basiuk Sep 5, 2024
e3161bd
fix: credential context order (#266)
volodymyr-basiuk Sep 6, 2024
189e973
Update README.md
0xpulkit Sep 6, 2024
936acba
ability to replace auth bjj with another status (#264)
vmidyllic Sep 9, 2024
fbd0026
update profile nonce, so it can be number or string (#268)
vmidyllic Sep 10, 2024
684292b
feat: add cspell (#267)
volodymyr-basiuk Sep 11, 2024
2185278
update js-iden3-core, js-jwz (#270)
daveroga Sep 16, 2024
1c34e2d
bump to 1.21.0
volodymyr-basiuk Sep 19, 2024
dcd9a6b
merge
volodymyr-basiuk Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/iden3comm/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AcceptProfile } from './types';

const IDEN3_PROTOCOL = 'https://iden3-communication.io/';
/**
* Constants for Iden3 protocol
Expand Down Expand Up @@ -73,5 +75,30 @@ export const SUPPORTED_PUBLIC_KEY_TYPES = {
]
};

export enum ProtocolVersion {
v1 = 'iden3comm/v1'
}

export enum AcceptAuthCircuits {
authV2 = 'authV2',
authV3 = 'authV3'
}

export enum AcceptJwzAlgorithms {
groth16 = 'groth16'
}

export enum AcceptJwsAlgorithms {
ES256K = 'ES256K',
ES256KR = 'ES256K-R'
}

export const defaultAcceptProfile: AcceptProfile = {
protocolVersion: ProtocolVersion.v1,
env: MediaType.ZKPMessage,
circuits: [AcceptAuthCircuits.authV2],
alg: [AcceptJwzAlgorithms.groth16]
};

export const DEFAULT_PROOF_VERIFY_DELAY = 1 * 60 * 60 * 1000; // 1 hour
export const DEFAULT_AUTH_VERIFY_DELAY = 5 * 60 * 1000; // 5 minutes
36 changes: 32 additions & 4 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MediaType } from '../constants';
import { MediaType, ProtocolVersion } from '../constants';
import { IProofService } from '../../proof/proof-service';
import { PROTOCOL_MESSAGE_TYPE } from '../constants';

Expand All @@ -21,6 +21,7 @@ import { byteDecoder, byteEncoder } from '../../utils';
import { processZeroKnowledgeProofRequests } from './common';
import { CircuitId } from '../../circuits';
import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
import { parseAcceptProfile } from '../utils';

/**
* createAuthorizationRequest is a function to create protocol authorization request
Expand Down Expand Up @@ -231,13 +232,40 @@ export class AuthHandler

// override sender did if it's explicitly specified in the auth request
const to = authRequest.to ? DID.parse(authRequest.to) : ctx.senderDid;
const mediaType = ctx.mediaType || MediaType.ZKPMessage;
const guid = uuid.v4();

if (!authRequest.from) {
throw new Error('auth request should contain from field');
}

const responseType = PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE;
let mediaType: MediaType;
if (authRequest.body.accept?.length) {
const supportedMediaTypes: MediaType[] = [];
for (const acceptProfile of authRequest.body.accept || []) {
// 1. check protocol version
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(acceptProfile);
if (protocolVersion === ProtocolVersion.v1 && responseType.split('/').at(-2) !== '1.0') {
continue;
vmidyllic marked this conversation as resolved.
Show resolved Hide resolved
}
// 2. check packer support
if (this._packerMgr.isSupported(env, alg, circuits)) {
supportedMediaTypes.push(env);
}
}

if (!supportedMediaTypes.length) {
throw new Error('no profile meets `access` header requirements');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error('no packer with profile which meets access header requirements');
Comment


mediaType = supportedMediaTypes[0];
if (ctx.mediaType && supportedMediaTypes.includes(ctx.mediaType)) {
mediaType = ctx.mediaType;
}
} else {
mediaType = ctx.mediaType || MediaType.ZKPMessage;
}

const from = DID.parse(authRequest.from);

const responseScope = await processZeroKnowledgeProofRequests(
Expand All @@ -250,8 +278,8 @@ export class AuthHandler

return {
id: guid,
typ: ctx.mediaType,
type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE,
typ: mediaType,
type: responseType,
thid: authRequest.thid ?? guid,
body: {
message: authRequest?.body?.message,
Expand Down
1 change: 1 addition & 0 deletions src/iden3comm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './packers';
export * from './types';
export * from './handlers';
export * from './utils/did';
export * from './utils/accept-profile';

import * as PROTOCOL_CONSTANTS from './constants';
export { PROTOCOL_CONSTANTS };
41 changes: 40 additions & 1 deletion src/iden3comm/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { BasicMessage, IPackageManager, IPacker, PackerParams } from './types';
import { bytesToHeaderStub } from './utils/envelope';
import { base64 } from 'rfc4648';
import { MediaType } from './constants';
import {
AcceptAuthCircuits,
AcceptJwsAlgorithms,
AcceptJwzAlgorithms,
MediaType
} from './constants';
import { byteDecoder, byteEncoder } from '../utils';
import { ZKPPacker } from './packers';

/**
* Basic package manager for iden3 communication protocol
Expand All @@ -21,6 +27,39 @@ export class PackageManager implements IPackageManager {
this.packers = new Map<MediaType, IPacker>();
}

/** {@inheritDoc IPackageManager.isSupported} */
isSupported(
mediaType: MediaType,
algs?: AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[],
circuits?: AcceptAuthCircuits[]
): boolean {
const p = this.packers.get(mediaType);
if (!p) {
return false;
}

let algFound = false || !algs?.length;
if (algs?.length) {
const packerSupportedAlgs = p.getSupportedAlgorithms().map((i) => i.toString());
algFound = algs?.some((alg) => packerSupportedAlgs.includes(alg));
}

let circuitFound = false || !circuits?.length;
if (circuits?.length) {
if (mediaType !== MediaType.ZKPMessage) {
throw new Error('circuits param not supported for ZKPMessage media type');
}
const packerCircuits = (p as ZKPPacker).getSupportedCircuits();
circuitFound = circuits.some((c) => packerCircuits.includes(c));
}
return algFound && circuitFound;
}

/** {@inheritDoc IPackageManager.getSupportedMediaTypes} */
getSupportedMediaTypes(): MediaType[] {
return [...this.packers.keys()];
}

/** {@inheritDoc IPackageManager.registerPackers} */
registerPackers(packers: Array<IPacker>): void {
packers.forEach((p) => {
Expand Down
7 changes: 6 additions & 1 deletion src/iden3comm/packers/jws.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicMessage, IPacker, JWSPackerParams } from '../types';
import { MediaType, SUPPORTED_PUBLIC_KEY_TYPES } from '../constants';
import { AcceptJwsAlgorithms, MediaType, SUPPORTED_PUBLIC_KEY_TYPES } from '../constants';
import { extractPublicKeyBytes, resolveVerificationMethods } from '../utils/did';
import { keyPath, KMS } from '../../kms/';

Expand Down Expand Up @@ -102,6 +102,11 @@ export class JWSPacker implements IPacker {
return MediaType.SignedMessage;
}

/** {@inheritDoc IPacker.getSupportedAlgorithms} */
getSupportedAlgorithms(): AcceptJwsAlgorithms[] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it is unique

return [AcceptJwsAlgorithms.ES256K, AcceptJwsAlgorithms.ES256KR];
}

private async resolveDidDoc(from: string) {
let didDocument: DIDDocument;
try {
Expand Down
7 changes: 6 additions & 1 deletion src/iden3comm/packers/plain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicMessage, IPacker } from '../types';
import { MediaType } from '../constants';
import { AcceptJwsAlgorithms, AcceptJwzAlgorithms, MediaType } from '../constants';
import { byteDecoder, byteEncoder } from '../../utils';

/**
Expand Down Expand Up @@ -53,4 +53,9 @@ export class PlainPacker implements IPacker {
mediaType(): MediaType {
return MediaType.PlainMessage;
}

/** {@inheritDoc IPacker.getSupportedAlgorithms} */
getSupportedAlgorithms(): AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[] {
return [];
}
}
11 changes: 10 additions & 1 deletion src/iden3comm/packers/zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ErrStateVerificationFailed,
ErrUnknownCircuitID
} from '../errors';
import { MediaType } from '../constants';
import { AcceptAuthCircuits, AcceptJwzAlgorithms, MediaType } from '../constants';
import { byteDecoder, byteEncoder } from '../../utils';
import { DEFAULT_AUTH_VERIFY_DELAY } from '../constants';

Expand Down Expand Up @@ -174,6 +174,15 @@ export class ZKPPacker implements IPacker {
mediaType(): MediaType {
return MediaType.ZKPMessage;
}

/** {@inheritDoc IPacker.getSupportedAlgorithms} */
getSupportedAlgorithms(): AcceptJwzAlgorithms[] {
return [AcceptJwzAlgorithms.groth16];
}

getSupportedCircuits(): AcceptAuthCircuits[] {
return [AcceptAuthCircuits.authV2];
}
}

const verifySender = async (token: Token, msg: BasicMessage): Promise<void> => {
Expand Down
1 change: 1 addition & 0 deletions src/iden3comm/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './protocol/revocation';
export * from './protocol/contract-request';
export * from './protocol/proposal-request';
export * from './protocol/payment';
export * from './protocol/accept-profile';

export * from './packer';
export * from './models';
Expand Down
26 changes: 25 additions & 1 deletion src/iden3comm/types/packageManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { BasicMessage, IPacker, PackerParams } from './packer';
import { MediaType } from '../constants';
import {
AcceptAuthCircuits,
AcceptJwsAlgorithms,
AcceptJwzAlgorithms,
MediaType
} from '../constants';

/**
* Interface for defining the registry of packers
Expand Down Expand Up @@ -74,6 +79,25 @@ export interface IPackageManager {
* @returns MediaType
*/
getMediaType(envelope: string): MediaType;

/**
* gets supported media type by packer manager
*
* @returns MediaType[]
*/
getSupportedMediaTypes(): MediaType[];

/**
* gets if media type and algorithms supported by packer manager
*
* @param {MediaType} mediaType
* @returns AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[]
*/
isSupported(
mediaType: MediaType,
alg?: AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[],
circuits?: AcceptAuthCircuits[]
): boolean;
}
/**
* EnvelopeStub is used to stub the jwt based envelops
Expand Down
9 changes: 8 additions & 1 deletion src/iden3comm/types/packer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DID } from '@iden3/js-iden3-core';
import { DataPrepareHandlerFunc, VerificationHandlerFunc } from '../packers';
import { ProvingMethodAlg } from '@iden3/js-jwz';
import { CircuitId } from '../../circuits';
import { MediaType } from '../constants';
import { AcceptJwsAlgorithms, AcceptJwzAlgorithms, MediaType } from '../constants';
import { DIDDocument, VerificationMethod } from 'did-resolver';
import { StateVerificationOpts } from './models';
/**
Expand Down Expand Up @@ -121,6 +121,13 @@ export interface IPacker {
* @returns The media type as a MediaType.
*/
mediaType(): MediaType;

/**
* gets supported algorithms for mediaType
*
* @returns AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[]
*/
getSupportedAlgorithms(): AcceptJwzAlgorithms[] | AcceptJwsAlgorithms[];
}
/**
* Params for verification of auth circuit public signals
Expand Down
14 changes: 14 additions & 0 deletions src/iden3comm/types/protocol/accept-profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
AcceptAuthCircuits,
AcceptJwsAlgorithms,
AcceptJwzAlgorithms,
MediaType,
ProtocolVersion
} from '../../constants';

export type AcceptProfile = {
protocolVersion: ProtocolVersion;
env: MediaType;
circuits?: AcceptAuthCircuits[];
alg?: AcceptJwsAlgorithms[] | AcceptJwzAlgorithms[];
};
1 change: 1 addition & 0 deletions src/iden3comm/types/protocol/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type AuthorizationRequestMessageBody = {
callbackUrl: string;
reason?: string;
message?: string;
accept?: string[];
did_doc?: JSONObject;
scope: Array<ZeroKnowledgeProofRequest>;
};
Expand Down
Loading
Loading