Skip to content

Commit 8923c9e

Browse files
committed
Update oidc-spa
1 parent 9fd365c commit 8923c9e

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@hono/node-server": "^1.11.1",
2525
"@hono/zod-openapi": "^0.13.0",
2626
"hono": "^4.11.1",
27-
"oidc-spa": "^8.7.7",
27+
"oidc-spa": "^8.7.10",
2828
"tsafe": "^1.8.12",
2929
"url-join": "^5.0.0",
3030
"zod": "^3.23.8"

src/auth.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { oidcSpa } from "oidc-spa/server";
1+
import { oidcSpa, extractRequestAuthContext } from "oidc-spa/server";
22
import { z } from "zod";
33
import { HTTPException } from "hono/http-exception";
44
import type { HonoRequest } from "hono";
@@ -9,7 +9,7 @@ const { bootstrapAuth, validateAndDecodeAccessToken } = oidcSpa
99
sub: z.string(),
1010
realm_access: z.object({
1111
roles: z.array(z.string())
12-
})
12+
}).optional()
1313
})
1414
})
1515
.createUtils();
@@ -24,39 +24,46 @@ export async function getUser(
2424
req: HonoRequest,
2525
requiredRole?: "realm-admin" | "support-staff"
2626
): Promise<User> {
27+
const requestAuthContext = extractRequestAuthContext({
28+
request: req,
29+
trustProxy: true
30+
});
2731

28-
const { isSuccess, errorCause, debugErrorMessage, decodedAccessToken } =
29-
await validateAndDecodeAccessToken({
30-
request: {
31-
url: req.url,
32-
method: req.method,
33-
getHeaderValue: headerName => req.header(headerName)
34-
}
35-
});
32+
if( !requestAuthContext ){
33+
// Demo shortcut: we return 401 on missing Authorization, but a mixed
34+
// public/private endpoint could instead return undefined here and let
35+
// the caller decide whether to process an anonymous request.
36+
console.warn("Anonymous request");
37+
throw new HTTPException(401); // Unauthorized
38+
}
3639

37-
if (!isSuccess) {
40+
if (!requestAuthContext.isWellFormed) {
41+
console.warn(requestAuthContext.debugErrorMessage);
42+
throw new HTTPException(400); // Bad Request
43+
}
3844

39-
if( errorCause === "missing Authorization header" ){
40-
// Demo shortcut: we return 401 on missing Authorization, but a mixed
41-
// public/private endpoint could instead return undefined here and let
42-
// the caller decide whether to process an anonymous request.
43-
console.warn("Anonymous request");
44-
}else{
45-
console.warn(debugErrorMessage);
46-
}
45+
const { isSuccess, debugErrorMessage, decodedAccessToken } =
46+
await validateAndDecodeAccessToken(
47+
requestAuthContext.accessTokenAndMetadata
48+
);
4749

48-
throw new HTTPException(401);
50+
if (!isSuccess) {
51+
console.warn(debugErrorMessage);
52+
throw new HTTPException(401); // Unauthorized
4953
}
5054

51-
if (
52-
requiredRole !== undefined &&
53-
!decodedAccessToken.realm_access.roles.includes(requiredRole)
54-
) {
55-
console.warn(`User missing role: ${requiredRole}`);
56-
throw new HTTPException(403);
55+
// Your custom Authorization logic: Grant per request access depending
56+
// on the access token claim.
57+
if (requiredRole) {
58+
if (!decodedAccessToken.realm_access?.roles.includes(requiredRole)) {
59+
console.warn(`User missing role: ${requiredRole}`);
60+
throw new HTTPException(403); // Forbidden
61+
}
5762
}
5863

59-
return {
64+
const user: User = {
6065
id: decodedAccessToken.sub
6166
};
67+
68+
return user;
6269
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ import { getUser, bootstrapAuth } from "./auth";
242242

243243
app.openapi(route, async c => {
244244
const user = await getUser(c.req);
245-
245+
246246
const { id } = c.req.valid("param");
247247

248248
getUserTodoStore(user.id).remove(id);

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ minimist@^1.2.6:
245245
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
246246
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
247247

248-
oidc-spa@^8.7.7:
249-
version "8.7.7"
250-
resolved "https://registry.yarnpkg.com/oidc-spa/-/oidc-spa-8.7.7.tgz#90438888772c2e1607391af378d8a597aeac3898"
251-
integrity sha512-iz+P2Lv9Xqx3oLKGSKToal5j0+nV/CQju8ZN9ZOJaNnwzFzvMJoj3u1AHt/tBWnfGAa3XvkFenmYsAspaVNqJw==
248+
oidc-spa@^8.7.10:
249+
version "8.7.10"
250+
resolved "https://registry.yarnpkg.com/oidc-spa/-/oidc-spa-8.7.10.tgz#ff2ca92f13526bddbe681d4ed68f28e0b2fbddcd"
251+
integrity sha512-OyR9vuQ8MXOX6qFRmcXrO9cc9oLkQBFfNhQYj6+XcjjG/2i0cjWdi9CSKFxFHFEPhbyDXbVT5Nc9+gsialUgTg==
252252

253253
openapi3-ts@^4.1.2:
254254
version "4.3.1"

0 commit comments

Comments
 (0)