UNPKG

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