UNPKG

7.56 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
34export interface APIGatewayRequestAuthorizerEventHeaders {
35 [name: string]: string | undefined;
36}
37
38export interface APIGatewayRequestAuthorizerEventMultiValueHeaders {
39 [name: string]: string[] | undefined;
40}
41
42export interface APIGatewayRequestAuthorizerEventPathParameters {
43 [name: string]: string | undefined;
44}
45
46export interface APIGatewayRequestAuthorizerEventQueryStringParameters {
47 [name: string]: string | undefined;
48}
49
50export interface APIGatewayRequestAuthorizerEventMultiValueQueryStringParameters {
51 [name: string]: string[] | undefined;
52}
53
54export interface APIGatewayRequestAuthorizerEventStageVariables {
55 [name: string]: string | undefined;
56}
57
58// Note, when invoked by the tester in the AWS web console, the map values can be null,
59// but they will be empty objects in the real object.
60// Worse, it will include "body" and "isBase64Encoded" properties, unlike the real call!
61// See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html for the
62// formal definition.
63export interface APIGatewayRequestAuthorizerEvent {
64 type: "REQUEST";
65 methodArn: string;
66 resource: string;
67 path: string;
68 httpMethod: string;
69 headers: APIGatewayRequestAuthorizerEventHeaders | null;
70 multiValueHeaders: APIGatewayRequestAuthorizerEventMultiValueHeaders | null;
71 pathParameters: APIGatewayRequestAuthorizerEventPathParameters | null;
72 queryStringParameters: APIGatewayRequestAuthorizerEventQueryStringParameters | null;
73 multiValueQueryStringParameters: APIGatewayRequestAuthorizerEventMultiValueQueryStringParameters | null;
74 stageVariables: APIGatewayRequestAuthorizerEventStageVariables | null;
75 requestContext: APIGatewayEventRequestContextWithAuthorizer<undefined>;
76}
77
78export interface APIGatewayAuthorizerResult {
79 principalId: string;
80 policyDocument: PolicyDocument;
81 context?: APIGatewayAuthorizerResultContext | null | undefined;
82 usageIdentifierKey?: string | null | undefined;
83}
84
85// Separate type so the context property is required, without pulling complex type magic.
86export interface APIGatewayAuthorizerWithContextResult<TAuthorizerContext extends APIGatewayAuthorizerResultContext> {
87 principalId: string;
88 policyDocument: PolicyDocument;
89 context: TAuthorizerContext;
90 usageIdentifierKey?: string | null | undefined;
91}
92
93// Legacy event / names
94
95/** @deprecated Use APIGatewayAuthorizerHandler or a subtype */
96export type CustomAuthorizerHandler = Handler<CustomAuthorizerEvent, APIGatewayAuthorizerResult>;
97
98// This one is actually fine.
99export type CustomAuthorizerCallback = APIGatewayAuthorizerCallback;
100
101/** @deprecated Use APIGatewayAuthorizerEvent or a subtype */
102export interface CustomAuthorizerEvent {
103 type: string;
104 methodArn: string;
105 authorizationToken?: string | undefined;
106 resource?: string | undefined;
107 path?: string | undefined;
108 httpMethod?: string | undefined;
109 headers?: { [name: string]: string } | undefined;
110 multiValueHeaders?: { [name: string]: string[] } | undefined;
111 pathParameters?: { [name: string]: string } | null | undefined;
112 queryStringParameters?: { [name: string]: string } | null | undefined;
113 multiValueQueryStringParameters?: { [name: string]: string[] } | null | undefined;
114 stageVariables?: { [name: string]: string } | undefined;
115 requestContext?: APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventDefaultAuthorizerContext> | undefined;
116 domainName?: string | undefined;
117 apiId?: string | undefined;
118}
119
120export type CustomAuthorizerResult = APIGatewayAuthorizerResult;
121export type AuthResponse = APIGatewayAuthorizerResult;
122export type AuthResponseContext = APIGatewayAuthorizerResultContext;
123
124/**
125 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.
126 * https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
127 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition
128 */
129export interface PolicyDocument {
130 Version: string;
131 Id?: string | undefined;
132 Statement: Statement[];
133}
134
135/**
136 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Condition.
137 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
138 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
139 */
140export interface ConditionBlock {
141 [condition: string]: Condition | Condition[];
142}
143
144export interface Condition {
145 [key: string]: string | string[];
146}
147
148/**
149 * API Gateway CustomAuthorizer AuthResponse.PolicyDocument.Statement.
150 * https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-policy-language-overview.html
151 * https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html
152 */
153export type Statement = BaseStatement & StatementAction & (StatementResource | StatementPrincipal);
154
155export interface BaseStatement {
156 Effect: string;
157 Sid?: string | undefined;
158 Condition?: ConditionBlock | undefined;
159}
160
161export type PrincipalValue = { [key: string]: string | string[] } | string | string[];
162export interface MaybeStatementPrincipal {
163 Principal?: PrincipalValue | undefined;
164 NotPrincipal?: PrincipalValue | undefined;
165}
166export interface MaybeStatementResource {
167 Resource?: string | string[] | undefined;
168 NotResource?: string | string[] | undefined;
169}
170export type StatementAction = { Action: string | string[] } | { NotAction: string | string[] };
171export type StatementResource = MaybeStatementPrincipal &
172 ({ Resource: string | string[] } | { NotResource: string | string[] });
173export type StatementPrincipal = MaybeStatementResource &
174 ({ Principal: PrincipalValue } | { NotPrincipal: PrincipalValue });