UNPKG

6.35 kBTypeScriptView Raw
1import {
2 APIGatewayAuthorizerResultContext,
3 APIGatewayEventDefaultAuthorizerContext,
4 APIGatewayEventRequestContextWithAuthorizer,
5} from "../common/api-gateway";
6import { Callback, Handler } from "../handler";
7
8export type APIGatewayAuthorizerHandler = Handler<APIGatewayAuthorizerEvent, APIGatewayAuthorizerResult>;
9export type APIGatewayAuthorizerWithContextHandler<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
10 Handler<APIGatewayAuthorizerEvent, APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
11
12export type APIGatewayAuthorizerCallback = Callback<APIGatewayAuthorizerResult>;
13export type APIGatewayAuthorizerWithContextCallback<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
14 Callback<APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
15
16export type APIGatewayTokenAuthorizerHandler =
17 Handler<APIGatewayTokenAuthorizerEvent, APIGatewayAuthorizerResult>;
18export type APIGatewayTokenAuthorizerWithContextHandler<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
19 Handler<APIGatewayTokenAuthorizerEvent, APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
20
21export type APIGatewayRequestAuthorizerHandler =
22 Handler<APIGatewayRequestAuthorizerEvent, APIGatewayAuthorizerResult>;
23export type APIGatewayRequestAuthorizerWithContextHandler<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
24 Handler<APIGatewayRequestAuthorizerEvent, APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
25
26export type APIGatewayAuthorizerEvent = APIGatewayTokenAuthorizerEvent | APIGatewayRequestAuthorizerEvent;
27
28export interface APIGatewayTokenAuthorizerEvent {
29 type: "TOKEN";
30 methodArn: string;
31 authorizationToken: string;
32}
33
34// Note, when invoked by the tester in the AWS web console, the map values can be null,
35// but they will be empty objects in the real object.
36// Worse, it will include "body" and "isBase64Encoded" properties, unlike the real call!
37export interface APIGatewayRequestAuthorizerEvent {
38 type: "REQUEST";
39 resource: string;
40 path: string;
41 httpMethod: string;
42 headers: { [name: string]: string } | null;
43 multiValueHeaders: { [name: string]: string[] } | null;
44 pathParameters: { [name: string]: string } | null;
45 queryStringParameters: { [name: string]: string } | null;
46 multiValueQueryStringParameters: { [name: string]: string[] } | null;
47 stageVariables: { [name: string]: string } | null;
48 requestContext: APIGatewayEventRequestContextWithAuthorizer<undefined>;
49 domainName: string;
50 apiId: string;
51}
52
53export interface APIGatewayAuthorizerResult {
54 principalId: string;
55 policyDocument: PolicyDocument;
56 context?: APIGatewayAuthorizerResultContext | null;
57 usageIdentifierKey?: string | null;
58}
59
60// Separate type so the context property is required, without pulling complex type magic.
61export interface APIGatewayAuthorizerWithContextResult<TAuthorizerContext extends APIGatewayAuthorizerResultContext> {
62 principalId: string;
63 policyDocument: PolicyDocument;
64 context: TAuthorizerContext;
65 usageIdentifierKey?: string | null;
66}
67
68// Legacy event / names
69
70/** @deprecated Use APIGatewayAuthorizerHandler or a subtype */
71export type CustomAuthorizerHandler = Handler<CustomAuthorizerEvent, APIGatewayAuthorizerResult>;
72
73// This one is actually fine.
74export type CustomAuthorizerCallback = APIGatewayAuthorizerCallback;
75
76/** @deprecated Use APIGatewayAuthorizerEvent or a subtype */
77export interface CustomAuthorizerEvent {
78 type: string;
79 methodArn: string;
80 authorizationToken?: string;
81 resource?: string;
82 path?: string;
83 httpMethod?: string;
84 headers?: { [name: string]: string };
85 multiValueHeaders?: { [name: string]: string[] };
86 pathParameters?: { [name: string]: string } | null;
87 queryStringParameters?: { [name: string]: string } | null;
88 multiValueQueryStringParameters?: { [name: string]: string[] } | null;
89 stageVariables?: { [name: string]: string };
90 requestContext?: APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventDefaultAuthorizerContext>;
91 domainName?: string;
92 apiId?: string;
93}
94
95export type CustomAuthorizerResult = APIGatewayAuthorizerResult;
96export type AuthResponse = APIGatewayAuthorizerResult;
97export type AuthResponseContext = APIGatewayAuthorizerResultContext;
98
99/**
100 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.
101 * https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
102 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition
103 */
104export interface PolicyDocument {
105 Version: string;
106 Id?: string;
107 Statement: Statement[];
108}
109
110/**
111 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Condition.
112 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
113 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
114 */
115export interface ConditionBlock {
116 [condition: string]: Condition | Condition[];
117}
118
119export interface Condition {
120 [key: string]: string | string[];
121}
122
123/**
124 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Statement.
125 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
126 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html
127 */
128export type Statement = BaseStatement & StatementAction & (StatementResource | StatementPrincipal);
129
130export interface BaseStatement {
131 Effect: string;
132 Sid?: string;
133 Condition?: ConditionBlock;
134}
135
136export type PrincipalValue = { [key: string]: string | string[] } | string | string[];
137export interface MaybeStatementPrincipal {
138 Principal?: PrincipalValue;
139 NotPrincipal?: PrincipalValue;
140}
141export interface MaybeStatementResource {
142 Resource?: string | string[];
143 NotResource?: string | string[];
144}
145export type StatementAction = { Action: string | string[] } | { NotAction: string | string[] };
146export type StatementResource = MaybeStatementPrincipal &
147 ({ Resource: string | string[] } | { NotResource: string | string[] });
148export type StatementPrincipal = MaybeStatementResource &
149 ({ Principal: PrincipalValue } | { NotPrincipal: PrincipalValue });