mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-29 17:32:12 +00:00
2a3240da1c
## Description: - Validate that user tokens are accepted by the API server, in case of token revoked / remote logout. - Lookup user roles by their token. - Sets the groundwork for validating custom flag codes, patterns, etc. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { jwtVerify } from "jose";
|
|
import {
|
|
TokenPayload,
|
|
TokenPayloadSchema,
|
|
UserMeResponse,
|
|
UserMeResponseSchema,
|
|
} from "../core/ApiSchemas";
|
|
import { ServerConfig } from "../core/configuration/Config";
|
|
|
|
type TokenVerificationResult = {
|
|
persistentId: string;
|
|
claims: TokenPayload | null;
|
|
};
|
|
|
|
export async function verifyClientToken(
|
|
token: string,
|
|
config: ServerConfig,
|
|
): Promise<TokenVerificationResult> {
|
|
if (token.length === 36) {
|
|
return { persistentId: token, claims: null };
|
|
}
|
|
const issuer = config.jwtIssuer();
|
|
const audience = config.jwtAudience();
|
|
const key = await config.jwkPublicKey();
|
|
const { payload, protectedHeader } = await jwtVerify(token, key, {
|
|
algorithms: ["EdDSA"],
|
|
issuer,
|
|
audience,
|
|
maxTokenAge: "6 days",
|
|
});
|
|
const claims = TokenPayloadSchema.parse(payload);
|
|
const persistentId = claims.sub;
|
|
return { persistentId, claims };
|
|
}
|
|
|
|
export async function getUserMe(
|
|
token: string,
|
|
config: ServerConfig,
|
|
): Promise<UserMeResponse | false> {
|
|
try {
|
|
// Get the user object
|
|
const response = await fetch(config.jwtIssuer() + "/users/@me", {
|
|
headers: {
|
|
authorization: `Bearer ${token}`,
|
|
},
|
|
});
|
|
if (response.status !== 200) return false;
|
|
const body = await response.json();
|
|
const result = UserMeResponseSchema.safeParse(body);
|
|
if (!result.success) {
|
|
console.error(
|
|
"Invalid response",
|
|
JSON.stringify(body),
|
|
JSON.stringify(result.error),
|
|
);
|
|
return false;
|
|
}
|
|
return result.data;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|