-
Notifications
You must be signed in to change notification settings - Fork 945
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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