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

[#26]Provide custom header in issue API #27

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 examples/custom_header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sdjwt, { DisclosureFrame } from '@hopae/sd-jwt';
import Crypto from 'node:crypto';

export const createKeyPair = () => {
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
return { privateKey, publicKey };
};

(async () => {
const { privateKey, publicKey } = createKeyPair();
const claims = {
firstname: 'John',
lastname: 'Doe',
ssn: '123-45-6789',
id: '1234',
};
const disclosureFrame: DisclosureFrame<typeof claims> = {
_sd: ['firstname', 'id'],
};

const encodedSdjwt = await sdjwt.issue(claims, privateKey, disclosureFrame, {
header: { typ: 'vc+sd-jwt', custom: 'data' },
});
console.log('encodedSdjwt:', encodedSdjwt);
const sdjwttoken = sdjwt.decode(encodedSdjwt);
console.log(sdjwttoken);
})();
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"validate": "ts-node validate.ts",
"verify": "ts-node verify.ts",
"custom": "ts-node custom.ts",
"decoy": "ts-node decoy.ts"
"decoy": "ts-node decoy.ts",
"custom_header": "ts-node custom_header.ts"
},
"keywords": [],
"author": "",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class SDJwtInstance {
privateKey: Uint8Array | KeyLike,
disclosureFrame?: DisclosureFrame<Payload>,
options?: {
header?: object;
sign_alg?: string;
hash_alg?: string;
kb?: {
Expand All @@ -85,7 +86,11 @@ export class SDJwtInstance {
this.userConfig.saltGenerator ?? defaultConfig.saltGenerator,
);
const alg = options?.sign_alg ?? SDJwtInstance.DEFAULT_ALG;
const header = this.userConfig.omitTyp ? { alg } : { alg, typ: SD_JWT_TYP };
const OptionHeader = options?.header ?? {};
const CustomHeader = this.userConfig.omitTyp
? OptionHeader
: { typ: SD_JWT_TYP, ...OptionHeader };
const header = { ...CustomHeader, alg };
const jwt = new Jwt({
header,
payload: {
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Jwt } from './jwt';
export const SD_SEPARATOR = '~';
export const SD_LIST_KEY = '...';
export const SD_DIGEST = '_sd';
export const SD_JWT_TYP = 'sd+jwt';
export const SD_JWT_TYP = 'sd-jwt';
export const SD_DECOY = '_sd_decoy';
export const KB_JWT_TYP = 'kb+jwt';

Expand Down
Loading