-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
222 additions
and
63 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# App | ||
keys/*.jwk | ||
|
||
# Nuxt | ||
.nuxt | ||
.nitro | ||
|
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,18 +1,18 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "TestUserInfo", | ||
"title": "Test User Info", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"email": { | ||
"type": "string" | ||
} | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "TestUserInfo", | ||
"title": "Test User Info", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"required": [ | ||
"name", | ||
"email" | ||
] | ||
} | ||
"email": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"email" | ||
] | ||
} |
Empty file.
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
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,24 @@ | ||
import { readFile } from 'node:fs/promises' | ||
|
||
import * as jose from 'jose' | ||
|
||
import type { KeyPair } from './types' | ||
|
||
export const keys1: KeyPair = JSON.parse(await readFile( | ||
'keys/key1.jwk', | ||
'utf8', | ||
)) | ||
export const keys2: KeyPair = JSON.parse(await readFile( | ||
'keys/key2.jwk', | ||
'utf8', | ||
)) | ||
|
||
export const keys1Private = await jose.importJWK(keys1.privateKey) | ||
export const keys2Private = await jose.importJWK(keys2.privateKey) | ||
|
||
export const jwks = jose.createLocalJWKSet({ | ||
keys: [ | ||
keys1.publicKey, | ||
keys2.publicKey, | ||
], | ||
}) |
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,41 @@ | ||
import * as jose from 'jose' | ||
|
||
import { keys1Private } from './keys' | ||
|
||
import type { KeyPair } from './types' | ||
|
||
const alg = 'ES256' | ||
const options = { | ||
issuer: 'urn:example:issuer', | ||
audience: 'urn:example:audience', | ||
} | ||
|
||
export async function sign( | ||
keyPair: KeyPair, | ||
payload: jose.JWTPayload, | ||
) { | ||
return new jose.SignJWT(payload) | ||
.setIssuer(options.issuer) | ||
.setAudience(options.audience) | ||
.setProtectedHeader({ | ||
alg, | ||
kid: 'key1', | ||
}) | ||
.setExpirationTime('10m') | ||
.setIssuedAt() | ||
.sign(keys1Private) | ||
} | ||
|
||
export async function verify( | ||
jwt: string, | ||
keySet: jose.JWTVerifyGetKey, | ||
verifyOptions?: jose.VerifyOptions, | ||
) { | ||
const { payload } = await jose.jwtVerify( | ||
jwt, | ||
keySet, | ||
verifyOptions, | ||
) | ||
|
||
return payload | ||
} |
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,5 @@ | ||
import type * as jose from 'jose' | ||
|
||
export type KeyPair = jose.GenerateKeyPairResult< | ||
jose.KeyLike & jose.JWK | ||
> |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { writeFile } from 'node:fs/promises' | ||
|
||
import * as jose from 'jose' | ||
|
||
const alg = 'ES256' | ||
|
||
const keys1 = await jose.generateKeyPair(alg, { extractable: true }) | ||
const keys2 = await jose.generateKeyPair(alg, { extractable: true }) | ||
|
||
const jwk1 = { | ||
publicKey: await jose.exportJWK(keys1.publicKey).then(addKid('key1')), | ||
privateKey: await jose.exportJWK(keys1.privateKey).then(addKid('key1')), | ||
} | ||
|
||
const jwk2 = { | ||
publicKey: await jose.exportJWK(keys2.publicKey).then(addKid('key2')), | ||
privateKey: await jose.exportJWK(keys2.privateKey).then(addKid('key2')), | ||
} | ||
|
||
await writeFile( | ||
`keys/key1.jwk`, | ||
JSON.stringify(jwk1, null, 2), | ||
) | ||
|
||
await writeFile( | ||
`keys/key2.jwk`, | ||
JSON.stringify(jwk2, null, 2), | ||
) | ||
|
||
function addKid(kid: string) { | ||
return (jwk: jose.JWK) => { | ||
jwk.kid = kid | ||
|
||
return jwk | ||
} | ||
} |
Oops, something went wrong.