File size: 782 Bytes
d9e3e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
  }
}