import { noop } from "../util"; import { callCognito } from "../platform"; export type TClientMetadata = Record; export type TUserAttribute = { Name: string; Value: string }; export type TMfaPreferenceSettings = { Enabled: boolean; PreferredMfa: boolean; }; export type TWebAuthnMfaPreferenceSettings = { Enabled: boolean; }; const identityActions = [ 'GetId', 'GetCredentialsForIdentity' ] export const cognitoFetch = async ({ operation, region, args, debug = noop, }: TCognitoFetchArgs & { region: string }) => { const isIdentityOp = identityActions.includes(operation) const service = (isIdentityOp) ? 'AWSCognitoIdentityService' : 'AWSCognitoIdentityProviderService' const baseService = (isIdentityOp) ? 'cognito-identity' : 'cognito-idp' const endpoint = `https://${baseService}.${region}.amazonaws.com/`; const headers = { "Content-Type": "application/x-amz-json-1.1", "X-Amz-Target": `${service}.${operation}`, "X-Amz-User-Agent": "amazon", }; debug(`cognitoFetch called ${operation} ${region} ${endpoint}`); debug({ headers }); debug({ args }); return await callCognito({ endpoint, body: JSON.stringify(args), headers, debug, }); }; type TInitiateAuthParams = | { AuthFlow: "USER_SRP_AUTH"; AuthParameters: { USERNAME: string; SRP_A: string; DEVICE_KEY: string | undefined; }; } | { AuthFlow: "REFRESH_TOKEN"; AuthParameters: { REFRESH_TOKEN: string; DEVICE_KEY: string | undefined; }; }; type TRespondToAuthChallengeParams = | { ChallengeName: "PASSWORD_VERIFIER"; ChallengeResponses: { USERNAME: string; TIMESTAMP: string; PASSWORD_CLAIM_SECRET_BLOCK: string; PASSWORD_CLAIM_SIGNATURE: string; DEVICE_KEY: string | undefined; }; } | { ChallengeName: "NEW_PASSWORD_REQUIRED"; ChallengeResponses: { USERNAME: string; NEW_PASSWORD: string; }; } | { ChallengeName: "DEVICE_SRP_AUTH"; ChallengeResponses: { USERNAME: string; SRP_A: string; DEVICE_KEY: string | undefined; }; } | { ChallengeName: "DEVICE_PASSWORD_VERIFIER"; ChallengeResponses: { USERNAME: string; TIMESTAMP: string; PASSWORD_CLAIM_SECRET_BLOCK: string; PASSWORD_CLAIM_SIGNATURE: string; DEVICE_KEY: string; }; } | { ChallengeName: "SOFTWARE_TOKEN_MFA"; ChallengeResponses: { USERNAME: string; SOFTWARE_TOKEN_MFA_CODE: string; DEVICE_KEY: string | undefined; }; } | { ChallengeName: "SMS_MFA"; ChallengeResponses: { USERNAME: string; SMS_MFA_CODE: string; DEVICE_KEY: string | undefined; }; }; type TCognitoFetchArgs = { debug?: (trace: any) => void } & ( | { operation: "InitiateAuth"; args: TInitiateAuthParams & { ClientId: string; ClientMetadata?: TClientMetadata }; } | { operation: "RespondToAuthChallenge"; args: TRespondToAuthChallengeParams & { ClientId: string; Session: string | undefined; ClientMetadata?: TClientMetadata; }; } | { operation: "ConfirmDevice"; args: { AccessToken: string; DeviceKey: string; DeviceName: string; DeviceSecretVerifierConfig: { Salt: string; PasswordVerifier: string; }; }; } | { operation: "UpdateDeviceStatus"; args: { AccessToken: string; DeviceKey: string; DeviceRememberedStatus: "not_remembered" | "remembered"; }; } | { operation: "GetUser"; args: { AccessToken: string; }; } | { operation: "ChangePassword"; args: { AccessToken: string; PreviousPassword?: string; ProposedPassword: string; }; } | { operation: "UpdateUserAttributes"; args: { AccessToken: string; UserAttributes: TUserAttribute[]; ClientMetadata?: TClientMetadata; }; } | { operation: "GetUserAttributeVerificationCode"; args: { AccessToken: string; AttributeName: string; ClientMetadata?: TClientMetadata; }; } | { operation: "VerifyUserAttribute"; args: { AccessToken: string; AttributeName: string; Code: string; }; } | { operation: "GlobalSignOut"; args: { AccessToken: string; }; } | { operation: "GetTokensFromRefreshToken"; args: { ClientId: string; RefreshToken: string; DeviceKey?: string; ClientSecret?: string; ClientMetadata?: TClientMetadata; }; } | { operation: "RevokeToken"; args: { ClientId: string; Token: string; ClientSecret?: string; }; } | { operation: "AssociateSoftwareToken"; args: | { AccessToken: string; Session?: never; } | { AccessToken?: never; Session: string; }; } | { operation: "VerifySoftwareToken"; args: | { AccessToken: string; Session?: never; UserCode: string; FriendlyDeviceName?: string; } | { AccessToken?: never; Session: string; UserCode: string; FriendlyDeviceName?: string; }; } | { operation: "SetUserMFAPreference"; args: { AccessToken: string; SMSMfaSettings?: TMfaPreferenceSettings; SoftwareTokenMfaSettings?: TMfaPreferenceSettings; EmailMfaSettings?: TMfaPreferenceSettings; WebAuthnMfaSettings?: TWebAuthnMfaPreferenceSettings; }; } | { operation: "ListDevices"; args: { AccessToken: string; }; } | { operation: "ForgetDevice"; args: { AccessToken: string; DeviceKey: string; }; } | { operation: "ForgotPassword"; args: { ClientId: string; Username: string; ClientMetadata?: TClientMetadata; }; } | { operation: "ConfirmForgotPassword"; args: { ClientId: string; Username: string; ConfirmationCode: string; Password: string; ClientMetadata?: TClientMetadata; }; } | { operation: "SignUp"; args: { ClientId: string; Username: string; Password: string; UserAttributes?: TUserAttribute[]; ClientMetadata?: TClientMetadata; }; } | { operation: "ConfirmSignUp"; args: { ClientId: string; Username: string; ConfirmationCode: string; ClientMetadata?: TClientMetadata; }; } | { operation: "ResendConfirmationCode"; args: { ClientId: string; Username: string; ClientMetadata?: TClientMetadata; }; } /* Identity Pool Actions */ | { operation: "GetId"; args: { IdentityPoolId: string; Logins?: Record; }; } | { operation: "GetCredentialsForIdentity"; args: { IdentityId: string; Logins?: Record; }; } );