-
Notifications
You must be signed in to change notification settings - Fork 71
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
chore: remove dev dependency did-key-creator #274
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ import { ES256Signer } from '../signers/ES256Signer' | |
import * as jwt from 'jsonwebtoken' | ||
import * as u8a from 'uint8arrays' | ||
import * as jwkToPem from 'jwk-to-pem' | ||
import { encodeDIDfromHexString } from 'did-key-creator' | ||
|
||
const NOW = 1485321133 | ||
MockDate.set(NOW * 1000 + 123) | ||
|
@@ -121,151 +120,156 @@ const audDidDoc = { | |
|
||
describe('createJWT()', () => { | ||
describe('ES256', () => { | ||
const alg = 'ES256' | ||
const privateKey = '736f625c9dda78a94bb16840c82779bb7bc18014b8ede52f0f03429902fc4ba8' | ||
const publicKey = '0314c58e581c7656ba153195669fe4ce53ff78dd5ede60a4039771a90c58cb41de' | ||
const publicKey_x = '14c58e581c7656ba153195669fe4ce53ff78dd5ede60a4039771a90c58cb41de' | ||
const publicKey_y = 'ec41869995bd661849414c523c7dff9a96f1c8dbc2e5e78172118f91c7199869' | ||
// construct did:key for secp256r1 (unlike did for secp256k1 which is for an Ethereum Address) | ||
const multicodecName = 'p256-pub'; | ||
const did = encodeDIDfromHexString(multicodecName,publicKey) | ||
|
||
const signer = ES256Signer(hexToBytes(privateKey)) | ||
|
||
interface privateJsonWebKey extends JsonWebKey { | ||
d?: string | ||
} | ||
|
||
// verifyTokenFormAndValidity | ||
function verifyTokenFormAndValidity(token: string, pemPublic: string): boolean { | ||
let result = null | ||
try { | ||
jwt.verify(token, pemPublic) | ||
result = true | ||
} catch (e) { | ||
console.error(e.name + ': ' + e.message) | ||
result = false | ||
} | ||
return result | ||
} | ||
|
||
// input public key in hex, and export pem | ||
function publicToJWK( | ||
publicPointHex_x: string, | ||
publicPointHex_y: string, | ||
kty_value: string, | ||
crv_value: string | ||
): JsonWebKey { | ||
if (publicPointHex_x.length % 2 != 0) { | ||
publicPointHex_x = '0' + publicPointHex_x | ||
} | ||
if (publicPointHex_y.length % 2 != 0) { | ||
publicPointHex_y = '0' + publicPointHex_y | ||
} | ||
const publicPointUint8_x = u8a.fromString(publicPointHex_x, 'hex') | ||
const publicPointBase64URL_x = u8a.toString(publicPointUint8_x, 'base64url') | ||
const publicPointUint8_y = u8a.fromString(publicPointHex_y, 'hex') | ||
const publicPointBase64URL_y = u8a.toString(publicPointUint8_y, 'base64url') | ||
return { | ||
kty: kty_value, | ||
crv: crv_value, | ||
x: publicPointBase64URL_x, | ||
y: publicPointBase64URL_y, | ||
} | ||
} | ||
const alg = 'ES256' | ||
const privateKey = '736f625c9dda78a94bb16840c82779bb7bc18014b8ede52f0f03429902fc4ba8' | ||
const publicKey_x = '14c58e581c7656ba153195669fe4ce53ff78dd5ede60a4039771a90c58cb41de' | ||
const publicKey_y = 'ec41869995bd661849414c523c7dff9a96f1c8dbc2e5e78172118f91c7199869' | ||
|
||
|
||
// input private key in hex, and export pem | ||
function privateToJWK(privatePointHex: string, kty_value: string, crv_value: string): privateJsonWebKey { | ||
if (privatePointHex.length % 2 != 0) { | ||
privatePointHex = '0' + privatePointHex | ||
} | ||
const privatePointUint8 = u8a.fromString(privatePointHex, 'hex') | ||
const privatePointBase64URL = u8a.toString(privatePointUint8, 'base64url') | ||
return { | ||
kty: kty_value, | ||
crv: crv_value, | ||
d: privatePointBase64URL, | ||
} | ||
// construct did:key for secp256r1 (unlike did for secp256k1 which is for an Ethereum Address) | ||
// This originally was constructed by `encodeDIDfromHextString` imported from `did-key-creator` | ||
// package, but that dependency was removed, so now `did` is just hardcoded | ||
// const multicodecName = 'p256-pub'; | ||
// const publicKey = '0314c58e581c7656ba153195669fe4ce53ff78dd5ede60a4039771a90c58cb41de' | ||
// const did = encodeDIDfromHexString(multicodecName,publicKey) | ||
const did = 'did:key:zDnaej4NHntda4rNW4FBUJgFzdcgEAXKGRVGE8LuVfRbuMuc1' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the actual change |
||
|
||
const signer = ES256Signer(hexToBytes(privateKey)) | ||
|
||
interface privateJsonWebKey extends JsonWebKey { | ||
d?: string | ||
} | ||
|
||
// verifyTokenFormAndValidity | ||
function verifyTokenFormAndValidity(token: string, pemPublic: string): boolean { | ||
let result = null | ||
try { | ||
jwt.verify(token, pemPublic) | ||
result = true | ||
} catch (e) { | ||
console.error(e.name + ': ' + e.message) | ||
result = false | ||
} | ||
return result | ||
} | ||
|
||
it('creates a valid JWT', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'}) | ||
const pemPublic = jwkToPem.default(publicToJWK(publicKey_x,publicKey_y,'EC','P-256')) | ||
expect(verifyTokenFormAndValidity(jwt,pemPublic)).toBe(true) | ||
}) | ||
// input public key in hex, and export pem | ||
function publicToJWK( | ||
publicPointHex_x: string, | ||
publicPointHex_y: string, | ||
kty_value: string, | ||
crv_value: string | ||
): JsonWebKey { | ||
if (publicPointHex_x.length % 2 != 0) { | ||
publicPointHex_x = '0' + publicPointHex_x | ||
} | ||
if (publicPointHex_y.length % 2 != 0) { | ||
publicPointHex_y = '0' + publicPointHex_y | ||
} | ||
const publicPointUint8_x = u8a.fromString(publicPointHex_x, 'hex') | ||
const publicPointBase64URL_x = u8a.toString(publicPointUint8_x, 'base64url') | ||
const publicPointUint8_y = u8a.fromString(publicPointHex_y, 'hex') | ||
const publicPointBase64URL_y = u8a.toString(publicPointUint8_y, 'base64url') | ||
return { | ||
kty: kty_value, | ||
crv: crv_value, | ||
x: publicPointBase64URL_x, | ||
y: publicPointBase64URL_y, | ||
} | ||
} | ||
|
||
it('creates a valid JWT using a MNID', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: address, signer },{alg: 'ES256'}) | ||
const pemPublic = jwkToPem.default(publicToJWK(publicKey_x,publicKey_y,'EC','P-256')) | ||
expect(verifyTokenFormAndValidity(jwt,pemPublic)).toBe(true) | ||
}) | ||
|
||
it('creates a JWT with correct format', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'}) | ||
return expect(decodeJWT(jwt)).toMatchSnapshot() | ||
}) | ||
|
||
it('creates a JWT with correct legacy format', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: address, signer },{alg: 'ES256'}) | ||
return expect(decodeJWT(jwt)).toMatchSnapshot() | ||
}) | ||
|
||
it('creates a JWT with expiry in 10000 seconds', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT( | ||
{ | ||
requested: ['name', 'phone'], | ||
nbf: Math.floor(new Date().getTime() / 1000), | ||
}, | ||
{ issuer: did, signer, expiresIn: 10000 }, | ||
{alg: 'ES256'} | ||
// input private key in hex, and export pem | ||
function privateToJWK(privatePointHex: string, kty_value: string, crv_value: string): privateJsonWebKey { | ||
if (privatePointHex.length % 2 != 0) { | ||
privatePointHex = '0' + privatePointHex | ||
} | ||
const privatePointUint8 = u8a.fromString(privatePointHex, 'hex') | ||
const privatePointBase64URL = u8a.toString(privatePointUint8, 'base64url') | ||
return { | ||
kty: kty_value, | ||
crv: crv_value, | ||
d: privatePointBase64URL, | ||
} | ||
} | ||
|
||
it('creates a valid JWT', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'}) | ||
const pemPublic = jwkToPem.default(publicToJWK(publicKey_x,publicKey_y,'EC','P-256')) | ||
expect(verifyTokenFormAndValidity(jwt,pemPublic)).toBe(true) | ||
}) | ||
|
||
it('creates a valid JWT using a MNID', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: address, signer },{alg: 'ES256'}) | ||
const pemPublic = jwkToPem.default(publicToJWK(publicKey_x,publicKey_y,'EC','P-256')) | ||
expect(verifyTokenFormAndValidity(jwt,pemPublic)).toBe(true) | ||
}) | ||
|
||
it('creates a JWT with correct format', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'}) | ||
return expect(decodeJWT(jwt)).toMatchSnapshot() | ||
}) | ||
|
||
it('creates a JWT with correct legacy format', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ requested: ['name', 'phone'] }, { issuer: address, signer },{alg: 'ES256'}) | ||
return expect(decodeJWT(jwt)).toMatchSnapshot() | ||
}) | ||
|
||
it('creates a JWT with expiry in 10000 seconds', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT( | ||
{ | ||
requested: ['name', 'phone'], | ||
nbf: Math.floor(new Date().getTime() / 1000), | ||
}, | ||
{ issuer: did, signer, expiresIn: 10000 }, | ||
{alg: 'ES256'} | ||
) | ||
const { payload } = decodeJWT(jwt) | ||
return expect(payload.exp).toEqual(payload.nbf + 10000) | ||
}) | ||
it('Uses iat if nbf is not defined but expiresIn is included', async () => { | ||
expect.assertions(1) | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, expiresIn: 10000 },{alg: 'ES256'}) | ||
) | ||
return expect(payload.exp).toEqual(payload.iat + 10000) | ||
}) | ||
it('sets iat to the current time by default', async () => { | ||
expect.assertions(1) | ||
const timestamp = Math.floor(Date.now() / 1000) | ||
const { payload } = decodeJWT(await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'})) | ||
return expect(payload.iat).toEqual(timestamp) | ||
}) | ||
it('sets iat to the value passed in payload', async () => { | ||
expect.assertions(1) | ||
const timestamp = 2000000 | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'], iat: timestamp }, { issuer: did, signer },{alg: 'ES256'}) | ||
) | ||
return expect(payload.iat).toEqual(timestamp) | ||
}) | ||
it('does not set iat if value in payload is undefined', async () => { | ||
expect.assertions(1) | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'], iat: undefined }, { issuer: did, signer },{alg: 'ES256'}) | ||
) | ||
return expect(payload.iat).toBeUndefined() | ||
}) | ||
it('throws an error if unsupported algorithm is passed in', async () => { | ||
expect.assertions(1) | ||
await expect( | ||
createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, alg: 'BADALGO' }) | ||
).rejects.toThrowError('Unsupported algorithm BADALGO') | ||
}) | ||
const { payload } = decodeJWT(jwt) | ||
return expect(payload.exp).toEqual(payload.nbf + 10000) | ||
}) | ||
|
||
it('Uses iat if nbf is not defined but expiresIn is included', async () => { | ||
expect.assertions(1) | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, expiresIn: 10000 },{alg: 'ES256'}) | ||
) | ||
return expect(payload.exp).toEqual(payload.iat + 10000) | ||
}) | ||
|
||
it('sets iat to the current time by default', async () => { | ||
expect.assertions(1) | ||
const timestamp = Math.floor(Date.now() / 1000) | ||
const { payload } = decodeJWT(await createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer },{alg: 'ES256'})) | ||
return expect(payload.iat).toEqual(timestamp) | ||
}) | ||
|
||
it('sets iat to the value passed in payload', async () => { | ||
expect.assertions(1) | ||
const timestamp = 2000000 | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'], iat: timestamp }, { issuer: did, signer },{alg: 'ES256'}) | ||
) | ||
return expect(payload.iat).toEqual(timestamp) | ||
}) | ||
|
||
it('does not set iat if value in payload is undefined', async () => { | ||
expect.assertions(1) | ||
const { payload } = decodeJWT( | ||
await createJWT({ requested: ['name', 'phone'], iat: undefined }, { issuer: did, signer },{alg: 'ES256'}) | ||
) | ||
return expect(payload.iat).toBeUndefined() | ||
}) | ||
|
||
it('throws an error if unsupported algorithm is passed in', async () => { | ||
expect.assertions(1) | ||
await expect( | ||
createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, alg: 'BADALGO' }) | ||
).rejects.toThrowError('Unsupported algorithm BADALGO') | ||
}) | ||
|
||
}) | ||
}) | ||
|
@@ -441,8 +445,9 @@ describe('verifyJWT() for ES256', () => { | |
const publicKey_x = '14c58e581c7656ba153195669fe4ce53ff78dd5ede60a4039771a90c58cb41de' | ||
const publicKey_y = 'ec41869995bd661849414c523c7dff9a96f1c8dbc2e5e78172118f91c7199869' | ||
// construct did:key for secp256r1 (unlike did for secp256k1 which is for an Ethereum Address) | ||
const multicodecName = 'p256-pub'; | ||
const did = encodeDIDfromHexString(multicodecName,publicKey) | ||
// const multicodecName = 'p256-pub'; | ||
// const did = encodeDIDfromHexString(multicodecName,publicKey) | ||
const did = 'did:key:zDnaej4NHntda4rNW4FBUJgFzdcgEAXKGRVGE8LuVfRbuMuc1' | ||
|
||
const didDoc = { | ||
didDocument: { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just whitespace changes