Skip to content

Add the remaining oauth providers #3500

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 3 commits into from
Jul 29, 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
27 changes: 15 additions & 12 deletions packages-exp/auth-exp/demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ import {
getMultiFactorResolver,
OAuthProvider,
GoogleAuthProvider,
FacebookAuthProvider,
TwitterAuthProvider,
GithubAuthProvider,
signInWithPopup,
linkWithPopup,
reauthenticateWithPopup,
Expand Down Expand Up @@ -1217,18 +1220,18 @@ function onPopupRedirectProviderClick(_event) {
const providerId = $(event.currentTarget).data('provider');
let provider = null;
switch (providerId) {
case 'google.com':
provider = new GoogleAuthProvider();
break;
// case 'facebook.com':
// provider = new FacebookAuthProvider();
// break;
// case 'github.com':
// provider = new GithubAuthProvider();
// break;
// case 'twitter.com':
// provider = new TwitterAuthProvider();
// break;
case 'google.com':
provider = new GoogleAuthProvider();
break;
case 'facebook.com':
provider = new FacebookAuthProvider();
break;
case 'github.com':
provider = new GithubAuthProvider();
break;
case 'twitter.com':
provider = new TwitterAuthProvider();
break;
default:
return;
}
Expand Down
3 changes: 3 additions & 0 deletions packages-exp/auth-exp/index.webworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ export { indexedDBLocalPersistence } from './src/core/persistence/indexed_db';

// core/providers
export { EmailAuthProvider } from './src/core/providers/email';
export { FacebookAuthProvider } from './src/core/providers/facebook';
export { GoogleAuthProvider } from './src/core/providers/google';
export { GithubAuthProvider } from './src/core/providers/github';
export { OAuthProvider } from './src/core/providers/oauth';
export { PhoneAuthProvider } from './src/core/providers/phone';
export { TwitterAuthProvider } from './src/core/providers/twitter';

// core/strategies
export { signInAnonymously } from './src/core/strategies/anonymous';
Expand Down
71 changes: 71 additions & 0 deletions packages-exp/auth-exp/src/core/providers/facebook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @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 {
OperationType,
ProviderId,
SignInMethod
} from '@firebase/auth-types-exp';

import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
import { testUser } from '../../../test/helpers/mock_auth';
import { TaggedWithTokenResponse } from '../../model/id_token';
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { FacebookAuthProvider } from './facebook';

describe('src/core/providers/facebook', () => {
it('generates the correct type of oauth credential', () => {
const cred = FacebookAuthProvider.credential('access-token');
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
});

it('credentialFromResult creates the cred from a tagged result', () => {
const userCred = new UserCredentialImpl({
user: testUser({}, 'uid'),
providerId: ProviderId.FACEBOOK,
_tokenResponse: {
...TEST_ID_TOKEN_RESPONSE,
oauthAccessToken: 'access-token'
},
operationType: OperationType.SIGN_IN
});
const cred = FacebookAuthProvider.credentialFromResult(userCred)!;
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
});

it('credentialFromError creates the cred from a tagged error', () => {
const error = AUTH_ERROR_FACTORY.create(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'foo'
});
(error as TaggedWithTokenResponse)._tokenResponse = {
...TEST_ID_TOKEN_RESPONSE,
oauthAccessToken: 'access-token'
};

const cred = FacebookAuthProvider.credentialFromError(error)!;
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.FACEBOOK);
expect(cred.signInMethod).to.eq(SignInMethod.FACEBOOK);
});
});
72 changes: 72 additions & 0 deletions packages-exp/auth-exp/src/core/providers/facebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @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 { FirebaseError } from '@firebase/util';

import { TaggedWithTokenResponse } from '../../model/id_token';
import { UserCredential } from '../../model/user';
import { OAuthCredential } from '../credentials/oauth';
import { OAuthProvider } from './oauth';

