Skip to content

Add sendSignInWithEmail to auth-next #2960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions packages-exp/auth-exp/src/core/action_code_url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { ActionCodeURL } from './action_code_url';
import { mockAuth } from '../../test/mock_auth';
import { Operation } from '../model/action_code_info';

describe('ActionCodeURL', () => {
describe('_fromLink', () => {
it('should parse correctly formatted links', () => {
const continueUrl = 'https://www.example.com/path/to/file?a=1&b=2#c=3';
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=signIn&apiKey=API_KEY&' +
'continueUrl=' +
encodeURIComponent(continueUrl) +
'&languageCode=en&tenantId=TENANT_ID&state=bla';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY');
// ContinueUrl should be decoded.
expect(actionCodeUrl!.continueUrl).to.eq(continueUrl);
expect(actionCodeUrl!.tenantId).to.eq('TENANT_ID');
expect(actionCodeUrl!.languageCode).to.eq('en');
});

context('operation', () => {
it('should identitfy EMAIL_SIGNIN', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=signIn&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
});

it('should identitfy VERIFY_AND_CHANGE_EMAIL', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=verifyAndChangeEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(
Operation.VERIFY_AND_CHANGE_EMAIL
);
});

it('should identitfy VERIFY_EMAIL', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=verifyEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.VERIFY_EMAIL);
});

it('should identitfy RECOVER_EMAIL', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=recoverEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.RECOVER_EMAIL);
});

it('should identitfy PASSWORD_RESET', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=resetPassword&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.PASSWORD_RESET);
});

it('should identitfy REVERT_SECOND_FACTOR_ADDITION', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=revertSecondFactorAddition&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(
Operation.REVERT_SECOND_FACTOR_ADDITION
);
});
});

it('should work if there is a port number in the URL', () => {
const actionLink =
'https://www.example.com:8080/finishSignIn?' +
'oobCode=CODE&mode=signIn&apiKey=API_KEY&state=bla';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY');
expect(actionCodeUrl!.continueUrl).to.be.null;
expect(actionCodeUrl!.tenantId).to.be.null;
expect(actionCodeUrl!.languageCode).to.be.null;
});

it('should ignore parameters after anchor', () => {
const actionLink =
'https://www.example.com/finishSignIn?' +
'oobCode=CODE1&mode=signIn&apiKey=API_KEY1&state=bla' +
'#oobCode=CODE2&mode=signIn&apiKey=API_KEY2&state=bla';
const actionCodeUrl = ActionCodeURL._fromLink(mockAuth, actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE1');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY1');
expect(actionCodeUrl!.continueUrl).to.be.null;
expect(actionCodeUrl!.tenantId).to.be.null;
expect(actionCodeUrl!.languageCode).to.be.null;
});

context('invalid links', () => {
it('should handle missing API key, code & mode', () => {
const actionLink = 'https://www.example.com/finishSignIn';
expect(ActionCodeURL._fromLink(mockAuth, actionLink)).to.be.null;
});

it('should handle invalid mode', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&mode=INVALID_MODE&apiKey=API_KEY';
expect(ActionCodeURL._fromLink(mockAuth, actionLink)).to.be.null;
});

it('should handle missing code', () => {
const actionLink =
'https://www.example.com/finishSignIn?mode=signIn&apiKey=API_KEY';
expect(ActionCodeURL._fromLink(mockAuth, actionLink)).to.be.null;
});

it('should handle missing API key', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&mode=signIn';
expect(ActionCodeURL._fromLink(mockAuth, actionLink)).to.be.null;
});

it('should handle missing mode', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&apiKey=API_KEY';
expect(ActionCodeURL._fromLink(mockAuth, actionLink)).to.be.null;
});
});
});
});
102 changes: 102 additions & 0 deletions packages-exp/auth-exp/src/core/action_code_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { AuthErrorCode, AUTH_ERROR_FACTORY } from './errors';
import { Operation } from '../model/action_code_info';
import { Auth } from '../model/auth';

/**
* Enums for fields in URL query string.
* @enum {string}
*/
enum QueryField {
API_KEY = 'apiKey',
CODE = 'oobCode',
CONTINUE_URL = 'continueUrl',
LANGUAGE_CODE = 'languageCode',
MODE = 'mode',
TENANT_ID = 'tenantId'
}

/**
* Map from mode string in action code URL to Action Code Info operation.
*/
const MODE_TO_OPERATION_MAP: { [key: string]: Operation } = {
'recoverEmail': Operation.RECOVER_EMAIL,
'resetPassword': Operation.PASSWORD_RESET,
'signIn': Operation.EMAIL_SIGNIN,
'verifyEmail': Operation.VERIFY_EMAIL,
'verifyAndChangeEmail': Operation.VERIFY_AND_CHANGE_EMAIL,
'revertSecondFactorAddition': Operation.REVERT_SECOND_FACTOR_ADDITION
};

/**
* Maps the mode string in action code URL to Action Code Info operation.
*/
function parseMode(mode: string | null): Operation | null {
return mode ? MODE_TO_OPERATION_MAP[mode] || null : null;
}

function parseDeepLink(url: string): string {
const uri = new URL(url);
const link = uri.searchParams.get('link');
// Double link case (automatic redirect).
const doubleDeepLink = link ? new URL(link).searchParams.get('link') : null;
// iOS custom scheme links.
const iOSDeepLink = uri.searchParams.get('deep_link_id');
const iOSDoubleDeepLink = iOSDeepLink
? new URL(iOSDeepLink).searchParams.get('link')
: null;
return iOSDoubleDeepLink || iOSDeepLink || doubleDeepLink || link || url;
}

export class ActionCodeURL {
readonly apiKey: string;
readonly code: string;
readonly continueUrl: string | null;
readonly languageCode: string | null;
readonly operation: Operation;
readonly tenantId: string | null;

constructor(auth: Auth, actionLink: string) {
const uri = new URL(actionLink);
const apiKey = uri.searchParams.get(QueryField.API_KEY);
const code = uri.searchParams.get(QueryField.CODE);
const operation = parseMode(uri.searchParams.get(QueryField.MODE));
// Validate API key, code and mode.
if (!apiKey || !code || !operation) {
throw AUTH_ERROR_FACTORY.create(AuthErrorCode.ARGUMENT_ERROR, {
appName: auth.name
});
}
this.apiKey = apiKey;
this.operation = operation;
this.code = code;
this.continueUrl = uri.searchParams.get(QueryField.CONTINUE_URL);
this.languageCode = uri.searchParams.get(QueryField.LANGUAGE_CODE);
this.tenantId = uri.searchParams.get(QueryField.TENANT_ID);
}

static _fromLink(auth: Auth, link: string): ActionCodeURL | null {
const actionLink = parseDeepLink(link);
try {
return new ActionCodeURL(auth, actionLink);
} catch {
return null;
}
}
}
Loading