Skip to content

Commit 366e9ef

Browse files
committed
Cleanup credential inheritance tree
1 parent 308e47d commit 366e9ef

File tree

15 files changed

+161
-131
lines changed

15 files changed

+161
-131
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { ProviderId, SignInMethod } from '@firebase/auth-types-exp';
19+
import { expect, use } from 'chai';
20+
import * as chaiAsPromised from 'chai-as-promised';
21+
import { testAuth } from '../../../test/mock_auth';
22+
import { Auth } from '../../model/auth';
23+
import { AnonymousCredential } from './anonymous';
24+
/* */
25+
use(chaiAsPromised);
26+
27+
describe('core/credentials/anonymous', () => {
28+
let auth: Auth;
29+
const credential = new AnonymousCredential();
30+
31+
beforeEach(async () => {
32+
auth = await testAuth();
33+
});
34+
35+
it('should have an anonymous provider', () => {
36+
expect(credential.providerId).to.eq(ProviderId.ANONYMOUS);
37+
});
38+
39+
it('should have an anonymous sign in method', () => {
40+
expect(credential.signInMethod).to.eq(SignInMethod.ANONYMOUS);
41+
});
42+
43+
describe('#toJSON', () => {
44+
it('throws', () => {
45+
expect(credential.toJSON).to.throw(Error);
46+
});
47+
});
48+
49+
describe('#_getIdTokenResponse', () => {
50+
it('throws', async () => {
51+
await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(
52+
Error
53+
);
54+
});
55+
});
56+
57+
describe('#_linkToIdToken', () => {
58+
it('throws', async () => {
59+
await expect(
60+
credential._linkToIdToken(auth, 'id-token')
61+
).to.be.rejectedWith(Error);
62+
});
63+
});
64+
65+
describe('#_matchIdTokenWithUid', () => {
66+
it('throws', () => {
67+
expect(() =>
68+
credential._matchIdTokenWithUid(auth, 'other-uid')
69+
).to.throw(Error);
70+
});
71+
});
72+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {
19+
ProviderId,
20+
SignInMethod
21+
} from '@firebase/auth-types-exp';
22+
import { signUp } from '../../api/authentication/sign_up';
23+
import { Auth } from '../../model/auth';
24+
import { IdTokenResponse } from '../../model/id_token';
25+
import { debugFail } from '../util/assert';
26+
import { AuthCredential } from '.';
27+
28+
export class AnonymousCredential implements AuthCredential {
29+
providerId = ProviderId.ANONYMOUS;
30+
signInMethod = SignInMethod.ANONYMOUS;
31+
32+
toJSON(): never {
33+
debugFail('Method not implemented.');
34+
}
35+
36+
static fromJSON(_json: object | string): AnonymousCredential | null {
37+
debugFail('Method not implemented');
38+
}
39+
40+
async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> {
41+
return signUp(auth, {
42+
returnSecureToken: true
43+
});
44+
}
45+
46+
async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> {
47+
debugFail("Can't link to an anonymous credential");
48+
}
49+
50+
_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> {
51+
debugFail('Method not implemented.');
52+
}
53+
}

packages-exp/auth-exp/src/model/auth_credential.d.ts renamed to packages-exp/auth-exp/src/core/credentials/index.d.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
*/
1717

1818
import * as externs from '@firebase/auth-types-exp';
19+
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa';
20+
import { IdTokenResponse } from '../../model/id_token';
21+
import { Auth } from '../../model/auth';
1922

20-
import { Auth } from './auth';
21-
import { IdTokenResponse } from './id_token';
22-
import { PhoneOrOauthTokenResponse } from '../api/authentication/mfa';
23-
24-
export interface AuthCredential extends externs.AuthCredential {
25-
readonly providerId: externs.ProviderId;
26-
readonly signInMethod: externs.SignInMethod;
27-
toJSON(): object;
23+
export abstract class AuthCredential extends externs.AuthCredential {
24+
static fromJSON(json: object | string): AuthCredential | null;
25+
2826
_getIdTokenResponse(auth: Auth): Promise<PhoneOrOauthTokenResponse>;
2927
_linkToIdToken(auth: Auth, idToken: string): Promise<IdTokenResponse>;
3028
_matchIdTokenWithUid(auth: Auth, uid: string): Promise<IdTokenResponse>;

packages-exp/auth-exp/src/core/strategies/phone_credential.test.ts renamed to packages-exp/auth-exp/src/core/credentials/phone.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import * as fetch from '../../../test/mock_fetch';
2323
import { Endpoint } from '../../api';
2424
import { Auth } from '../../model/auth';
2525
import { IdTokenResponse } from '../../model/id_token';
26-
import { PhoneAuthCredential } from './phone_credential';
26+
import { PhoneAuthCredential } from '../credentials/phone';
2727

28-
describe('core/strategies/phone_credential', () => {
28+
describe('core/credentials/phone', () => {
2929
let auth: Auth;
3030

3131
beforeEach(async () => {

packages-exp/auth-exp/src/core/strategies/phone_credential.ts renamed to packages-exp/auth-exp/src/core/credentials/phone.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { Auth } from '../../model/auth';
2626
import { IdTokenResponse } from '../../model/id_token';
2727
import { debugFail } from '../util/assert';
28+
import { AuthCredential } from '.';
2829

2930
export interface PhoneAuthCredentialParameters {
3031
verificationId?: string;
@@ -33,7 +34,7 @@ export interface PhoneAuthCredentialParameters {
3334
temporaryProof?: string;
3435
}
3536

36-
export class PhoneAuthCredential implements externs.AuthCredential {
37+
export class PhoneAuthCredential implements AuthCredential {
3738
readonly providerId = externs.ProviderId.PHONE;
3839
readonly signInMethod = externs.SignInMethod.PHONE;
3940

@@ -92,7 +93,7 @@ export class PhoneAuthCredential implements externs.AuthCredential {
9293
return obj;
9394
}
9495

95-
static fromJSON(json: string | object): externs.AuthCredential | null {
96+
static fromJSON(json: object | string): PhoneAuthCredential | null {
9697
if (typeof json === 'string') {
9798
json = JSON.parse(json);
9899
}
@@ -119,11 +120,4 @@ export class PhoneAuthCredential implements externs.AuthCredential {
119120
temporaryProof
120121
});
121122
}
122-
}
123-
124-
/** PhoneAuthCredential for public export; has a private constructor */
125-
export class ExternPhoneAuthCredential extends PhoneAuthCredential {
126-
private constructor(params: PhoneAuthCredentialParameters) {
127-
super(params);
128-
}
129-
}
123+
}

packages-exp/auth-exp/src/core/providers/anonymous.test.ts

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,68 +18,16 @@
1818
import { ProviderId, SignInMethod } from '@firebase/auth-types-exp';
1919
import { expect, use } from 'chai';
2020
import * as chaiAsPromised from 'chai-as-promised';
21-
import { testAuth } from '../../../test/mock_auth';
22-
import { Auth } from '../../model/auth';
23-
import { AnonymousCredential, AnonymousProvider } from './anonymous';
21+
import { AnonymousProvider } from './anonymous';
2422

2523
use(chaiAsPromised);
2624

2725
describe('core/providers/anonymous', () => {
28-
let auth: Auth;
29-
30-
beforeEach(async () => {
31-
auth = await testAuth();
32-
});
33-
34-
describe('AnonymousCredential', () => {
35-
const credential = new AnonymousCredential();
36-
37-
it('should have an anonymous provider', () => {
26+
describe('.credential', () => {
27+
it('should return an anonymous credential', () => {
28+
const credential = AnonymousProvider.credential();
3829
expect(credential.providerId).to.eq(ProviderId.ANONYMOUS);
39-
});
40-
41-
it('should have an anonymous sign in method', () => {
4230
expect(credential.signInMethod).to.eq(SignInMethod.ANONYMOUS);
4331
});
44-
45-
describe('#toJSON', () => {
46-
it('throws', () => {
47-
expect(credential.toJSON).to.throw(Error);
48-
});
49-
});
50-
51-
describe('#_getIdTokenResponse', () => {
52-
it('throws', async () => {
53-
await expect(credential._getIdTokenResponse(auth)).to.be.rejectedWith(
54-
Error
55-
);
56-
});
57-
});
58-
59-
describe('#_linkToIdToken', () => {
60-
it('throws', async () => {
61-
await expect(
62-
credential._linkToIdToken(auth, 'id-token')
63-
).to.be.rejectedWith(Error);
64-
});
65-
});
66-
67-
describe('#_matchIdTokenWithUid', () => {
68-
it('throws', () => {
69-
expect(() =>
70-
credential._matchIdTokenWithUid(auth, 'other-uid')
71-
).to.throw(Error);
72-
});
73-
});
74-
});
75-
76-
describe('AnonymousProvider', () => {
77-
describe('.credential', () => {
78-
it('should return an anonymous credential', () => {
79-
const credential = AnonymousProvider.credential();
80-
expect(credential.providerId).to.eq(ProviderId.ANONYMOUS);
81-
expect(credential.signInMethod).to.eq(SignInMethod.ANONYMOUS);
82-
});
83-
});
8432
});
8533
});

packages-exp/auth-exp/src/core/providers/anonymous.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
import {
19-
AuthCredential,
20-
ProviderId,
21-
SignInMethod,
22-
AuthProvider
23-
} from '@firebase/auth-types-exp';
24-
import { signUp } from '../../api/authentication/sign_up';
25-
import { Auth } from '../../model/auth';
26-
import { IdTokenResponse } from '../../model/id_token';
27-
import { debugFail } from '../util/assert';
28-
29-
export class AnonymousCredential implements AuthCredential {
30-
providerId = ProviderId.ANONYMOUS;
31-
signInMethod = SignInMethod.ANONYMOUS;
32-
33-
toJSON(): never {
34-
debugFail('Method not implemented.');
35-
}
36-
37-
static fromJSON(): never {
38-
debugFail('Method not implemented');
39-
}
40-
41-
async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> {
42-
return signUp(auth, {
43-
returnSecureToken: true
44-
});
45-
}
46-
47-
async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> {
48-
debugFail("Can't link to an anonymous credential");
49-
}
50-
51-
_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> {
52-
debugFail('Method not implemented.');
53-
}
54-
}
18+
import { AuthProvider, ProviderId } from '@firebase/auth-types-exp';
19+
import { AnonymousCredential } from '../credentials/anonymous';
5520

5621
export class AnonymousProvider implements AuthProvider {
5722
providerId = ProviderId.ANONYMOUS;

packages-exp/auth-exp/src/core/providers/phone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
*/
1717

1818
import * as externs from '@firebase/auth-types-exp';
19-
import { FirebaseError } from '@firebase/util';
2019

20+
import { FirebaseError } from '@firebase/util';
2121
import { Auth } from '../../model/auth';
2222
import { initializeAuth } from '../auth/auth_impl';
23+
import { PhoneAuthCredential } from '../credentials/phone';
2324
import { _verifyPhoneNumber } from '../strategies/phone';
24-
import { PhoneAuthCredential } from '../strategies/phone_credential';
2525
import { debugFail } from '../util/assert';
2626

2727
export class PhoneAuthProvider implements externs.AuthProvider {

packages-exp/auth-exp/src/core/strategies/credential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717

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

20+
import { OperationType, UserCredential } from '@firebase/auth-types-exp';
2121
import { Auth } from '../../model/auth';
22-
import { AuthCredential } from '../../model/auth_credential';
22+
import { AuthCredential } from '../credentials';
2323
import { UserCredentialImpl } from '../user/user_credential_impl';
2424

2525
export async function signInWithCredential(

packages-exp/auth-exp/src/core/strategies/phone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { AuthErrorCode } from '../errors';
2424
import { PhoneAuthProvider } from '../providers/phone';
2525
import { assert } from '../util/assert';
2626
import { signInWithCredential } from './credential';
27-
import { PhoneAuthCredential } from './phone_credential';
27+
import { PhoneAuthCredential } from '../credentials/phone';
2828

2929
interface OnConfirmationCallback {
3030
(credential: PhoneAuthCredential): Promise<externs.UserCredential>;

packages-exp/auth-exp/src/core/user/user_credential_impl.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ import * as mockFetch from '../../../test/mock_fetch';
3333
import { Endpoint } from '../../api';
3434
import { APIUserInfo } from '../../api/account_management/account';
3535
import { Auth } from '../../model/auth';
36-
import { AuthCredential } from '../../model/auth_credential';
3736
import { IdTokenResponse } from '../../model/id_token';
3837
import { UserCredentialImpl } from './user_credential_impl';
38+
import { AuthCredential } from '../credentials';
3939

4040
use(chaiAsPromised);
4141
use(sinonChai);

packages-exp/auth-exp/src/core/user/user_credential_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import * as externs from '@firebase/auth-types-exp';
1919

2020
import { Auth } from '../../model/auth';
21-
import { AuthCredential } from '../../model/auth_credential';
2221
import { IdTokenResponse } from '../../model/id_token';
2322
import { User, UserCredential } from '../../model/user';
2423
import { UserImpl } from './user_impl';
24+
import { AuthCredential } from '../credentials';
2525

2626
export class UserCredentialImpl implements UserCredential {
2727
constructor(

0 commit comments

Comments
 (0)