UNPKG

9.9 kBTypeScriptView Raw
1import {
2 APIGatewayAuthorizerResultContext,
3 APIGatewayEventDefaultAuthorizerContext,
4 APIGatewayEventRequestContextWithAuthorizer,
5} from '../common/api-gateway';
6import { Callback, Handler } from '../handler';
7import { APIGatewayEventRequestContextV2 } from './api-gateway-proxy';
8
9export type APIGatewayAuthorizerHandler = Handler<APIGatewayAuthorizerEvent, APIGatewayAuthorizerResult>;
10export type APIGatewayAuthorizerWithContextHandler<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
11 Handler<APIGatewayAuthorizerEvent, APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
12
13export type APIGatewayAuthorizerCallback = Callback<APIGatewayAuthorizerResult>;
14export type APIGatewayAuthorizerWithContextCallback<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
15 Callback<APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
16
17export type APIGatewayTokenAuthorizerHandler = Handler<APIGatewayTokenAuthorizerEvent, APIGatewayAuthorizerResult>;
18export type APIGatewayTokenAuthorizerWithContextHandler<TAuthorizerContext extends APIGatewayAuthorizerResultContext> =
19 Handler<APIGatewayTokenAuthorizerEvent, APIGatewayAuthorizerWithContextResult<TAuthorizerContext>>;
20
21export type APIGatewayRequestAuthorizerHandler = Handler<APIGatewayRequestAuthorizerEvent, APIGatewayAuthorizerResult>;
22export type APIGatewayRequestAuthorizerWithContextHandler<
23 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
34export interface APIGatewayRequestAuthorizerEventV2 {
35 version: string;
36 type: 'REQUEST';
37 routeArn: string;
38 identitySource: string[];
39 routeKey: string;
40 rawPath: string;
41 rawQueryString: string;
42 cookies: string[];
43 headers?: APIGatewayRequestAuthorizerEventHeaders;
44 queryStringParameters?: APIGatewayRequestAuthorizerEventQueryStringParameters;
45 requestContext: APIGatewayEventRequestContextV2;
46 pathParameters?: APIGatewayRequestAuthorizerEventPathParameters;
47 stageVariables?: APIGatewayRequestAuthorizerEventStageVariables;
48}
49
50export interface APIGatewayRequestAuthorizerEventHeaders {
51 [name: string]: string | undefined;
52}
53
54export interface APIGatewayRequestAuthorizerEventMultiValueHeaders {
55 [name: string]: string[] | undefined;
56}
57
58export interface APIGatewayRequestAuthorizerEventPathParameters {
59 [name: string]: string | undefined;
60}
61
62export interface APIGatewayRequestAuthorizerEventQueryStringParameters {
63 [name: string]: string | undefined;
64}
65
66export interface APIGatewayRequestAuthorizerEventMultiValueQueryStringParameters {
67 [name: string]: string[] | undefined;
68}
69
70export interface APIGatewayRequestAuthorizerEventStageVariables {
71 [name: string]: string | undefined;
72}
73
74// Note, when invoked by the tester in the AWS web console, the map values can be null,
75// but they will be empty objects in the real object.
76// Worse, it will include "body" and "isBase64Encoded" properties, unlike the real call!
77// See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html for the
78// formal definition.
79export interface APIGatewayRequestAuthorizerEvent {
80 type: 'REQUEST';
81 methodArn: string;
82 resource: string;
83 path: string;
84 httpMethod: string;
85 headers: APIGatewayRequestAuthorizerEventHeaders | null;
86 multiValueHeaders: APIGatewayRequestAuthorizerEventMultiValueHeaders | null;
87 pathParameters: APIGatewayRequestAuthorizerEventPathParameters | null;
88 queryStringParameters: APIGatewayRequestAuthorizerEventQueryStringParameters | null;
89 multiValueQueryStringParameters: APIGatewayRequestAuthorizerEventMultiValueQueryStringParameters | null;
90 stageVariables: APIGatewayRequestAuthorizerEventStageVariables | null;
91 requestContext: APIGatewayEventRequestContextWithAuthorizer<undefined>;
92}
93
94export interface APIGatewayAuthorizerResult {
95 principalId: string;
96 policyDocument: PolicyDocument;
97 context?: APIGatewayAuthorizerResultContext | null | undefined;
98 usageIdentifierKey?: string | null | undefined;
99}
100
101// Separate type so the context property is required, without pulling complex type magic.
102export interface APIGatewayAuthorizerWithContextResult<TAuthorizerContext extends APIGatewayAuthorizerResultContext> {
103 principalId: string;
104 policyDocument: PolicyDocument;
105 context: TAuthorizerContext;
106 usageIdentifierKey?: string | null | undefined;
107}
108
109/**
110 * IAM Authorizer Types
111 */
112export interface APIGatewayIAMAuthorizerResult {
113 principalId: string;
114 policyDocument: PolicyDocument;
115 context?: APIGatewayAuthorizerResultContext | null | undefined;
116 usageIdentifierKey?: string | null | undefined;
117}
118
119export interface APIGatewayIAMAuthorizerWithContextResult<
120 TAuthorizerContext extends APIGatewayAuthorizerResultContext,
121> {
122 principalId: string;
123 policyDocument: PolicyDocument;
124 context: TAuthorizerContext;
125 usageIdentifierKey?: string | null | undefined;
126}
127
128export type APIGatewayRequestIAMAuthorizerHandlerV2 = Handler<
129 APIGatewayRequestAuthorizerEventV2,
130 APIGatewayIAMAuthorizerResult
131>;
132
133export type APIGatewayRequestIAMAuthorizerV2WithContextHandler<
134 TAuthorizerContext extends APIGatewayAuthorizerResultContext,
135> = Handler<APIGatewayRequestAuthorizerEventV2, APIGatewayIAMAuthorizerWithContextResult<TAuthorizerContext>>;
136
137/**
138 * Simple Lambda Authorizer Types V2 spec with simple response
139 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
140 */
141export interface APIGatewaySimpleAuthorizerResult {
142 isAuthorized: boolean;
143}
144
145export interface APIGatewaySimpleAuthorizerWithContextResult<TAuthorizerContext>
146 extends APIGatewaySimpleAuthorizerResult {
147 context: TAuthorizerContext;
148}
149
150export type APIGatewayRequestSimpleAuthorizerHandlerV2 = Handler<
151 APIGatewayRequestAuthorizerEventV2,
152 APIGatewaySimpleAuthorizerResult
153>;
154
155export type APIGatewayRequestSimpleAuthorizerHandlerV2WithContext<TAuthorizerContext> = Handler<
156 APIGatewayRequestAuthorizerEventV2,
157 APIGatewaySimpleAuthorizerWithContextResult<TAuthorizerContext>
158>;
159
160// Legacy event / names
161
162/** @deprecated Use APIGatewayAuthorizerHandler or a subtype */
163export type CustomAuthorizerHandler = Handler<CustomAuthorizerEvent, APIGatewayAuthorizerResult>;
164
165// This one is actually fine.
166export type CustomAuthorizerCallback = APIGatewayAuthorizerCallback;
167
168/** @deprecated Use APIGatewayAuthorizerEvent or a subtype */
169export interface CustomAuthorizerEvent {
170 type: string;
171 methodArn: string;
172 authorizationToken?: string | undefined;
173 resource?: string | undefined;
174 path?: string | undefined;
175 httpMethod?: string | undefined;
176 headers?: { [name: string]: string } | undefined;
177 multiValueHeaders?: { [name: string]: string[] } | undefined;
178 pathParameters?: { [name: string]: string } | null | undefined;
179 queryStringParameters?: { [name: string]: string } | null | undefined;
180 multiValueQueryStringParameters?: { [name: string]: string[] } | null | undefined;
181 stageVariables?: { [name: string]: string } | undefined;
182 requestContext?: APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventDefaultAuthorizerContext> | undefined;
183 domainName?: string | undefined;
184 apiId?: string | undefined;
185}
186
187export type CustomAuthorizerResult = APIGatewayAuthorizerResult;
188export type AuthResponse = APIGatewayAuthorizerResult;
189export type AuthResponseContext = APIGatewayAuthorizerResultContext;
190
191/**
192 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.
193 * https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
194 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition
195 */
196export interface PolicyDocument {
197 Version: string;
198 Id?: string | undefined;
199 Statement: Statement[];
200}
201
202/**
203 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Condition.
204 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
205 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
206 */
207export interface ConditionBlock {
208 [condition: string]: Condition | Condition[];
209}
210
211export interface Condition {
212 [key: string]: string | string[];
213}
214
215/**
216 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Statement.
217 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
218 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html
219 */
220export type Statement = BaseStatement & StatementAction & (StatementResource | StatementPrincipal);
221
222export interface BaseStatement {
223 Effect: string;
224 Sid?: string | undefined;
225 Condition?: ConditionBlock | undefined;
226}
227
228export type PrincipalValue = { [key: string]: string | string[] } | string | string[];
229export interface MaybeStatementPrincipal {
230 Principal?: PrincipalValue | undefined;
231 NotPrincipal?: PrincipalValue | undefined;
232}
233export interface MaybeStatementResource {
234 Resource?: string | string[] | undefined;
235 NotResource?: string | string[] | undefined;
236}
237export type StatementAction = { Action: string | string[] } | { NotAction: string | string[] };
238export type StatementResource = MaybeStatementPrincipal &
239 ({ Resource: string | string[] } | { NotResource: string | string[] });
240export type StatementPrincipal = MaybeStatementResource &
241 ({ Principal: PrincipalValue } | { NotPrincipal: PrincipalValue });