Skip to content

Commit

Permalink
Merge pull request #5 from ashwinkumar6/next/release-feat
Browse files Browse the repository at this point in the history
Next/release feat
  • Loading branch information
ashwinkumar6 authored Sep 12, 2023
2 parents a7e0a38 + f0dbcf3 commit 863576f
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 33 deletions.
7 changes: 7 additions & 0 deletions packages/auth/cognito/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@aws-amplify/auth/cognito/server",
"main": "../../lib/providers/cognito/apis/server/index.js",
"browser": "../../lib-esm/providers/cognito/apis/server/index.js",
"module": "../../lib-esm/providers/cognito/apis/server/index.js",
"typings": "../../lib-esm/providers/cognito/apis/server/index.d.ts"
}
18 changes: 17 additions & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
],
"cognito": [
"./lib-esm/providers/cognito/index.d.ts"
],
"cognito/server": [
"./lib-esm/providers/cognito/apis/server/index.d.ts"
],
"server": [
"./lib-esm/server.d.ts"
],
"internals": [
"./lib-esm/lib-esm/internals/index.d.ts"
]
}
},
Expand All @@ -48,6 +57,11 @@
"import": "./lib-esm/providers/cognito/index.js",
"require": "./lib/providers/cognito/index.js"
},
"./cognito/server": {
"types": "./lib-esm/providers/cognito/apis/server/index.d.ts",
"import": "./lib-esm/providers/cognito/apis/server/index.js",
"require": "./lib/providers/cognito/apis/server/index.js"
},
"./server": {
"types": "./lib-esm/server.d.ts",
"import": "./lib-esm/server.js",
Expand All @@ -74,7 +88,9 @@
"lib",
"lib-esm",
"src",
"internals"
"internals",
"cognito",
"server"
],
"dependencies": {
"@smithy/util-base64": "2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/providers/cognito/apis/verifyTOTPSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { AuthValidationErrorCode } from '../../../errors/types/validation';
import { assertValidationError } from '../../../errors/utils/assertValidationError';
import { VerifyTOTPSetupRequest } from '../../../types/requests';
import { CogntioVerifyTOTPSetupOptions } from '../types/options';
import { CognitoVerifyTOTPSetupOptions } from '../types/options';
import { verifySoftwareToken } from '../utils/clients/CognitoIdentityProvider';
import { VerifySoftwareTokenException } from '../types/errors';
import { Amplify } from '@aws-amplify/core';
Expand All @@ -27,7 +27,7 @@ import { assertAuthTokens } from '../utils/types';
* @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
*/
export async function verifyTOTPSetup(
verifyTOTPSetupRequest: VerifyTOTPSetupRequest<CogntioVerifyTOTPSetupOptions>
verifyTOTPSetupRequest: VerifyTOTPSetupRequest<CognitoVerifyTOTPSetupOptions>
): Promise<void> {
const authConfig = Amplify.getConfig().Auth?.Cognito;
assertTokenProviderConfig(authConfig);
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/providers/cognito/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {
CognitoConfirmSignUpOptions,
CognitoConfirmSignInOptions,
CognitoUpdateUserAttributesOptions,
CogntioVerifyTOTPSetupOptions,
CognitoVerifyTOTPSetupOptions,
} from './options';

export { UpdateMFAPreferenceRequest } from './requests';
2 changes: 1 addition & 1 deletion packages/auth/src/providers/cognito/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type CognitoConfirmSignInOptions<
/**
* Options specific to a Cognito Verify TOTP Setup request.
*/
export type CogntioVerifyTOTPSetupOptions = {
export type CognitoVerifyTOTPSetupOptions = {
friendlyDeviceName?: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const mockKeyValueStorage: KeyValueStorageInterface = {
clear: jest.fn(),
};
const mockAuthConfig: AuthConfig = {
identityPoolId: '123',
userPoolId: 'abc',
userPoolWebClientId: 'def',
Cognito: {
identityPoolId: '123',
userPoolId: 'abc',
userPoolClientId: 'def',
},
};
const MockDefaultTokenStore = DefaultTokenStore as jest.Mock;
const MockTokenOrchestrator = TokenOrchestrator as jest.Mock;
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-amplify/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import * as exported from '../src';

describe('aws-amplify', () => {
Expand Down
169 changes: 169 additions & 0 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
ResourcesConfig,
Amplify as AmplifySingleton,
LocalStorage,
CookieStorage,
TokenProvider,
} from '@aws-amplify/core';
import {
CognitoUserPoolsTokenProvider,
cognitoCredentialsProvider,
} from '../src/auth/cognito';

import { Amplify } from '../src';

jest.mock('@aws-amplify/core', () => ({
...jest.requireActual('@aws-amplify/core'),
Amplify: {
configure: jest.fn(),
getConfig: jest.fn(),
},
LocalStorage: jest.fn(),
CookieStorage: jest.fn(),
}));

jest.mock('../src/auth/cognito', () => ({
CognitoUserPoolsTokenProvider: {
setAuthConfig: jest.fn(),
setKeyValueStorage: jest.fn(),
},
cognitoCredentialsProvider: jest.fn(),
}));

const mockCognitoUserPoolsTokenProviderSetAuthConfig =
CognitoUserPoolsTokenProvider.setAuthConfig as jest.Mock;
const mockCognitoUserPoolsTokenProviderSetKeyValueStorage =
CognitoUserPoolsTokenProvider.setKeyValueStorage as jest.Mock;
const mockAmplifySingletonConfigure = AmplifySingleton.configure as jest.Mock;
const mockAmplifySingletonGetConfig = AmplifySingleton.getConfig as jest.Mock;
const MockCookieStorage = CookieStorage as jest.Mock;

const mockResourceConfig: ResourcesConfig = {
Auth: {
Cognito: {
userPoolClientId: 'userPoolClientId',
userPoolId: 'userPoolId',
},
},
Storage: {
S3: {
bucket: 'bucket',
region: 'us-west-2',
},
},
};

describe('initSingleton (DefaultAmplify)', () => {
beforeEach(() => {
mockCognitoUserPoolsTokenProviderSetAuthConfig.mockReset();
mockCognitoUserPoolsTokenProviderSetKeyValueStorage.mockReset();
mockAmplifySingletonConfigure.mockReset();
mockAmplifySingletonGetConfig.mockReset();
});

describe('DefaultAmplify.configure()', () => {
describe('when ResourcesConfig.Auth is defined', () => {
describe('when libraryOptions.Auth is undefined', () => {
it('should invoke AmplifySingleton.configure with the default auth providers', () => {
Amplify.configure(mockResourceConfig);

expect(
mockCognitoUserPoolsTokenProviderSetAuthConfig
).toHaveBeenCalledWith(mockResourceConfig.Auth);

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
Auth: {
tokenProvider: CognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
},
}
);
});

it('should invoke AmplifySingleton.configure with other provided library options', () => {
const libraryOptionsWithStorage = {
Storage: {
S3: {
defaultAccessLevel: 'private',
isObjectLockEnabled: true,
},
},
};

Amplify.configure(mockResourceConfig, {
Storage: {
S3: {
defaultAccessLevel: 'private',
isObjectLockEnabled: true,
},
},
});

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
{
Auth: {
tokenProvider: CognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
},
...libraryOptionsWithStorage,
}
);
});

it('should use LocalStorage by default for the default CognitoUserPoolsTokenProvider', () => {
Amplify.configure(mockResourceConfig);

expect(
mockCognitoUserPoolsTokenProviderSetKeyValueStorage
).toHaveBeenCalledWith(LocalStorage);
});

it('should use cookie storage if LibraryOptions.ssr is set to true for the default CognitoUserPoolsTokenProvider', () => {
Amplify.configure(mockResourceConfig, { ssr: true });

expect(MockCookieStorage).toHaveBeenCalledWith({
sameSite: 'strict',
});
expect(
mockCognitoUserPoolsTokenProviderSetKeyValueStorage
).toHaveBeenCalledTimes(1);
});
});

describe('when libraryOptions.Auth is defined', () => {
it('should forward the libraryOptions to AmplifySingleton.configure', () => {
const mockTokenProvider: TokenProvider = {
getTokens: jest.fn(),
};
const mockLibraryOptions = {
Auth: {
tokenProvider: mockTokenProvider,
},
};
Amplify.configure(mockResourceConfig, mockLibraryOptions);

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
mockResourceConfig,
mockLibraryOptions
);
});
});
});
});

describe('DefaultAmplify.getConfig()', () => {
it('should invoke AmplifySingleton.getConfig and return its result', () => {
mockAmplifySingletonGetConfig.mockReturnValueOnce(mockResourceConfig);
const result = Amplify.getConfig();

expect(mockAmplifySingletonGetConfig).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockResourceConfig);
});
});
});
7 changes: 7 additions & 0 deletions packages/aws-amplify/auth/cognito/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "aws-amplify/auth/cognito/server",
"main": "../../../lib/auth/cognito/server/index.js",
"browser": "../../../lib-esm/auth/cognito/server/index.js",
"module": "../../../lib-esm/auth/cognito/server/index.js",
"typings": "../../../lib-esm/auth/cognito/server/index.d.ts"
}
20 changes: 20 additions & 0 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"import": "./lib-esm/auth/cognito/index.js",
"require": "./lib/auth/cognito/index.js"
},
"./auth/cognito/server": {
"types": "./lib-esm/auth/cognito/server/index.d.ts",
"import": "./lib-esm/auth/cognito/server/index.js",
"require": "./lib/auth/cognito/server/index.js"
},
"./auth/server": {
"types": "./lib-esm/auth/server.d.ts",
"import": "./lib-esm/auth/server.js",
Expand Down Expand Up @@ -83,6 +88,12 @@
"auth/cognito": [
"./lib-esm/auth/cognito/index.d.ts"
],
"auth/cognito/server": [
"./lib-esm/auth/cognito/server/index.d.ts"
],
"auth/server": [
"./lib-esm/auth/server.d.ts"
],
"analytics": [
"./lib-esm/analytics/index.d.ts"
],
Expand All @@ -94,6 +105,15 @@
],
"storage/s3": [
"./lib-esm/storage/s3/index.d.ts"
],
"storage/server": [
"./lib-esm/storage/server.d.ts"
],
"storage/s3/server": [
"./lib-esm/storage/s3/server.d.ts"
],
"internals/adapter-core": [
"./lib-esm/adapterCore/index.d.ts"
]
}
},
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-amplify/src/auth/cognito/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/*
This file maps exports from `aws-amplify/auth/cognito/server`. It provides access to server-enabled Cognito APIs.
*/
export * from '@aws-amplify/auth/cognito/server';
13 changes: 8 additions & 5 deletions packages/aws-amplify/src/initSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ import {

export const DefaultAmplify = {
configure(resourceConfig: ResourcesConfig, libraryOptions?: LibraryOptions) {
if (resourceConfig.Auth && !libraryOptions) {
// When Auth config is provided but no custom Auth provider defined
// use the default Auth Providers
if (resourceConfig.Auth && !libraryOptions?.Auth) {
CognitoUserPoolsTokenProvider.setAuthConfig(resourceConfig.Auth);

const defaultLibraryOptions: LibraryOptions = {
const libraryOptionsWithDefaultAuthProviders: LibraryOptions = {
...libraryOptions,
Auth: {
tokenProvider: CognitoUserPoolsTokenProvider,
credentialsProvider: cognitoCredentialsProvider,
},
};

CognitoUserPoolsTokenProvider.setKeyValueStorage(
resourceConfig.ssr
libraryOptions?.ssr
? new CookieStorage({
sameSite: 'strict',
})
})
: LocalStorage
);

Amplify.configure(resourceConfig, defaultLibraryOptions);
Amplify.configure(resourceConfig, libraryOptionsWithDefaultAuthProviders);
} else {
Amplify.configure(resourceConfig, libraryOptions);
}
Expand Down
6 changes: 1 addition & 5 deletions packages/aws-amplify/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@
/*
This file maps exports from `aws-amplify/utils`.
*/
export {
Cache,
Hub,
I18n
} from '@aws-amplify/core';
export { Hub } from '@aws-amplify/core';
5 changes: 4 additions & 1 deletion packages/core/src/parseAWSExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const parseAWSExports = (
signUpVerificationMethod: aws_cognito_sign_up_verification_method,
userPoolClientId: aws_user_pools_web_client_id,
userPoolId: aws_user_pools_id,
...(oauth && { loginWith: getOAuthConfig(oauth) }),
...(oauth &&
Object.keys(oauth).length > 0 && {
loginWith: getOAuthConfig(oauth),
}),
},
};
}
Expand Down
Loading

0 comments on commit 863576f

Please sign in to comment.