diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index 8193109802c..cd9c90280a0 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -75,4 +75,13 @@ export { export { AuthError } from './errors/AuthError'; -export { fetchAuthSession } from '@aws-amplify/core'; +export { + fetchAuthSession, + decodeJWT, + CredentialsAndIdentityIdProvider, + GetCredentialsOptions, + CredentialsAndIdentityId, + TokenProvider, + AuthTokens, + JWT, +} from '@aws-amplify/core'; diff --git a/packages/auth/src/providers/cognito/credentialsProvider/credentialsProvider.ts b/packages/auth/src/providers/cognito/credentialsProvider/credentialsProvider.ts index e4daf73d1c7..b394ebd883e 100644 --- a/packages/auth/src/providers/cognito/credentialsProvider/credentialsProvider.ts +++ b/packages/auth/src/providers/cognito/credentialsProvider/credentialsProvider.ts @@ -4,8 +4,8 @@ import { cognitoIdentityIdProvider } from './IdentityIdProvider'; import { AuthTokens, - AWSCredentialsAndIdentityIdProvider, - AWSCredentialsAndIdentityId, + CredentialsAndIdentityIdProvider, + CredentialsAndIdentityId, getCredentialsForIdentity, GetCredentialsOptions, ConsoleLogger, @@ -23,7 +23,7 @@ import { assertIdTokenInAuthTokens } from '../utils/types'; const logger = new ConsoleLogger('CognitoCredentialsProvider'); const CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future export class CognitoAWSCredentialsAndIdentityIdProvider - implements AWSCredentialsAndIdentityIdProvider + implements CredentialsAndIdentityIdProvider { constructor(identityIdStore: IdentityIdStore) { this._identityIdStore = identityIdStore; @@ -31,7 +31,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider private _identityIdStore: IdentityIdStore; - private _credentialsAndIdentityId?: AWSCredentialsAndIdentityId & { + private _credentialsAndIdentityId?: CredentialsAndIdentityId & { isAuthenticatedCreds: boolean; associatedIdToken?: string; }; @@ -50,7 +50,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider async getCredentialsAndIdentityId( getCredentialsOptions: GetCredentialsOptions - ): Promise { + ): Promise { const isAuthenticated = getCredentialsOptions.authenticated; const tokens = getCredentialsOptions.tokens; const authConfig = getCredentialsOptions.authConfig; @@ -89,7 +89,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider private async getGuestCredentials( identityId: string, authConfig: CognitoIdentityPoolConfig - ): Promise { + ): Promise { // Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials if ( this._credentialsAndIdentityId && @@ -124,7 +124,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider clientResult.Credentials.SecretKey ) { this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL; - const res: AWSCredentialsAndIdentityId = { + const res: CredentialsAndIdentityId = { credentials: { accessKeyId: clientResult.Credentials.AccessKeyId, secretAccessKey: clientResult.Credentials.SecretKey, @@ -159,7 +159,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider authConfig: CognitoIdentityPoolConfig, authTokens: AuthTokens, identityId: string - ): Promise { + ): Promise { if ( this._credentialsAndIdentityId && !this.isPastTTL() && @@ -193,7 +193,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider clientResult.Credentials.AccessKeyId && clientResult.Credentials.SecretKey ) { - const res: AWSCredentialsAndIdentityId = { + const res: CredentialsAndIdentityId = { credentials: { accessKeyId: clientResult.Credentials.AccessKeyId, secretAccessKey: clientResult.Credentials.SecretKey, diff --git a/packages/aws-amplify/__tests__/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.test.ts b/packages/aws-amplify/__tests__/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.test.ts index 57130645d3d..0d3e2570fd3 100644 --- a/packages/aws-amplify/__tests__/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.test.ts +++ b/packages/aws-amplify/__tests__/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.test.ts @@ -6,7 +6,7 @@ import { DefaultIdentityIdStore, } from '@aws-amplify/auth/cognito'; import { - AWSCredentialsAndIdentityIdProvider, + CredentialsAndIdentityIdProvider, AuthConfig, KeyValueStorageInterface, } from '@aws-amplify/core'; diff --git a/packages/aws-amplify/__tests__/exports.test.ts b/packages/aws-amplify/__tests__/exports.test.ts index f9cad7933a5..d5f7208356e 100644 --- a/packages/aws-amplify/__tests__/exports.test.ts +++ b/packages/aws-amplify/__tests__/exports.test.ts @@ -195,6 +195,7 @@ describe('aws-amplify Exports', () => { "autoSignIn", "AuthError", "fetchAuthSession", + "decodeJWT", ] `); }); diff --git a/packages/aws-amplify/src/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.ts b/packages/aws-amplify/src/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.ts index 3ccf4dad2f6..521ab0bc072 100644 --- a/packages/aws-amplify/src/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.ts +++ b/packages/aws-amplify/src/adapterCore/authProvidersFactories/cognito/createAWSCredentialsAndIdentityIdProvider.ts @@ -6,7 +6,7 @@ import { DefaultIdentityIdStore, } from '@aws-amplify/auth/cognito'; import { - AWSCredentialsAndIdentityIdProvider, + CredentialsAndIdentityIdProvider, AuthConfig, KeyValueStorageInterface, } from '@aws-amplify/core'; @@ -21,7 +21,7 @@ import { export const createAWSCredentialsAndIdentityIdProvider = ( authConfig: AuthConfig, keyValueStorage: KeyValueStorageInterface -): AWSCredentialsAndIdentityIdProvider => { +): CredentialsAndIdentityIdProvider => { const credentialsProvider = new CognitoAWSCredentialsAndIdentityIdProvider( new DefaultIdentityIdStore(keyValueStorage) ); diff --git a/packages/core/__tests__/singleton/Singleton.test.ts b/packages/core/__tests__/singleton/Singleton.test.ts index a6a51f8c068..ad84565cc3e 100644 --- a/packages/core/__tests__/singleton/Singleton.test.ts +++ b/packages/core/__tests__/singleton/Singleton.test.ts @@ -1,7 +1,7 @@ import { Amplify } from '../../src/singleton'; import { AuthClass as Auth } from '../../src/singleton/Auth'; import { decodeJWT } from '../../src/singleton/Auth/utils'; -import { AWSCredentialsAndIdentityId } from '../../src/singleton/Auth/types'; +import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types'; import { TextEncoder, TextDecoder } from 'util'; import { fetchAuthSession } from '../../src'; Object.assign(global, { TextDecoder, TextEncoder }); @@ -246,7 +246,7 @@ describe('Session tests', () => { tokens, authConfig, identityId, - }): Promise => { + }): Promise => { return { credentials: { accessKeyId: 'accessKeyIdValue', @@ -341,7 +341,7 @@ describe('Session tests', () => { tokens, authConfig, identityId, - }): Promise => { + }): Promise => { return { credentials: { accessKeyId: 'accessKeyIdValue', diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 29587871944..05c753c7e6b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -13,12 +13,14 @@ export { TokenProvider, AuthTokens, FetchAuthSessionOptions, - AWSCredentialsAndIdentityIdProvider, - AWSCredentialsAndIdentityId, + CredentialsAndIdentityIdProvider, + CredentialsAndIdentityId, Identity, OAuthConfig, CognitoUserPoolConfig, + JWT, } from './singleton/Auth/types'; +export { decodeJWT } from './singleton/Auth/utils'; export { AuthConfig, AuthUserPoolConfig, diff --git a/packages/core/src/singleton/Auth/index.ts b/packages/core/src/singleton/Auth/index.ts index 66321ebb7d6..d43f508674c 100644 --- a/packages/core/src/singleton/Auth/index.ts +++ b/packages/core/src/singleton/Auth/index.ts @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { - AWSCredentialsAndIdentityId, + CredentialsAndIdentityId, AuthConfig, AuthSession, AuthTokens, @@ -48,7 +48,7 @@ export class AuthClass { options: FetchAuthSessionOptions = {} ): Promise { let tokens: AuthTokens | undefined; - let credentialsAndIdentityId: AWSCredentialsAndIdentityId | undefined; + let credentialsAndIdentityId: CredentialsAndIdentityId | undefined; let userSub: string | undefined; // Get tokens will throw if session cannot be refreshed (network or service error) or return null if not available diff --git a/packages/core/src/singleton/Auth/types.ts b/packages/core/src/singleton/Auth/types.ts index 479fd46f3b2..69d0e8fff7e 100644 --- a/packages/core/src/singleton/Auth/types.ts +++ b/packages/core/src/singleton/Auth/types.ts @@ -38,7 +38,7 @@ export type AuthSession = { export type LibraryAuthOptions = { tokenProvider?: TokenProvider; - credentialsProvider?: AWSCredentialsAndIdentityIdProvider; + credentialsProvider?: CredentialsAndIdentityIdProvider; }; export type Identity = { @@ -46,10 +46,10 @@ export type Identity = { type: 'guest' | 'primary'; }; -export interface AWSCredentialsAndIdentityIdProvider { +export interface CredentialsAndIdentityIdProvider { getCredentialsAndIdentityId: ( getCredentialsOptions: GetCredentialsOptions - ) => Promise; + ) => Promise; clearCredentialsAndIdentityId: () => void; } @@ -212,7 +212,7 @@ type GetCredentialsUnauthenticatedUser = { tokens?: never; }; -export type AWSCredentialsAndIdentityId = { +export type CredentialsAndIdentityId = { credentials: AWSCredentials; identityId?: string; };