Skip to content

Commit

Permalink
fix(auth): vend the helpful types for custom providers (#12497)
Browse files Browse the repository at this point in the history
* fix: vend the required types for custom providers

* chore: expose JWT related types and method

* update snapshot

* rename types to not include AWS prefix

---------

Co-authored-by: Aaron S <[email protected]>
  • Loading branch information
Samaritan1011001 and stocaaro authored Nov 7, 2023
1 parent 7dde343 commit 875e28a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 24 deletions.
11 changes: 10 additions & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import { cognitoIdentityIdProvider } from './IdentityIdProvider';
import {
AuthTokens,
AWSCredentialsAndIdentityIdProvider,
AWSCredentialsAndIdentityId,
CredentialsAndIdentityIdProvider,
CredentialsAndIdentityId,
getCredentialsForIdentity,
GetCredentialsOptions,
ConsoleLogger,
Expand All @@ -23,15 +23,15 @@ 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;
}

private _identityIdStore: IdentityIdStore;

private _credentialsAndIdentityId?: AWSCredentialsAndIdentityId & {
private _credentialsAndIdentityId?: CredentialsAndIdentityId & {
isAuthenticatedCreds: boolean;
associatedIdToken?: string;
};
Expand All @@ -50,7 +50,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider

async getCredentialsAndIdentityId(
getCredentialsOptions: GetCredentialsOptions
): Promise<AWSCredentialsAndIdentityId | undefined> {
): Promise<CredentialsAndIdentityId | undefined> {
const isAuthenticated = getCredentialsOptions.authenticated;
const tokens = getCredentialsOptions.tokens;
const authConfig = getCredentialsOptions.authConfig;
Expand Down Expand Up @@ -89,7 +89,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
private async getGuestCredentials(
identityId: string,
authConfig: CognitoIdentityPoolConfig
): Promise<AWSCredentialsAndIdentityId> {
): Promise<CredentialsAndIdentityId> {
// Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials
if (
this._credentialsAndIdentityId &&
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -159,7 +159,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
authConfig: CognitoIdentityPoolConfig,
authTokens: AuthTokens,
identityId: string
): Promise<AWSCredentialsAndIdentityId> {
): Promise<CredentialsAndIdentityId> {
if (
this._credentialsAndIdentityId &&
!this.isPastTTL() &&
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DefaultIdentityIdStore,
} from '@aws-amplify/auth/cognito';
import {
AWSCredentialsAndIdentityIdProvider,
CredentialsAndIdentityIdProvider,
AuthConfig,
KeyValueStorageInterface,
} from '@aws-amplify/core';
Expand Down
1 change: 1 addition & 0 deletions packages/aws-amplify/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe('aws-amplify Exports', () => {
"autoSignIn",
"AuthError",
"fetchAuthSession",
"decodeJWT",
]
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DefaultIdentityIdStore,
} from '@aws-amplify/auth/cognito';
import {
AWSCredentialsAndIdentityIdProvider,
CredentialsAndIdentityIdProvider,
AuthConfig,
KeyValueStorageInterface,
} from '@aws-amplify/core';
Expand All @@ -21,7 +21,7 @@ import {
export const createAWSCredentialsAndIdentityIdProvider = (
authConfig: AuthConfig,
keyValueStorage: KeyValueStorageInterface
): AWSCredentialsAndIdentityIdProvider => {
): CredentialsAndIdentityIdProvider => {
const credentialsProvider = new CognitoAWSCredentialsAndIdentityIdProvider(
new DefaultIdentityIdStore(keyValueStorage)
);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down Expand Up @@ -246,7 +246,7 @@ describe('Session tests', () => {
tokens,
authConfig,
identityId,
}): Promise<AWSCredentialsAndIdentityId> => {
}): Promise<CredentialsAndIdentityId> => {
return {
credentials: {
accessKeyId: 'accessKeyIdValue',
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('Session tests', () => {
tokens,
authConfig,
identityId,
}): Promise<AWSCredentialsAndIdentityId> => {
}): Promise<CredentialsAndIdentityId> => {
return {
credentials: {
accessKeyId: 'accessKeyIdValue',
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/singleton/Auth/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -48,7 +48,7 @@ export class AuthClass {
options: FetchAuthSessionOptions = {}
): Promise<AuthSession> {
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
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ export type AuthSession = {

export type LibraryAuthOptions = {
tokenProvider?: TokenProvider;
credentialsProvider?: AWSCredentialsAndIdentityIdProvider;
credentialsProvider?: CredentialsAndIdentityIdProvider;
};

export type Identity = {
id: string;
type: 'guest' | 'primary';
};

export interface AWSCredentialsAndIdentityIdProvider {
export interface CredentialsAndIdentityIdProvider {
getCredentialsAndIdentityId: (
getCredentialsOptions: GetCredentialsOptions
) => Promise<AWSCredentialsAndIdentityId | undefined>;
) => Promise<CredentialsAndIdentityId | undefined>;
clearCredentialsAndIdentityId: () => void;
}

Expand Down Expand Up @@ -212,7 +212,7 @@ type GetCredentialsUnauthenticatedUser = {
tokens?: never;
};

export type AWSCredentialsAndIdentityId = {
export type CredentialsAndIdentityId = {
credentials: AWSCredentials;
identityId?: string;
};
Expand Down

0 comments on commit 875e28a

Please sign in to comment.