MentraOS / cloud-v2 /packages /runtime /src /api /runtime-auth.ts
Anirudh1950's picture
Add files using upload-large-folder tool
d9e3e09 verified
Raw History Blame Contribute Delete
782 Bytes
import type { Context } from "hono";
import {
AccessTokenError,
verifyRuntimeToken,
type VerifiedAccessToken,
} from "@mentra/cloud-shared";
export async function authenticateRuntimeRequest(
c: Context,
): Promise<{ identity: VerifiedAccessToken } | { error: Response }> {
const authHeader = c.req.header("authorization");
const match = authHeader?.match(/^Bearer\s+(.+)$/i);
const token = match?.[1]?.trim();
if (!token) {
return {
error: c.json({ error: "missing or malformed Authorization" }, 401),
};
}
try {
return { identity: await verifyRuntimeToken(token) };
} catch (err) {
if (err instanceof AccessTokenError) {
return { error: c.json({ error: err.message }, 401) };
}
throw err;
}
}