export class FacebookAuthProvider extends OAuthProvider {
static readonly FACEBOOK_SIGN_IN_METHOD = externs.SignInMethod.FACEBOOK;
static readonly PROVIDER_ID = externs.ProviderId.FACEBOOK;
readonly providerId = FacebookAuthProvider.PROVIDER_ID;

static credential(accessToken: string): externs.OAuthCredential {
return OAuthCredential._fromParams({
providerId: FacebookAuthProvider.PROVIDER_ID,
signInMethod: FacebookAuthProvider.FACEBOOK_SIGN_IN_METHOD,
accessToken
});
}

static credentialFromResult(
Copy link
Contributor

Choose a reason for hiding this comment

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

can these be defined in OAuthProvider?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately no, since they're all subtly different and TS has no concept of static abstract functions

userCredential: externs.UserCredential
): externs.OAuthCredential | null {
return FacebookAuthProvider.credentialFromTaggedObject(
userCredential as UserCredential
);
}

static credentialFromError(
error: FirebaseError
): externs.OAuthCredential | null {
return FacebookAuthProvider.credentialFromTaggedObject(
error as TaggedWithTokenResponse
);
}

private static credentialFromTaggedObject({
_tokenResponse: tokenResponse
}: TaggedWithTokenResponse): externs.OAuthCredential | null {
if (!tokenResponse || !('oauthAccessToken' in tokenResponse)) {
return null;
}

if (!tokenResponse.oauthAccessToken) {
return null;
}

try {
return FacebookAuthProvider.credential(tokenResponse.oauthAccessToken);
} catch {
return null;
}
}
}
71 changes: 71 additions & 0 deletions packages-exp/auth-exp/src/core/providers/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @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 {
OperationType,
ProviderId,
SignInMethod
} from '@firebase/auth-types-exp';

import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
import { testUser } from '../../../test/helpers/mock_auth';
import { TaggedWithTokenResponse } from '../../model/id_token';
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { GithubAuthProvider } from './github';

describe('src/core/providers/github', () => {
it('generates the correct type of oauth credential', () => {
const cred = GithubAuthProvider.credential('access-token');
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.GITHUB);
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
});

it('credentialFromResult creates the cred from a tagged result', () => {
const userCred = new UserCredentialImpl({
user: testUser({}, 'uid'),
providerId: ProviderId.GITHUB,
_tokenResponse: {
...TEST_ID_TOKEN_RESPONSE,
oauthAccessToken: 'access-token'
},
operationType: OperationType.SIGN_IN
});
const cred = GithubAuthProvider.credentialFromResult(userCred)!;
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.GITHUB);
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
});

it('credentialFromError creates the cred from a tagged error', () => {
const error = AUTH_ERROR_FACTORY.create(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'foo'
});
(error as TaggedWithTokenResponse)._tokenResponse = {
...TEST_ID_TOKEN_RESPONSE,
oauthAccessToken: 'access-token'
};

const cred = GithubAuthProvider.credentialFromError(error)!;
expect(cred.accessToken).to.eq('access-token');
expect(cred.providerId).to.eq(ProviderId.GITHUB);
expect(cred.signInMethod).to.eq(SignInMethod.GITHUB);
});
});
72 changes: 72 additions & 0 deletions packages-exp/auth-exp/src/core/providers/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @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 { FirebaseError } from '@firebase/util';

import { TaggedWithTokenResponse } from '../../model/id_token';
import { UserCredential } from '../../model/user';
import { OAuthCredential } from '../credentials/oauth';
import { OAuthProvider } from './oauth';

export class GithubAuthProvider extends OAuthProvider {
static readonly GITHUB_SIGN_IN_METHOD = externs.SignInMethod.GITHUB;
static readonly PROVIDER_ID = externs.ProviderId.GITHUB;
readonly providerId = GithubAuthProvider.PROVIDER_ID;

static credential(accessToken: string): externs.OAuthCredential {
return OAuthCredential._fromParams({
providerId: GithubAuthProvider.PROVIDER_ID,
signInMethod: GithubAuthProvider.GITHUB_SIGN_IN_METHOD,
accessToken
});
}

static credentialFromResult(
userCredential: externs.UserCredential
): externs.OAuthCredential | null {
return GithubAuthProvider.credentialFromTaggedObject(
userCredential as UserCredential
);
}

static credentialFromError(
error: FirebaseError
): externs.OAuthCredential | null {
return GithubAuthProvider.credentialFromTaggedObject(
error as TaggedWithTokenResponse
);
}

private static credentialFromTaggedObject({
_tokenResponse: tokenResponse
}: TaggedWithTokenResponse): externs.OAuthCredential | null {
if (!tokenResponse || !('oauthAccessToken' in tokenResponse)) {
return null;
}

if (!tokenResponse.oauthAccessToken) {
return null;
}

try {
return GithubAuthProvider.credential(tokenResponse.oauthAccessToken);
} catch {
return null;
}
}
}
Loading