Skip to content

Commit 02147ef

Browse files
sam-gcavolkovi
authored andcommitted
Add the remaining oauth providers (#3500)
* Add the rest of the OAuth providers * Formatting * Add to demo
1 parent 5c20ddc commit 02147ef

File tree

8 files changed

+491
-12
lines changed

8 files changed

+491
-12
lines changed

packages-exp/auth-exp/demo/src/index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getMultiFactorResolver,
5757
OAuthProvider,
5858
GoogleAuthProvider,
59+
FacebookAuthProvider,
60+
TwitterAuthProvider,
61+
GithubAuthProvider,
5962
signInWithPopup,
6063
linkWithPopup,
6164
reauthenticateWithPopup,
@@ -1217,18 +1220,18 @@ function onPopupRedirectProviderClick(_event) {
12171220
const providerId = $(event.currentTarget).data('provider');
12181221
let provider = null;
12191222
switch (providerId) {
1220-
case 'google.com':
1221-
provider = new GoogleAuthProvider();
1222-
break;
1223-
// case 'facebook.com':
1224-
// provider = new FacebookAuthProvider();
1225-
// break;
1226-
// case 'github.com':
1227-
// provider = new GithubAuthProvider();
1228-
// break;
1229-
// case 'twitter.com':
1230-
// provider = new TwitterAuthProvider();
1231-
// break;
1223+
case 'google.com':
1224+
provider = new GoogleAuthProvider();
1225+
break;
1226+
case 'facebook.com':
1227+
provider = new FacebookAuthProvider();
1228+
break;
1229+
case 'github.com':
1230+
provider = new GithubAuthProvider();
1231+
break;
1232+
case 'twitter.com':
1233+
provider = new TwitterAuthProvider();
1234+
break;
12321235
default:
12331236
return;
12341237
}

packages-exp/auth-exp/index.webworker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ export { indexedDBLocalPersistence } from './src/core/persistence/indexed_db';
6060

6161
// core/providers
6262
export { EmailAuthProvider } from './src/core/providers/email';
63+
export { FacebookAuthProvider } from './src/core/providers/facebook';
6364
export { GoogleAuthProvider } from './src/core/providers/google';
65+
export { GithubAuthProvider } from './src/core/providers/github';
6466
export { OAuthProvider } from './src/core/providers/oauth';
6567
export { PhoneAuthProvider } from './src/core/providers/phone';
68+
export { TwitterAuthProvider } from './src/core/providers/twitter';
6669

6770
// core/strategies
6871
export { signInAnonymously } from './src/core/strategies/anonymous';
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 { expect } from 'chai';
19+
20+
import {
21+
OperationType,
22+
ProviderId,
23+
SignInMethod
24+
} from '@firebase/auth-types-exp';
25+
26+
import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
27+
import { testUser } from '../../../test/helpers/mock_auth';
28+
import { TaggedWithTokenResponse } from '../../model/id_token';
29+
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
30+
import { UserCredentialImpl } from '../user/user_credential_impl';
31+
import { FacebookAuthProvider } from './facebook';
32+
33+
describe('src/core/providers/facebook', () => {
34+
it('generates the correct type of oauth credential', () => {
35+
const cred = FacebookAuthProvider.credential('access-token');
36+
expect(cred.accessToken).to.eq('access-token');
37+
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
38+
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
39+
});
40+
41+
it('credentialFromResult creates the cred from a tagged result', () => {
42+
const userCred = new UserCredentialImpl({
43+
user: testUser({}, 'uid'),
44+
providerId: ProviderId.FACEBOOK,
45+
_tokenResponse: {
46+
...TEST_ID_TOKEN_RESPONSE,
47+
oauthAccessToken: 'access-token'
48+
},
49+
operationType: OperationType.SIGN_IN
50+
});
51+
const cred = FacebookAuthProvider.credentialFromResult(userCred)!;
52+
expect(cred.accessToken).to.eq('access-token');
53+
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
54+
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
55+
});
56+
57+
it('credentialFromError creates the cred from a tagged error', () => {
58+
const error = AUTH_ERROR_FACTORY.create(AuthErrorCode.NEED_CONFIRMATION, {
59+
appName: 'foo'
60+
});
61+
(error as TaggedWithTokenResponse)._tokenResponse = {
62+
...TEST_ID_TOKEN_RESPONSE,
63+
oauthAccessToken: 'access-token'
64+
};
65+
66+
const cred = FacebookAuthProvider.credentialFromError(error)!;
67+
expect(cred.accessToken).to.eq('access-token');
68+
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
69+
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
70+
});
71+
});
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 * as externs from '@firebase/auth-types-exp';
19+
import { FirebaseError } from '@firebase/util';
20+
21+
import { TaggedWithTokenResponse } from '../../model/id_token';
22+
import { UserCredential } from '../../model/user';
23+
import { OAuthCredential } from '../credentials/oauth';
24+
import { OAuthProvider } from './oauth';
25+
26+
export class FacebookAuthProvider extends OAuthProvider {
27+
static readonly FACEBOOK_SIGN_IN_METHOD = externs.SignInMethod.FACEBOOK;
28+
static readonly PROVIDER_ID = externs.ProviderId.FACEBOOK;
29+
readonly providerId = FacebookAuthProvider.PROVIDER_ID;
30+
31+
static credential(accessToken: string): externs.OAuthCredential {
32+
return OAuthCredential._fromParams({
33+
providerId: FacebookAuthProvider.PROVIDER_ID,
34+
signInMethod: FacebookAuthProvider.FACEBOOK_SIGN_IN_METHOD,
35+
accessToken
36+
});
37+
}
38+
39+
static credentialFromResult(
40+
userCredential: externs.UserCredential
41+
): externs.OAuthCredential | null {
42+
return FacebookAuthProvider.credentialFromTaggedObject(
43+
userCredential as UserCredential
44+
);
45+
}
46+
47+
static credentialFromError(
48+
error: FirebaseError
49+
): externs.OAuthCredential | null {
50+
return FacebookAuthProvider.credentialFromTaggedObject(
51+
error as TaggedWithTokenResponse
52+
);
53+
}
54+
55+
private static credentialFromTaggedObject({
56+
_tokenResponse: tokenResponse
57+
}: TaggedWithTokenResponse): externs.OAuthCredential | null {
58+
if (!tokenResponse || !('oauthAccessToken' in tokenResponse)) {
59+
return null;
60+
}
61+
62+
if (!tokenResponse.oauthAccessToken) {
63+
return null;
64+
}
65+
66+
try {
67+
return FacebookAuthProvider.credential(tokenResponse.oauthAccessToken);
68+
} catch {
69+
return null;
70+
}
71+
}
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 { expect } from 'chai';
19+
20+
import {
21+
OperationType,
22+
ProviderId,
23+
SignInMethod
24+
} from '@firebase/auth-types-exp';
25+
26+
import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
27+
import { testUser } from '../../../test/helpers/mock_auth';
28+
import { TaggedWithTokenResponse } from '../../model/id_token';
29+
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
30+
import { UserCredentialImpl } from '../user/user_credential_impl';
31+
import { GithubAuthProvider } from './github';
32+
33+
describe('src/core/providers/github', () => {
34+
it('generates the correct type of oauth credential', () => {
35+
const cred = GithubAuthProvider.credential('access-token');
36+
expect(cred.accessToken).to.eq('access-token');
37+
expect(cred.providerId).to.eq(ProviderId.GITHUB);
38+
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
39+
});
40+
41+
it('credentialFromResult creates the cred from a tagged result', () => {
42+
const userCred = new UserCredentialImpl({
43+
user: testUser({}, 'uid'),
44+
providerId: ProviderId.GITHUB,
45+
_tokenResponse: {
46+
...TEST_ID_TOKEN_RESPONSE,
47+
oauthAccessToken: 'access-token'
48+
},
49+
operationType: OperationType.SIGN_IN
50+
});
51+
const cred = GithubAuthProvider.credentialFromResult(userCred)!;
52+
expect(cred.accessToken).to.eq('access-token');
53+
expect(cred.providerId).to.eq(ProviderId.GITHUB);
54+
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
55+
});
56+
57+
it('credentialFromError creates the cred from a tagged error', () => {
58+
const error = AUTH_ERROR_FACTORY.create(AuthErrorCode.NEED_CONFIRMATION, {
59+
appName: 'foo'
60+
});
61+
(error as TaggedWithTokenResponse)._tokenResponse = {
62+
...TEST_ID_TOKEN_RESPONSE,
63+
oauthAccessToken: 'access-token'
64+
};
65+
66+
const cred = GithubAuthProvider.credentialFromError(error)!;
67+
expect(cred.accessToken).to.eq('access-token');
68+
expect(cred.providerId).to.eq(ProviderId.GITHUB);
69+
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
70+
});
71+
});
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 * as externs from '@firebase/auth-types-exp';
19+
import { FirebaseError } from '@firebase/util';
20+
21+
import { TaggedWithTokenResponse } from '../../model/id_token';
22+
import { UserCredential } from '../../model/user';
23+
import { OAuthCredential } from '../credentials/oauth';
24+
import { OAuthProvider } from './oauth';
25+
26+
export class GithubAuthProvider extends OAuthProvider {
27+
static readonly GITHUB_SIGN_IN_METHOD = externs.SignInMethod.GITHUB;
28+
static readonly PROVIDER_ID = externs.ProviderId.GITHUB;
29+
readonly providerId = GithubAuthProvider.PROVIDER_ID;
30+
31+
static credential(accessToken: string): externs.OAuthCredential {
32+
return OAuthCredential._fromParams({
33+
providerId: GithubAuthProvider.PROVIDER_ID,
34+
signInMethod: GithubAuthProvider.GITHUB_SIGN_IN_METHOD,
35+
accessToken
36+
});
37+
}
38+
39+
static credentialFromResult(
40+
userCredential: externs.UserCredential
41+
): externs.OAuthCredential | null {
42+
return GithubAuthProvider.credentialFromTaggedObject(
43+
userCredential as UserCredential
44+
);
45+
}
46+
47+
static credentialFromError(
48+
error: FirebaseError
49+
): externs.OAuthCredential | null {
50+
return GithubAuthProvider.credentialFromTaggedObject(
51+
error as TaggedWithTokenResponse
52+
);
53+
}
54+
55+
private static credentialFromTaggedObject({
56+
_tokenResponse: tokenResponse
57+
}: TaggedWithTokenResponse): externs.OAuthCredential | null {
58+
if (!tokenResponse || !('oauthAccessToken' in tokenResponse)) {
59+
return null;
60+
}
61+
62+
if (!tokenResponse.oauthAccessToken) {
63+
return null;
64+
}
65+
66+
try {
67+
return GithubAuthProvider.credential(tokenResponse.oauthAccessToken);
68+
} catch {
69+
return null;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)