-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added tests that use node:crypto
Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>
- Loading branch information
1 parent
df7aea3
commit 2e6050f
Showing
7 changed files
with
206 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import assert from 'node:assert' | ||
import { subtle } from 'node:crypto' | ||
import { describe, it } from 'node:test' | ||
|
||
import nock from 'nock' | ||
|
||
import { createEntityConfiguration, fetchEntityConfiguration } from '../src/entityConfiguration' | ||
import { type EntityStatementClaims, createEntityStatement, fetchEntityStatement } from '../src/entityStatement' | ||
import type { SignCallback, VerifyCallback } from '../src/utils' | ||
|
||
describe('End To End', async () => { | ||
const key = await subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']) | ||
const exportedKey = await subtle.exportKey('jwk', key.publicKey) | ||
const publicKeyJwk = { | ||
kid: 'some-id', | ||
kty: 'EC', | ||
key_ops: exportedKey.key_ops, | ||
x: exportedKey.x, | ||
y: exportedKey.y, | ||
} | ||
|
||
const signJwtCallback: SignCallback = async ({ toBeSigned }) => | ||
new Uint8Array(await subtle.sign({ hash: 'SHA-256', name: 'ECDSA' }, key.privateKey, toBeSigned)) | ||
|
||
const verifyJwtCallback: VerifyCallback = async ({ signature, data }) => | ||
subtle.verify({ name: 'ECDSA', hash: 'SHA-256' }, key.publicKey, signature, data) | ||
|
||
it('should fetch an entity configuration', async () => { | ||
const iss = 'https://example.org' | ||
|
||
const claims = { | ||
iss, | ||
sub: iss, | ||
exp: new Date(), | ||
iat: new Date(), | ||
jwks: { | ||
keys: [publicKeyJwk], | ||
}, | ||
} | ||
|
||
const entityConfigurationJwt = await createEntityConfiguration({ | ||
signJwtCallback, | ||
claims, | ||
header: { | ||
kid: 'some-id', | ||
typ: 'entity-statement+jwt', | ||
}, | ||
}) | ||
|
||
const scope = nock(iss).get('/.well-known/openid-federation').reply(200, entityConfigurationJwt, { | ||
'content-type': 'application/entity-statement+jwt', | ||
}) | ||
|
||
const fetchedEntityConfigurationClaims = await fetchEntityConfiguration({ | ||
entityId: iss, | ||
verifyJwtCallback, | ||
}) | ||
|
||
assert.deepStrictEqual(fetchedEntityConfigurationClaims, claims) | ||
|
||
scope.done() | ||
}) | ||
|
||
it('should fetch an entity statement', async () => { | ||
const iss = 'https://example.org' | ||
const sub = 'https://sub.example.org' | ||
|
||
const entityConfigurationJwt = await createEntityConfiguration({ | ||
signJwtCallback, | ||
claims: { | ||
iss, | ||
sub: iss, | ||
exp: new Date(), | ||
iat: new Date(), | ||
jwks: { | ||
keys: [publicKeyJwk], | ||
}, | ||
source_endpoint: `${iss}/fetch`, | ||
}, | ||
header: { | ||
kid: 'some-id', | ||
typ: 'entity-statement+jwt', | ||
}, | ||
}) | ||
|
||
const entityStamentClaims: EntityStatementClaims = { | ||
iss, | ||
sub: iss, | ||
exp: new Date(), | ||
iat: new Date(), | ||
jwks: { | ||
keys: [], | ||
}, | ||
authority_hints: [iss], | ||
metadata: { | ||
federation_entity: { | ||
organization_name: 'my org!', | ||
}, | ||
}, | ||
} | ||
|
||
const entityStatementJwt = await createEntityStatement({ | ||
signJwtCallback, | ||
claims: entityStamentClaims, | ||
header: { | ||
kid: 'some-id', | ||
typ: 'entity-statement+jwt', | ||
}, | ||
jwk: publicKeyJwk, | ||
}) | ||
|
||
const scope = nock(iss) | ||
.get('/.well-known/openid-federation') | ||
.reply(200, entityConfigurationJwt, { | ||
'content-type': 'application/entity-statement+jwt', | ||
}) | ||
.get('/fetch') | ||
.query({ iss, sub }) | ||
.reply(200, entityStatementJwt, { | ||
'content-type': 'application/entity-statement+jwt', | ||
}) | ||
|
||
const fetchedEntityConfigurationClaims = await fetchEntityConfiguration({ | ||
entityId: iss, | ||
verifyJwtCallback, | ||
}) | ||
|
||
const fetchedEntityStatementClaims = await fetchEntityStatement({ | ||
iss, | ||
sub, | ||
issEntityConfiguration: fetchedEntityConfigurationClaims, | ||
verifyJwtCallback, | ||
}) | ||
|
||
assert.deepStrictEqual(fetchedEntityStatementClaims, entityStamentClaims) | ||
|
||
scope.done() | ||
}) | ||
}) |
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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './entityStatementClaims' | ||
export * from './createEntityStatement' | ||
export * from './fetchEntityStatement' |
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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { z } from 'zod' | ||
|
||
export const jsonWebKeySchema = z.object({ | ||
kty: z.string(), | ||
// TODO: spec mentions kid may be undefined, but we always need a key id for open id federation | ||
kid: z.string(), | ||
use: z.string().optional(), | ||
key_ops: z.string().optional(), | ||
alg: z.string().optional(), | ||
x5u: z.string().optional(), | ||
x5c: z.string().optional(), | ||
x5t: z.string().optional(), | ||
'x5t#S256': z.string().optional(), | ||
}) | ||
export const jsonWebKeySchema = z | ||
.object({ | ||
kty: z.string(), | ||
// TODO: spec mentions kid may be undefined, but we always need a key id for open id federation | ||
kid: z.string(), | ||
use: z.string().optional(), | ||
key_ops: z.array(z.string()).optional(), | ||
alg: z.string().optional(), | ||
x5u: z.string().optional(), | ||
x5c: z.string().optional(), | ||
x5t: z.string().optional(), | ||
'x5t#S256': z.string().optional(), | ||
}) | ||
.passthrough() | ||
|
||
export type JsonWebKey = z.input<typeof jsonWebKeySchema> |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Buffer } from 'node:buffer' | ||
|
||
type JsonWebTokenParts = { | ||
header: Record<string, unknown> | ||
claims: Record<string, unknown> | ||
signature: Uint8Array | ||
signableInput: Uint8Array | ||
} | ||
|
||
export const parseJsonWebToken = (jwt: string): JsonWebTokenParts => { | ||
const [encodedHeader, encodedClaims, encodedSignature] = jwt.split('.') | ||
|
||
if (!encodedHeader) { | ||
throw new Error('could not find the header in the JWT') | ||
} | ||
|
||
if (!encodedClaims) { | ||
throw new Error('could not find the claims in the JWT') | ||
} | ||
|
||
if (!encodedSignature) { | ||
throw new Error('could not find the signature in the JWT') | ||
} | ||
|
||
const header = JSON.parse(Buffer.from(encodedHeader, 'base64url').toString()) | ||
const claims = JSON.parse(Buffer.from(encodedClaims, 'base64url').toString()) | ||
const signature = new Uint8Array(Buffer.from(encodedSignature, 'base64url')) | ||
|
||
return { | ||
header, | ||
claims, | ||
signature, | ||
signableInput: new Uint8Array(Buffer.from(`${encodedHeader}.${encodedClaims}`)), | ||
} | ||
} |
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