Skip to content

Commit

Permalink
Use token introspection endpoint instead of userinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
gf-rog committed May 15, 2024
1 parent 4b26923 commit b8f852b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions backend/src/kcAdminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import KeycloakAdminClient from "@keycloak/keycloak-admin-client";

export const keycloakUri = process.env.KEYCLOAK_URI || "http://localhost:3000";
export const keycloakIssuer = process.env.KEYCLOAK_ISSUER || keycloakUri;
export const keycloakCredentials = Buffer.from(
`mercury-backend:${process.env.CLIENT_SECRET}`,
).toString("base64");

const kcAdminClient = new KeycloakAdminClient({
baseUrl: keycloakUri,
Expand Down
14 changes: 10 additions & 4 deletions backend/src/misc/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { AuthResponse, CustomResponse } from "../models/Response.js";
import DecodedData from "../models/DecodedData.js";
import Issuer from "../models/Issuer.js";
import TokenPayload from "../models/TokenPayload.js";
import { keycloakIssuer, keycloakUri } from "../kcAdminClient.js";
import {
keycloakCredentials,
keycloakIssuer,
keycloakUri,
} from "../kcAdminClient.js";

export interface JWTRequest extends Request {
token?: TokenPayload;
Expand Down Expand Up @@ -37,12 +41,14 @@ function tokenIssuerToName(issuer: string): Issuer | "unknown" {

export async function verifyKeycloakToken(tokenStr: string): Promise<boolean> {
const response = await fetch(
`${keycloakUri}/realms/mercury/protocol/openid-connect/userinfo`,
`${keycloakUri}/realms/mercury/protocol/openid-connect/token/introspect`,
{
method: "GET",
method: "POST",
headers: {
"Authorization": `Bearer ${tokenStr}`,
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Basic ${keycloakCredentials}`,
},
body: `token=${tokenStr}`,
},
);

Expand Down

0 comments on commit b8f852b

Please sign in to comment.