Skip to content

Commit

Permalink
fix: base64url convention (#179)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
  • Loading branch information
lukasjhan committed Mar 15, 2024
1 parent fec88f0 commit f8db275
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/decoy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HasherAndAlg, SaltGenerator } from '@sd-jwt/types';
import { Uint8ArrayToBase64Url } from '@sd-jwt/utils';
import { uint8ArrayToBase64Url } from '@sd-jwt/utils';

// This function creates a decoy value that can be used to obscure SD JWT payload.
// The value is basically a hash of a random salt. So the value is not predictable.
Expand All @@ -11,5 +11,5 @@ export const createDecoy = async (
const { hasher, alg } = hash;
const salt = await saltGenerator(16);
const decoy = await hasher(salt, alg);
return Uint8ArrayToBase64Url(decoy);
return uint8ArrayToBase64Url(decoy);
};
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SDJWTException, Uint8ArrayToBase64Url } from '@sd-jwt/utils';
import { SDJWTException, uint8ArrayToBase64Url } from '@sd-jwt/utils';
import { Jwt } from './jwt';
import { KBJwt } from './kbjwt';
import { SDJwt, pack } from './sdjwt';
Expand Down Expand Up @@ -250,7 +250,7 @@ export class SDJwtInstance<ExtendedPayload extends SdJwtPayload> {
}
const { _sd_alg } = getSDAlgAndPayload(sdjwt.jwt.payload);
const sdHash = await hasher(presentSdJwtWithoutKb, _sd_alg);
const sdHashStr = Uint8ArrayToBase64Url(sdHash);
const sdHashStr = uint8ArrayToBase64Url(sdHash);
return sdHashStr;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils';
import { base64urlEncode, SDJWTException } from '@sd-jwt/utils';
import type { Base64urlString, Signer, Verifier } from '@sd-jwt/types';
import { decodeJwt } from '@sd-jwt/decode';

Expand Down Expand Up @@ -83,8 +83,8 @@ export class Jwt<
return unsignedToken;
}

const header = Base64urlEncode(JSON.stringify(this.header));
const payload = Base64urlEncode(JSON.stringify(this.payload));
const header = base64urlEncode(JSON.stringify(this.header));
const payload = base64urlEncode(JSON.stringify(this.payload));
return `${header}.${payload}`;
}

Expand All @@ -104,8 +104,8 @@ export class Jwt<
throw new SDJWTException('Serialize Error: Invalid JWT');
}

const header = Base64urlEncode(JSON.stringify(this.header));
const payload = Base64urlEncode(JSON.stringify(this.payload));
const header = base64urlEncode(JSON.stringify(this.header));
const payload = base64urlEncode(JSON.stringify(this.payload));
const signature = this.signature;
const compact = `${header}.${payload}.${signature}`;
this.encoded = compact;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/kbjwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Base64urlEncode, SDJWTException } from '@sd-jwt/utils';
import { SDJWTException } from '@sd-jwt/utils';
import { Jwt } from './jwt';
import {
type JwtPayload,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/test/decoy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createDecoy } from '../decoy';
import { describe, expect, test } from 'vitest';
import { Base64urlEncode } from '@sd-jwt/utils';
import { base64urlEncode } from '@sd-jwt/utils';
import { digest, generateSalt } from '@sd-jwt/crypto-nodejs';

