mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 19:16:39 +00:00
b56e380107
## Description: Enable the `sort-keys` eslint rule. Fixes #1629 ## 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 - [ ] I have read and accepted the CLA agreement (only required once).
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { jwtVerify } from "jose";
|
|
import { z } from "zod";
|
|
import {
|
|
TokenPayload,
|
|
TokenPayloadSchema,
|
|
UserMeResponse,
|
|
UserMeResponseSchema,
|
|
} from "../core/ApiSchemas";
|
|
import { ServerConfig } from "../core/configuration/Config";
|
|
import { PersistentIdSchema } from "../core/Schemas";
|
|
|
|
type TokenVerificationResult =
|
|
| {
|
|
persistentId: string;
|
|
claims: TokenPayload | null;
|
|
}
|
|
| false;
|
|
|
|
export async function verifyClientToken(
|
|
token: string,
|
|
config: ServerConfig,
|
|
): Promise<TokenVerificationResult> {
|
|
if (PersistentIdSchema.safeParse(token).success) {
|
|
// eslint-disable-next-line sort-keys
|
|
return { persistentId: token, claims: null };
|
|
}
|
|
try {
|
|
const issuer = config.jwtIssuer();
|
|
const audience = config.jwtAudience();
|
|
const key = await config.jwkPublicKey();
|
|
const { payload, protectedHeader } = await jwtVerify(token, key, {
|
|
algorithms: ["EdDSA"],
|
|
audience,
|
|
issuer,
|
|
});
|
|
const result = TokenPayloadSchema.safeParse(payload);
|
|
if (!result.success) {
|
|
const error = z.prettifyError(result.error);
|
|
console.warn("Error parsing token payload", error);
|
|
return false;
|
|
}
|
|
const claims = result.data;
|
|
const persistentId = claims.sub;
|
|
return { claims, persistentId };
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|