Skip to content

Add signInWithCustomToken implementation #3136

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
Jun 10, 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
10 changes: 5 additions & 5 deletions packages-exp/auth-exp/src/core/providers/anonymous.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use(chaiAsPromised);

describe('core/providers/anonymous', () => {
let auth: Auth;

beforeEach(async () => {
auth = await testAuth();
});

describe('AnonymousCredential', () => {
const credential = new AnonymousCredential();

Expand All @@ -50,9 +50,9 @@ describe('core/providers/anonymous', () => {

describe('#_getIdTokenResponse', () => {
it('throws', async () => {
await expect(
credential._getIdTokenResponse(auth)
).to.be.rejectedWith(Error);
await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(
Error
);
});
});

Expand Down
9 changes: 7 additions & 2 deletions packages-exp/auth-exp/src/core/providers/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
* limitations under the License.
*/

import { AuthCredential, ProviderId, SignInMethod, AuthProvider } from "@firebase/auth-types-exp";
import {
AuthCredential,
ProviderId,
SignInMethod,
AuthProvider
} from '@firebase/auth-types-exp';
import { signUp } from '../../api/authentication/sign_up';
import { Auth } from '../../model/auth';
import { IdTokenResponse } from "../../model/id_token";
import { IdTokenResponse } from '../../model/id_token';
import { debugFail } from '../util/assert';

export class AnonymousCredential implements AuthCredential {
Expand Down
6 changes: 2 additions & 4 deletions packages-exp/auth-exp/src/core/strategies/anonymous.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('core/strategies/anonymous', () => {
const serverUser: APIUserInfo = {
localId: 'local-id'
};

beforeEach(async () => {
auth = await testAuth();
mockFetch.setUp();
Expand All @@ -52,9 +52,7 @@ describe('core/strategies/anonymous', () => {

describe('signInAnonymously', () => {
it('should sign in an anonymous user', async () => {
const { credential, user, operationType } = await signInAnonymously(
auth
);
const { credential, user, operationType } = await signInAnonymously(auth);
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
expect(operationType).to.eq(OperationType.SIGN_IN);
Expand Down
5 changes: 3 additions & 2 deletions packages-exp/auth-exp/src/core/strategies/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { AnonymousProvider } from '../providers/anonymous';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { signInWithCredential } from './credential';

export async function signInAnonymously(externAuth: externs.Auth): Promise<externs.UserCredential> {
export async function signInAnonymously(
externAuth: externs.Auth
): Promise<externs.UserCredential> {
const auth = externAuth as Auth;
const credential = AnonymousProvider.credential();
if (auth.currentUser?.isAnonymous) {
Expand All @@ -34,4 +36,3 @@ export async function signInAnonymously(externAuth: externs.Auth): Promise<exter
}
return signInWithCredential(auth, credential);
}

3 changes: 1 addition & 2 deletions packages-exp/auth-exp/src/core/strategies/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { OperationType, UserCredential } from '@firebase/auth-types-exp';

import { Auth } from '../../model/auth';
import { AuthCredential } from '../../model/auth_credential';
import { User } from '../../model/user';
import { UserCredentialImpl } from '../user/user_credential_impl';

export async function signInWithCredential(
Expand All @@ -37,6 +36,6 @@ export async function signInWithCredential(
OperationType.SIGN_IN,
response
);
await auth.updateCurrentUser(userCredential.user as User);
await auth.updateCurrentUser(userCredential.user);
return userCredential;
}
83 changes: 83 additions & 0 deletions packages-exp/auth-exp/src/core/strategies/custom_token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @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, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

import { OperationType } from '@firebase/auth-types-exp';

import { mockEndpoint } from '../../../test/api/helper';
import { testAuth } from '../../../test/mock_auth';
import * as mockFetch from '../../../test/mock_fetch';
import { Endpoint } from '../../api';
import { APIUserInfo } from '../../api/account_management/account';
import { Auth } from '../../model/auth';
import { IdTokenResponse } from '../../model/id_token';
import { signInWithCustomToken } from './custom_token';

use(chaiAsPromised);

describe('core/strategies/signInWithCustomToken', () => {
const serverUser: APIUserInfo = {
localId: 'local-id',
displayName: 'display-name',
photoUrl: 'photo-url',
email: 'email',
emailVerified: true,
phoneNumber: 'phone-number',
tenantId: 'tenant-id',
createdAt: 123,
lastLoginAt: 456
};

const idTokenResponse: IdTokenResponse = {
idToken: 'my-id-token',
refreshToken: 'my-refresh-token',
expiresIn: '1234',
localId: serverUser.localId!,
kind: 'my-kind'
};

let auth: Auth;

beforeEach(async () => {
auth = await testAuth();
mockFetch.setUp();
mockEndpoint(Endpoint.SIGN_IN_WITH_CUSTOM_TOKEN, idTokenResponse);
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
users: [serverUser]
});
});
afterEach(mockFetch.tearDown);

it('should return a valid user credential', async () => {
const { credential, user, operationType } = await signInWithCustomToken(
auth,
'look-at-me-im-a-jwt'
);
expect(credential).to.be.null;
expect(user.uid).to.eq('local-id');
expect(user.tenantId).to.eq('tenant-id');
expect(user.displayName).to.eq('display-name');
expect(operationType).to.eq(OperationType.SIGN_IN);
});

it('should update the current user', async () => {
const { user } = await signInWithCustomToken(auth, 'oh.no');
expect(auth.currentUser).to.eq(user);
});
});
41 changes: 41 additions & 0 deletions packages-exp/auth-exp/src/core/strategies/custom_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @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 * as externs from '@firebase/auth-types-exp';

import { signInWithCustomToken as getIdTokenResponse } from '../../api/authentication/custom_token';
import { Auth } from '../../model/auth';
import { IdTokenResponse } from '../../model/id_token';
import { UserCredentialImpl } from '../user/user_credential_impl';

export async function signInWithCustomToken(
authExtern: externs.Auth,
customToken: string
): Promise<externs.UserCredential> {
const auth = authExtern as Auth;
const response: IdTokenResponse = await getIdTokenResponse(auth, {
token: customToken
});
const cred = await UserCredentialImpl._fromIdTokenResponse(
auth,
null,
externs.OperationType.SIGN_IN,
response
);
await auth.updateCurrentUser(cred.user);
return cred;
}
15 changes: 10 additions & 5 deletions packages-exp/auth-exp/src/core/user/user_credential_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,32 @@
* limitations under the License.
*/

import { OperationType, UserCredential, ProviderId } from '@firebase/auth-types-exp';
import * as externs from '@firebase/auth-types-exp';

import { Auth } from '../../model/auth';
import { AuthCredential } from '../../model/auth_credential';
import { IdTokenResponse } from '../../model/id_token';
import { User } from '../../model/user';
import { User, UserCredential } from '../../model/user';
import { UserImpl } from './user_impl';

export class UserCredentialImpl implements UserCredential {
constructor(
public readonly user: User,
public readonly credential: AuthCredential | null,
public readonly operationType: OperationType
public readonly operationType: externs.OperationType
) {}

static async _fromIdTokenResponse(
auth: Auth,
credential: AuthCredential | null,
operationType: OperationType,
operationType: externs.OperationType,
idTokenResponse: IdTokenResponse
): Promise<UserCredential> {
const user = await UserImpl._fromIdTokenResponse(auth, idTokenResponse, credential?.providerId === ProviderId.ANONYMOUS);
const user = await UserImpl._fromIdTokenResponse(
auth,
idTokenResponse,
credential?.providerId === externs.ProviderId.ANONYMOUS
);
const userCred = new UserCredentialImpl(user, credential, operationType);
// TODO: handle additional user info
// updateAdditionalUserInfoFromIdTokenResponse(userCred, idTokenResponse);
Expand Down
2 changes: 1 addition & 1 deletion packages-exp/auth-exp/src/core/user/user_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class UserImpl implements User {
phoneNumber: string | null;
photoURL: string | null;
isAnonymous: boolean = false;

constructor({ uid, auth, stsTokenManager, ...opt }: UserParameters) {
this.uid = uid;
this.auth = auth;
Expand Down
1 change: 1 addition & 0 deletions packages-exp/auth-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { PhoneAuthProvider } from './core/providers/phone';
// core/strategies
export { signInAnonymously } from './core/strategies/anonymous';
export { signInWithCredential } from './core/strategies/credential';
export { signInWithCustomToken } from './core/strategies/custom_token';
export {
sendPasswordResetEmail,
confirmPasswordReset,
Expand Down
4 changes: 4 additions & 0 deletions packages-exp/auth-exp/src/model/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ export interface User extends externs.User {
delete(): Promise<void>;
toPlainObject(): PersistedBlob;
}

export interface UserCredential extends externs.UserCredential {
user: User;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}