const hash = {
Expand All @@ -21,7 +21,7 @@ describe('Decoy', () => {
// * Contents: ["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"]
test('apply hasher and saltGenerator', async () => {
const decoyValue = await createDecoy(hash, () =>
Base64urlEncode(
base64urlEncode(
'["6Ij7tM-a5iVPGboS5tmvVA", "email", "johndoe@example.com"]',
),
);
Expand Down
6 changes: 3 additions & 3 deletions packages/decode/src/decode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils';
import { base64urlDecode, SDJWTException, Disclosure } from '@sd-jwt/utils';
import {
type Hasher,
type HasherAndAlg,
Expand All @@ -20,8 +20,8 @@ export const decodeJwt = <
}

return {
header: JSON.parse(Base64urlDecode(header)),
payload: JSON.parse(Base64urlDecode(payload)),
header: JSON.parse(base64urlDecode(header)),
payload: JSON.parse(base64urlDecode(payload)),
signature: signature,
};
};
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/base64url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Base64 } from 'js-base64';

export const Base64urlEncode = Base64.encodeURI;
export const base64urlEncode = Base64.encodeURI;

export const Base64urlDecode = Base64.decode;
export const base64urlDecode = Base64.decode;

export const Uint8ArrayToBase64Url = (input: Uint8Array): string =>
export const uint8ArrayToBase64Url = (input: Uint8Array): string =>
Base64.fromUint8Array(input, true);
20 changes: 10 additions & 10 deletions packages/utils/src/disclosure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Uint8ArrayToBase64Url,
Base64urlDecode,
Base64urlEncode,
uint8ArrayToBase64Url,
base64urlDecode,
base64urlEncode,
} from './base64url';
import { SDJWTException } from './error';
import type {
Expand Down Expand Up @@ -45,16 +45,16 @@ export class Disclosure<T = unknown> {
public static async fromEncode<T>(s: string, hash: HasherAndAlg) {
const { hasher, alg } = hash;
const digest = await hasher(s, alg);
const digestStr = Uint8ArrayToBase64Url(digest);
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
const digestStr = uint8ArrayToBase64Url(digest);
const item = JSON.parse(base64urlDecode(s)) as DisclosureData<T>;
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
}

public static fromEncodeSync<T>(s: string, hash: HasherAndAlgSync) {
const { hasher, alg } = hash;
const digest = hasher(s, alg);
const digestStr = Uint8ArrayToBase64Url(digest);
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
const digestStr = uint8ArrayToBase64Url(digest);
const item = JSON.parse(base64urlDecode(s)) as DisclosureData<T>;
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
}

Expand All @@ -69,7 +69,7 @@ export class Disclosure<T = unknown> {
if (!this._encoded) {
// we use JSON.stringify to encode the data
// It's the most reliable and universal way to encode JSON object
this._encoded = Base64urlEncode(JSON.stringify(this.decode()));
this._encoded = base64urlEncode(JSON.stringify(this.decode()));
}
return this._encoded;
}
Expand All @@ -84,7 +84,7 @@ export class Disclosure<T = unknown> {
const { hasher, alg } = hash;
if (!this._digest) {
const hash = await hasher(this.encode(), alg);
this._digest = Uint8ArrayToBase64Url(hash);
this._digest = uint8ArrayToBase64Url(hash);
}

return this._digest;
Expand All @@ -94,7 +94,7 @@ export class Disclosure<T = unknown> {
const { hasher, alg } = hash;
if (!this._digest) {
const hash = hasher(this.encode(), alg);
this._digest = Uint8ArrayToBase64Url(hash);
this._digest = uint8ArrayToBase64Url(hash);
}

return this._digest;
Expand Down
14 changes: 7 additions & 7 deletions packages/utils/src/test/base64url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { describe, expect, test } from 'vitest';
import {
Base64urlDecode,
Base64urlEncode,
Uint8ArrayToBase64Url,
base64urlDecode,
base64urlEncode,
uint8ArrayToBase64Url,
} from '../base64url';

describe('Base64url', () => {
const raw = 'abcdefghijklmnopqrstuvwxyz';
const encoded = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo';
test('Encode', () => {
expect(Base64urlEncode(raw)).toStrictEqual(encoded);
expect(base64urlEncode(raw)).toStrictEqual(encoded);
});
test('Decode', () => {
expect(Base64urlDecode(encoded)).toStrictEqual(raw);
expect(base64urlDecode(encoded)).toStrictEqual(raw);
});
test('Encode and decode', () => {
const str = 'hello world';
expect(Base64urlDecode(Base64urlEncode(str))).toStrictEqual(str);
expect(base64urlDecode(base64urlEncode(str))).toStrictEqual(str);
});
test('Uint8Array', () => {
const str = 'hello world';
const uint8 = new TextEncoder().encode(str);
expect(Uint8ArrayToBase64Url(uint8)).toStrictEqual(Base64urlEncode(str));
expect(uint8ArrayToBase64Url(uint8)).toStrictEqual(base64urlEncode(str));
});
});
4 changes: 2 additions & 2 deletions packages/utils/src/test/disclosure.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateSalt, digest as hasher } from '@sd-jwt/crypto-nodejs';
import { Disclosure } from '../disclosure';
import { describe, expect, test } from 'vitest';
import { Base64urlEncode, type SDJWTException } from '../index';
import { base64urlEncode, type SDJWTException } from '../index';

const hash = { alg: 'SHA256', hasher };

Expand Down Expand Up @@ -144,7 +144,7 @@ describe('Disclosure', () => {
});

test('digest disclosure #3', async () => {
const encoded = Base64urlEncode(TestDataDraft7.claimTests[0].contents);
const encoded = base64urlEncode(TestDataDraft7.claimTests[0].contents);
expect(encoded).toStrictEqual(TestDataDraft7.claimTests[0].disclosure);

const disclosure = await Disclosure.fromEncode(
Expand Down

0 comments on commit f8db275

Please sign in to comment.