Skip to content

Commit 7e4d844

Browse files
authored
Merge pull request #637 from govuk-one-login/OLH-3836-stub-jwks-json
OLH-3836: Add /.well-known/jwks.json stub route
2 parents 809e84f + 49bfaa9 commit 7e4d844

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/amc/amc-routes.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { APIGatewayProxyEvent } from "aws-lambda";
2+
import { generateJwks } from "./utils/generate-jwks";
3+
24

35
export const handler = async (event: APIGatewayProxyEvent) => {
46
if (event.path === "/status") {
@@ -8,6 +10,17 @@ export const handler = async (event: APIGatewayProxyEvent) => {
810
};
911
}
1012

13+
if (event.path === "/.well-known/jwks.json" && event.httpMethod === "GET") {
14+
const jwks = await generateJwks();
15+
return {
16+
statusCode: 200,
17+
headers: {
18+
"Content-Type": "application/json",
19+
},
20+
body: JSON.stringify(jwks),
21+
};
22+
}
23+
1124
return {
1225
statusCode: 404,
1326
body: JSON.stringify({ error: "Not Found" }),

src/amc/utils/generate-jwks.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as jose from "jose";
2+
import { randomUUID } from "node:crypto";
3+
4+
let cachedJWK: unknown | null = null;
5+
6+
export async function generateJwks() {
7+
if (cachedJWK) {
8+
return cachedJWK;
9+
}
10+
11+
const { publicKey } = await jose.generateKeyPair("RSA-OAEP-256");
12+
13+
const publicJwk = await jose.exportJWK(publicKey);
14+
15+
cachedJWK = {
16+
keys: [
17+
{
18+
...publicJwk,
19+
kid: randomUUID(),
20+
alg: "RSA-OAEP-256",
21+
use: "enc",
22+
},
23+
],
24+
};
25+
26+
return cachedJWK;
27+
}

template.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ Resources:
13801380
Method: GET
13811381
RestApiId:
13821382
Ref: AmcStubsRestApi
1383+
jwks:
1384+
Type: Api
1385+
Properties:
1386+
Path: /.well-known/jwks.json
1387+
Method: GET
1388+
RestApiId:
1389+
Ref: AmcStubsRestApi
13831390
VpcConfig:
13841391
SubnetIds:
13851392
- Fn::ImportValue: !Sub ${VpcStackName}-PrivateSubnetIdA

0 commit comments

Comments
 (0)