UNPKG

5.42 kBPlain TextView Raw
1/*
2 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13
14import {
15 ICookieStorageData,
16 ICognitoStorage,
17} from 'amazon-cognito-identity-js';
18
19/**
20 * Parameters for user sign up
21 */
22export interface SignUpParams {
23 username: string;
24 password: string;
25 attributes?: object;
26 validationData?: { [key: string]: any };
27 clientMetadata?: { [key: string]: string };
28}
29
30export interface AuthCache {
31 setItem();
32 getItem();
33 removeItem();
34}
35
36/**
37 * Auth instance options
38 */
39export interface AuthOptions {
40 userPoolId?: string;
41 userPoolWebClientId?: string;
42 identityPoolId?: string;
43 region?: string;
44 mandatorySignIn?: boolean;
45 cookieStorage?: ICookieStorageData;
46 oauth?: OAuthOpts;
47 refreshHandlers?: object;
48 storage?: ICognitoStorage;
49 authenticationFlowType?: string;
50 identityPoolRegion?: string;
51 clientMetadata?: any;
52 endpoint?: string;
53}
54
55export enum CognitoHostedUIIdentityProvider {
56 Cognito = 'COGNITO',
57 Google = 'Google',
58 Facebook = 'Facebook',
59 Amazon = 'LoginWithAmazon',
60 Apple = 'SignInWithApple',
61}
62
63export type LegacyProvider =
64 | 'google'
65 | 'facebook'
66 | 'amazon'
67 | 'developer'
68 | string;
69
70export type FederatedSignInOptions = {
71 provider: CognitoHostedUIIdentityProvider;
72 customState?: string;
73};
74
75export type FederatedSignInOptionsCustom = {
76 customProvider: string;
77 customState?: string;
78};
79
80export function isFederatedSignInOptions(
81 obj: any
82): obj is FederatedSignInOptions {
83 const keys: (keyof FederatedSignInOptions)[] = ['provider'];
84 return obj && !!keys.find(k => obj.hasOwnProperty(k));
85}
86
87export function isFederatedSignInOptionsCustom(
88 obj: any
89): obj is FederatedSignInOptionsCustom {
90 const keys: (keyof FederatedSignInOptionsCustom)[] = ['customProvider'];
91 return obj && !!keys.find(k => obj.hasOwnProperty(k));
92}
93
94export function hasCustomState(obj: any): boolean {
95 const keys: (keyof (
96 | FederatedSignInOptions
97 | FederatedSignInOptionsCustom
98 ))[] = ['customState'];
99 return obj && !!keys.find(k => obj.hasOwnProperty(k));
100}
101
102/**
103 * Details for multi-factor authentication
104 */
105export interface MfaRequiredDetails {
106 challengeName: any;
107 challengeParameters: any;
108}
109
110/**
111 * interface for federatedResponse
112 */
113export interface FederatedResponse {
114 // access token
115 token: string;
116 // identity id
117 identity_id?: string;
118 // the universal time when token expired
119 expires_at: number;
120}
121
122/**
123 * interface for federatedUser
124 */
125export interface FederatedUser {
126 name: string;
127 email?: string;
128 picture?: string;
129}
130
131export interface AwsCognitoOAuthOpts {
132 domain: string;
133 scope: Array<string>;
134 redirectSignIn: string;
135 redirectSignOut: string;
136 responseType: string;
137 options?: object;
138 urlOpener?: (url: string, redirectUrl: string) => Promise<any>;
139}
140
141export function isCognitoHostedOpts(
142 oauth: OAuthOpts
143): oauth is AwsCognitoOAuthOpts {
144 return (<AwsCognitoOAuthOpts>oauth).redirectSignIn !== undefined;
145}
146
147export interface Auth0OAuthOpts {
148 domain: string;
149 clientID: string;
150 scope: string;
151 redirectUri: string;
152 audience: string;
153 responseType: string;
154 returnTo: string;
155 urlOpener?: (url: string, redirectUrl: string) => Promise<any>;
156}
157
158// Replacing to fix typings
159// export interface OAuth {
160// awsCognito?: awsCognitoOAuthOpts,
161// auth0?: any
162// }
163
164export type OAuthOpts = AwsCognitoOAuthOpts | Auth0OAuthOpts;
165
166export interface ConfirmSignUpOptions {
167 forceAliasCreation?: boolean;
168 clientMetadata?: ClientMetaData;
169}
170
171export interface SignOutOpts {
172 global?: boolean;
173}
174
175export interface CurrentUserOpts {
176 bypassCache: boolean;
177}
178
179export interface GetPreferredMFAOpts {
180 bypassCache: boolean;
181}
182
183export type UsernamePasswordOpts = {
184 username: string;
185 password: string;
186 validationData?: { [key: string]: any };
187};
188
189export enum AuthErrorTypes {
190 NoConfig = 'noConfig',
191 MissingAuthConfig = 'missingAuthConfig',
192 EmptyUsername = 'emptyUsername',
193 InvalidUsername = 'invalidUsername',
194 EmptyPassword = 'emptyPassword',
195 EmptyCode = 'emptyCode',
196 SignUpError = 'signUpError',
197 NoMFA = 'noMFA',
198 InvalidMFA = 'invalidMFA',
199 EmptyChallengeResponse = 'emptyChallengeResponse',
200 NoUserSession = 'noUserSession',
201 Default = 'default',
202 DeviceConfig = 'deviceConfig',
203 NetworkError = 'networkError',
204}
205
206export type AuthErrorMessages = { [key in AuthErrorTypes]: AuthErrorMessage };
207
208export interface AuthErrorMessage {
209 message: string;
210 log?: string;
211}
212
213// We can extend this in the future if needed
214export type SignInOpts = UsernamePasswordOpts;
215
216export type ClientMetaData =
217 | {
218 [key: string]: string;
219 }
220 | undefined;
221
222export function isUsernamePasswordOpts(obj: any): obj is UsernamePasswordOpts {
223 return !!(obj as UsernamePasswordOpts).username;
224}
225
226export interface IAuthDevice {
227 id: string;
228 name: string;
229}
230
231export enum GRAPHQL_AUTH_MODE {
232 API_KEY = 'API_KEY',
233 AWS_IAM = 'AWS_IAM',
234 OPENID_CONNECT = 'OPENID_CONNECT',
235 AMAZON_COGNITO_USER_POOLS = 'AMAZON_COGNITO_USER_POOLS',
236 AWS_LAMBDA = 'AWS_LAMBDA',
237}