File size: 1,101 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { AuthSubject, RouteClass, RouteClassification } from "./types";

export interface AuthDecision {
  allow: true;
  subject: AuthSubject;
}

export interface AuthRejection {
  allow: false;
  status: number;
  code: string;
  message: string;
}

export type AuthOutcome = AuthDecision | AuthRejection;

export interface RequestLike {
  method: string;
  headers: Headers;
  cookies?: { get?: (name: string) => { value?: string } | undefined };
  nextUrl?: { pathname?: string | null } | null;
  url?: string;
  ip?: string;
  socket?: { remoteAddress?: string };
}

export interface PolicyContext {
  request: RequestLike;
  classification: RouteClassification;
  requestId: string;
}

export interface RoutePolicy {
  readonly routeClass: RouteClass;
  evaluate(ctx: PolicyContext): Promise<AuthOutcome>;
}

export function allow(subject: AuthSubject): AuthDecision {
  return { allow: true, subject };
}

export function reject(status: number, code: string, message: string): AuthRejection {
  return { allow: false, status, code, message };
}