Skip to content

Commit f5124b1

Browse files
Merge pull request #46 from authts/fix-metadata-fetch
fix ProtectedApp
2 parents 6aca5c8 + b04816d commit f5124b1

File tree

7 files changed

+106
-21
lines changed

7 files changed

+106
-21
lines changed

.env.sample

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ KC_DB_SCHEMA=public
1818
KC_DB_USERNAME=admin
1919
KC_DB_PASSWORD=juggle-prance-shallot-wireless-outlet
2020
KC_HTTP_PORT=8080
21+
KC_HTTP_MANAGEMENT_PORT=9000
22+
KC_HEALTH_ENABLED=true
2123

2224
################################################################################
2325
# mailhog
@@ -29,7 +31,9 @@ MH_UI_BIND_ADDR=0.0.0.0:${MH_UI_BIND_ADDR_PORT}
2931
# api
3032
################################################################################
3133
API_PORT=5174
32-
API_JSON_WEB_KEY_SET_URL=http://kc:${KC_HTTP_PORT}/realms/master/protocol/openid-connect/certs
34+
API_AUTH_HEALTH_CHECK_URL=http://kc:${KC_HTTP_MANAGEMENT_PORT}/health/ready
35+
API_AUTH_WELL_KNOWN_CONFIG_URL=http://kc:${KC_HTTP_PORT}/realms/master/.well-known/openid-configuration
36+
API_AUTH_JSON_WEB_KEY_SET_URL=http://kc:${KC_HTTP_PORT}/realms/master/protocol/openid-connect/certs
3337

3438
################################################################################
3539
# react

api/package-lock.json

Lines changed: 68 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
},
1010
"dependencies": {
1111
"express": "^4.21.2",
12-
"jose": "^5.9.6"
12+
"jose": "^5.9.6",
13+
"morgan": "^1.10.0"
1314
},
1415
"devDependencies": {
1516
"@biomejs/biome": "^1.9.4",
1617
"@tsconfig/node20": "^20.1.4",
1718
"@types/express": "^4.17.21",
19+
"@types/morgan": "^1.9.9",
1820
"@types/node": "^22.13.1",
1921
"tsx": "^4.19.2",
2022
"typescript": "^5.7.3"

api/src/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
import express from 'express';
2+
import morgan from 'morgan';
23
import { verifyJwtMiddleware } from './jwtUtils.js';
34
import type { AugmentedRequest } from './types.js';
45

6+
type CustomError = {
7+
error: string;
8+
};
9+
510
const app = express();
611

712
// biome-ignore lint/style/noNonNullAssertion: We expect this env var to always be populated
813
const port = Number(process.env.API_PORT!);
914

10-
app.use(verifyJwtMiddleware);
15+
app.use(morgan('tiny'));
16+
17+
app.get('/auth-well-known-config', async (_req, res) => {
18+
try {
19+
// biome-ignore lint/style/noNonNullAssertion: We expect this env var to always be populated
20+
const response = await fetch(process.env.API_AUTH_WELL_KNOWN_CONFIG_URL!, {
21+
headers: { accept: 'application/json' },
22+
});
23+
if (!response.ok) {
24+
return res.status(500).json({ error: `Unexpected response status: ${response.status}` } satisfies CustomError);
25+
}
26+
const body = await response.json();
27+
return res.json(body);
28+
} catch (error) {
29+
console.log(error);
30+
return res.status(500).json({ error: String(error) } satisfies CustomError);
31+
}
32+
});
1133

12-
app.get('/payload', (req, res) => {
34+
app.get('/payload', verifyJwtMiddleware, (req, res) => {
1335
const data = (req as AugmentedRequest).payload;
1436
res.json(data);
1537
});

api/src/jwtUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type JWTVerifyGetKey, createRemoteJWKSet, jwtVerify } from 'jose';
33
import type { AugmentedRequest } from './types.js';
44

55
// biome-ignore lint/style/noNonNullAssertion: We expect this env var to always be populated
6-
const jsonWebKeySetUrl = process.env.API_JSON_WEB_KEY_SET_URL!;
6+
const jsonWebKeySetUrl = process.env.API_AUTH_JSON_WEB_KEY_SET_URL!;
77

88
// This function is cached so that the json web key set is not looked up on every request
99
let getJsonWebKeySet: JWTVerifyGetKey | null = null;

compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
context: ./kc
1616
ports:
1717
- ${KC_HTTP_PORT}:${KC_HTTP_PORT}
18+
- ${KC_HTTP_MANAGEMENT_PORT}:${KC_HTTP_MANAGEMENT_PORT}
1819
env_file:
1920
- ./.env
2021

react/src/components/ProtectedApp.tsx

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@ import { type FC, type ReactNode, useEffect, useState } from 'react';
33
import { hasAuthParams, useAuth } from 'react-oidc-context';
44
import { Alert } from './Alert.tsx';
55

6-
const getMetadata = async (metadataUrl?: string) => {
7-
if (!metadataUrl) {
8-
throw new Error('metadataUrl is required');
9-
}
10-
let response: Response;
11-
try {
12-
response = await fetch(metadataUrl, {
13-
mode: 'no-cors',
14-
headers: { accept: 'application/jwk-set+json, application/json' },
15-
});
16-
} catch {
17-
throw new Error(`Unable to fetch metadataUrl\n\n${metadataUrl}\n\nPlease confirm your auth server is up`);
18-
}
6+
const queryFn = async () => {
7+
const response = await fetch('/api/auth-well-known-config');
198
if (!response.ok) {
20-
throw new Error(`Unexpected response status: ${response.status}`);
9+
throw new Error('Please confirm your auth server is up');
2110
}
2211
return await response.json();
2312
};
@@ -31,8 +20,8 @@ export const ProtectedApp: FC<ProtectedAppProps> = (props) => {
3120

3221
const { isPending: metadataIsPending, error: metadataError } = useQuery({
3322
queryKey: ['getMetadata'],
23+
queryFn,
3424
retry: false,
35-
queryFn: () => getMetadata(auth.settings.metadataUrl),
3625
});
3726

3827
const auth = useAuth();

0 commit comments

Comments
 (0)