Skip to content

Commit

Permalink
Merge pull request #398 from buggy/feature/cognito-cookie-storage
Browse files Browse the repository at this point in the history
Cognito cookie storage
  • Loading branch information
powerful23 authored Mar 5, 2018
2 parents b5754aa + 182e337 commit 7cb6195
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
13 changes: 12 additions & 1 deletion docs/media/authentication_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ Amplify.configure({
// OPTIONAL - Amazon Cognito Web Client ID
userPoolWebClientId: 'XX-XXXX-X_abcd1234',
// OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
mandatorySignIn: false
mandatorySignIn: false,
// OPTIONAL - Configuration for cookie storage
cookieStorage: {
// REQUIRED - Cookie domain (only required if cookieStorage is provided)
domain: '.yourdomain.com',
// OPTIONAL - Cookie path
path: '/',
// OPTIONAL - Cookie expiration in days
expires: 365,
// OPTIONAL - Cookie secure flag
secure: true
}
}
});
```
Expand Down
21 changes: 20 additions & 1 deletion packages/aws-amplify/__tests__/Auth/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jest.mock('amazon-cognito-identity-js/lib/CognitoUser', () => {
import { AuthOptions, SignUpParams } from '../../src/Auth/types';
import Auth from '../../src/Auth/Auth';
import Cache from '../../src/Cache';
import { CognitoUserPool, CognitoUser, CognitoUserSession, CognitoIdToken, CognitoAccessToken } from 'amazon-cognito-identity-js';
import { CookieStorage, CognitoUserPool, CognitoUser, CognitoUserSession, CognitoIdToken, CognitoAccessToken } from 'amazon-cognito-identity-js';
import { CognitoIdentityCredentials } from 'aws-sdk';

const authOptions: AuthOptions = {
Expand Down Expand Up @@ -416,6 +416,25 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('happy case using cookie storage', async () => {
const spyon = jest.spyOn(CognitoUser.prototype, 'authenticateUser')
.mockImplementationOnce((authenticationDetails, callback) => {
callback.onSuccess(session);
});

const auth = new Auth({ ...authOptions, cookieStorage: { domain: ".example.com" } });
const user = new CognitoUser({
Username: 'username',
Pool: userPool,
Storage: new CookieStorage({domain: ".yourdomain.com"})
});

expect.assertions(1);
expect(await auth.signIn('username', 'password')).toEqual(user);

spyon.mockClear();
});

test('onFailure', async () => {
const spyon = jest.spyOn(CognitoUser.prototype, "authenticateUser")
.mockImplementationOnce((authenticationDetails, callback) => {
Expand Down
53 changes: 29 additions & 24 deletions packages/aws-amplify/src/Auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '../Common';
import Platform from '../Common/Platform';
import Cache from '../Cache';
import { ICognitoUserPoolData, ICognitoUserData } from 'amazon-cognito-identity-js';

const logger = new Logger('AuthClass');

Expand All @@ -30,6 +31,7 @@ const {
} = AWS;

const {
CookieStorage,
CognitoUserPool,
CognitoUserAttribute,
CognitoUser,
Expand Down Expand Up @@ -79,12 +81,16 @@ export default class AuthClass {
}
this._config = Object.assign({}, this._config, conf);
if (!this._config.identityPoolId) { logger.debug('Do not have identityPoolId yet.'); }
const { userPoolId, userPoolWebClientId } = this._config;
const { userPoolId, userPoolWebClientId, cookieStorage } = this._config;
if (userPoolId) {
this.userPool = new CognitoUserPool({
const userPoolData: ICognitoUserPoolData = {
UserPoolId: userPoolId,
ClientId: userPoolWebClientId
});
ClientId: userPoolWebClientId,
};
if (cookieStorage) {
userPoolData.Storage = new CookieStorage(cookieStorage);
}
this.userPool = new CognitoUserPool(userPoolData);
if (Platform.isReactNative) {
const that = this;
this._userPoolStorageSync = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -168,10 +174,7 @@ export default class AuthClass {
if (!username) { return Promise.reject('Username cannot be empty'); }
if (!code) { return Promise.reject('Code cannot be empty'); }

const user = new CognitoUser({
Username: username,
Pool: this.userPool
});
const user = this.createCognitoUser(username);
return new Promise((resolve, reject) => {
user.confirmRegistration(code, true, function(err, data) {
if (err) { reject(err); } else { resolve(data); }
Expand All @@ -188,10 +191,7 @@ export default class AuthClass {
if (!this.userPool) { return Promise.reject('No userPool'); }
if (!username) { return Promise.reject('Username cannot be empty'); }

const user = new CognitoUser({
Username: username,
Pool: this.userPool
});
const user = this.createCognitoUser(username);
return new Promise((resolve, reject) => {
user.resendConfirmationCode(function(err, data) {
if (err) { reject(err); } else { resolve(data); }
Expand All @@ -210,10 +210,7 @@ export default class AuthClass {
if (!username) { return Promise.reject('Username cannot be empty'); }
if (!password) { return Promise.reject('Password cannot be empty'); }

const user = new CognitoUser({
Username: username,
Pool: this.userPool
});
const user = this.createCognitoUser(username);
const authDetails = new AuthenticationDetails({
Username: username,
Password: password
Expand Down Expand Up @@ -634,10 +631,7 @@ export default class AuthClass {
if (!this.userPool) { return Promise.reject('No userPool'); }
if (!username) { return Promise.reject('Username cannot be empty'); }

const user = new CognitoUser({
Username: username,
Pool: this.userPool
});
const user = this.createCognitoUser(username);
return new Promise((resolve, reject) => {
user.forgotPassword({
onSuccess: () => { resolve(); },
Expand Down Expand Up @@ -669,10 +663,7 @@ export default class AuthClass {
if (!code) { return Promise.reject('Code cannot be empty'); }
if (!password) { return Promise.reject('Password cannot be empty'); }

const user = new CognitoUser({
Username: username,
Pool: this.userPool
});
const user = this.createCognitoUser(username);
return new Promise((resolve, reject) => {
user.confirmPassword(code, password, {
onSuccess: () => { resolve(); },
Expand Down Expand Up @@ -898,4 +889,18 @@ export default class AuthClass {
.catch(() => resolve(null));
});
}

private createCognitoUser(username: string): Cognito.CognitoUser {
const userData: ICognitoUserData = {
Username: username,
Pool: this.userPool,
};

const { cookieStorage } = this._config;
if (cookieStorage) {
userData.Storage = new CookieStorage(cookieStorage);
}

return new CognitoUser(userData);
}
}
3 changes: 3 additions & 0 deletions packages/aws-amplify/src/Auth/types/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* and limitations under the License.
*/

import { ICookieStorageData } from "amazon-cognito-identity-js";

/**
* Parameters for user sign up
*/
Expand All @@ -29,6 +31,7 @@ export interface AuthOptions {
identityPoolId: string,
region?: string,
mandatorySignIn: boolean
cookieStorage?: ICookieStorageData,
}

/**
Expand Down

0 comments on commit 7cb6195

Please sign in to comment.