Skip to content

Commit

Permalink
Merge branch 'main' into feature/#21
Browse files Browse the repository at this point in the history
  • Loading branch information
aceshim authored Dec 16, 2023
2 parents 8198fb2 + f594737 commit bd70b4e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/test/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ describe('Crypto', () => {
test('Salt', () => {
expect(generateSalt(16).length).toBe(32);
});
test('Salt Error', () => {
expect(() => generateSalt(0)).toThrow();
});
test('Hash SHA-256', async () => {
expect(await digest('df9sf67d0fsdf8')).toBe(
'0d3363fd83fa29a955ac77a4cfb80ac99b05c5e59d4c90fcfd2e4696eaba0e22',
Expand Down
14 changes: 14 additions & 0 deletions src/test/disclosure.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateSalt, digest as hash } from '../crypto';
import { Disclosure } from '../disclosure';
import { SDJWTException } from '../error';

describe('Disclosure', () => {
test('create object disclosure', async () => {
Expand All @@ -20,6 +21,19 @@ describe('Disclosure', () => {
expect(disclosure.salt).toBe(salt);
});

test('create disclosure error', async () => {
const salt = generateSalt(16);
const data: [string, string, string] = [salt, 'name', 'James'];
data.push('any');
expect(() => new Disclosure(data)).toThrow();
try {
new Disclosure(data);
} catch (e: unknown) {
const error = e as SDJWTException;
expect(typeof error.getFullMessage()).toBe('string');
}
});

test('encode disclosure', async () => {
const salt = generateSalt(16);
const disclosure = new Disclosure([salt, 'name', 'James']);
Expand Down
14 changes: 14 additions & 0 deletions src/test/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SDJWTException } from '../error';

describe('Error tests', () => {
test('Detail', () => {
try {
throw new SDJWTException('msg', { info: 'details' });
} catch (e: unknown) {
const exception = e as SDJWTException;
expect(exception.getFullMessage()).toEqual(
'SDJWTException: msg - {"info":"details"}',
);
}
});
});
72 changes: 72 additions & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sdjwt from '../index';
import Crypto from 'node:crypto';

describe('index', () => {
test('create', async () => {
const SDJwtInstance = sdjwt.create({
omitTyp: true,
});
expect(SDJwtInstance).toBeDefined();
});

test('issue kbJwt', async () => {
const { privateKey } = Crypto.generateKeyPairSync('ed25519');
const credential = await sdjwt.issue(
{
foo: 'bar',
},
privateKey,
{
_sd: ['foo'],
},
{
kb: {
alg: 'EdDSA',
payload: {
_sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
},
privateKey,
},
},
);

expect(credential).toBeDefined();
});

test('verify failed', async () => {
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
const credential = await sdjwt.issue(
{
foo: 'bar',
},
privateKey,
{
_sd: ['foo'],
},
{
kb: {
alg: 'EdDSA',
payload: {
_sd_hash: 'sha-256',
aud: '1',
iat: 1,
nonce: '342',
},
privateKey,
},
},
);

try {
await sdjwt.verify(
credential,
Crypto.generateKeyPairSync('ed25519').privateKey,
);
} catch (e) {
expect(e).toBeDefined();
}
});
});
47 changes: 47 additions & 0 deletions src/test/jwt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SDJWTException } from '../error';
import { Jwt } from '../jwt';
import Crypto from 'node:crypto';

Expand Down Expand Up @@ -43,6 +44,10 @@ describe('JWT', () => {
const newJwt = Jwt.fromEncode(encodedJwt);
const verified = await newJwt.verify(publicKey);
expect(verified).toBe(true);
const notVerified = await newJwt.verify(
Crypto.generateKeyPairSync('ed25519').privateKey,
);
expect(notVerified).toBe(false);
});

test('encode', async () => {
Expand All @@ -57,4 +62,46 @@ describe('JWT', () => {
const newEncodedJwt = newJwt.encodeJwt();
expect(newEncodedJwt).toBe(encodedJwt);
});

test('decode failed', () => {
expect(() => Jwt.fromEncode('asfasfas')).toThrow();
});

test('sign failed', async () => {
const { privateKey } = Crypto.generateKeyPairSync('ed25519');
const jwt = new Jwt({
header: { alg: 'EdDSA' },
});

try {
await jwt.sign(privateKey);
} catch (e: unknown) {
expect(e).toBeInstanceOf(SDJWTException);
}
});

test('encode failed', async () => {
const jwt = new Jwt({
header: { alg: 'EdDSA' },
});

try {
jwt.encodeJwt();
} catch (e: unknown) {
expect(e).toBeInstanceOf(SDJWTException);
}
});

test('verify failed', async () => {
const { privateKey } = Crypto.generateKeyPairSync('ed25519');
const jwt = new Jwt({
header: { alg: 'EdDSA' },
});

try {
await jwt.verify(privateKey);
} catch (e: unknown) {
expect(e).toBeInstanceOf(SDJWTException);
}
});
});
27 changes: 26 additions & 1 deletion src/test/kbjwt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SDJWTException } from '../error';
import { KBJwt } from '../kbjwt';
import { KB_JWT_TYP } from '../type';
import { KB_JWT_TYP, SDJWTCompact } from '../type';
import Crypto from 'node:crypto';

describe('KB JWT', () => {
Expand Down Expand Up @@ -76,4 +77,28 @@ describe('KB JWT', () => {
const verified = await decoded.verify(publicKey);
expect(verified).toBe(true);
});

test('verify failed', async () => {
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
const kbJwt = new KBJwt({
header: {
typ: KB_JWT_TYP,
alg: 'EdDSA',
},
payload: {
iat: 1,
aud: 'aud',
nonce: 'nonce',
_sd_hash: '',
},
});
const encodedKbJwt = await kbJwt.sign(privateKey);
const decoded = KBJwt.fromKBEncode(encodedKbJwt);
try {
await decoded.verify(publicKey);
} catch (e: unknown) {
const error = e as SDJWTException;
expect(error.message).toBe('Invalid Key Binding Jwt');
}
});
});

0 comments on commit bd70b4e

Please sign in to comment.