Skip to content

Fixes firebaseui.auth.idp.getAuthCredential credential initializer wh… #125

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 2 commits into from
Apr 5, 2017
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
13 changes: 11 additions & 2 deletions javascript/utils/idp.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ firebaseui.auth.idp.getAuthCredential = function(credentialObject) {
if (credentialObject['secret'] && credentialObject['accessToken']) {
credentialObject['oauthToken'] = credentialObject['accessToken'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need these lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we don't. I will remove them in my next PR.

credentialObject['oauthTokenSecret'] = credentialObject['secret'];
return firebase.auth[firebaseui.auth.idp.AuthProviders[providerId]]
.credential(credentialObject['accessToken'],
credentialObject['secret']);
} else if (providerId == firebase.auth.GoogleAuthProvider.PROVIDER_ID) {
return firebase.auth[firebaseui.auth.idp.AuthProviders[providerId]]
.credential(credentialObject['idToken'],
credentialObject['accessToken']);
} else {
// GitHub and Facebook.
return firebase.auth[firebaseui.auth.idp.AuthProviders[providerId]]
.credential(credentialObject['accessToken']);
}
return firebase.auth[firebaseui.auth.idp.AuthProviders[providerId]]
.credential(credentialObject);
}
return null;
};
31 changes: 12 additions & 19 deletions javascript/utils/idp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function setUp() {
firebase.auth = {};
for (var providerId in firebaseui.auth.idp.AuthProviders) {
firebase.auth[firebaseui.auth.idp.AuthProviders[providerId]] = {
'PROVIDER_ID': providerId,
'credential': goog.testing.recordFunction(function() {
// Return something.
return providerId;
Expand Down Expand Up @@ -64,15 +65,17 @@ function testIsSupportedProvider() {
/**
* Asserts the credential is initialized with correct OAuth response.
* @param {!Object} provider The provider object.
* @param {!Object} oauthResponse The response used to initialize the
* @param {!Array<string>} oauthParams The OAuth params used to initialize the
* credential.
* @param {!Object} ref The credential reference.
*/
function assertCredential(provider, oauthResponse, ref) {
function assertCredential(provider, oauthParams, ref) {
assertNotNullNorUndefined(ref);
var parameter = provider.credential.getLastCall().getArgument(0);
for (var key in oauthResponse) {
assertEquals(oauthResponse[key], parameter[key]);
var parameters = provider.credential.getLastCall().getArguments();
assertEquals(oauthParams.length, parameters.length);
// Confirm the expected parameters passed when initializing the credential.
for (var i = 0; i < parameters.length; i++) {
assertEquals(oauthParams[i], parameters[i]);
}
}

Expand All @@ -86,10 +89,7 @@ function testGetAuthCredential_google() {
var ref = firebaseui.auth.idp.getAuthCredential(cred);
assertCredential(
firebase.auth.GoogleAuthProvider,
{
'idToken': 'ID_TOKEN',
'accessToken': 'ACCESS_TOKEN'
},
['ID_TOKEN', 'ACCESS_TOKEN'],
ref);
}

Expand All @@ -102,9 +102,7 @@ function testGetAuthCredential_facebook() {
var ref = firebaseui.auth.idp.getAuthCredential(cred);
assertCredential(
firebase.auth.FacebookAuthProvider,
{
'accessToken': 'ACCESS_TOKEN'
},
['ACCESS_TOKEN'],
ref);
}

Expand All @@ -118,10 +116,7 @@ function testGetAuthCredential_twitter() {
var ref = firebaseui.auth.idp.getAuthCredential(cred);
assertCredential(
firebase.auth.TwitterAuthProvider,
{
'oauthToken': 'ACCESS_TOKEN',
'oauthTokenSecret': 'SECRET'
},
['ACCESS_TOKEN', 'SECRET'],
ref);
}

Expand All @@ -134,9 +129,7 @@ function testGetAuthCredential_github() {
var ref = firebaseui.auth.idp.getAuthCredential(cred);
assertCredential(
firebase.auth.GithubAuthProvider,
{
'accessToken': 'ACCESS_TOKEN'
},
['ACCESS_TOKEN'],
ref);
}

Expand Down
9 changes: 6 additions & 3 deletions javascript/utils/pendingemailcredential_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ function setUp() {
// Mock credential.
firebase['auth'] = firebase['auth'] || {
'GoogleAuthProvider' : {
'credential' : function(response) {
return response;
}
'credential' : function(idToken, accessToken) {
assertEquals(credential['idToken'], idToken);
assertEquals(credential['accessToken'], accessToken);
return credential;
},
'PROVIDER_ID': 'google.com'
}
};
credential = {
Expand Down
16 changes: 15 additions & 1 deletion javascript/widgets/handler/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,11 @@ firebaseui.auth.widget.handler.common.isCredentialExpired = function(error) {
* configuration is used.
* @param {!firebaseui.auth.ui.page.Base} component The current UI component.
* @param {string} providerId The provider ID of the selected IdP.
* @param {?string=} opt_email The optional email to try to sign in with.
* @package
*/
firebaseui.auth.widget.handler.common.federatedSignIn = function(
app, component, providerId) {
app, component, providerId, opt_email) {
var container = component.getContainer();
var providerSigninFailedCallback = function(error) {
// TODO: align redirect and popup flow error handling for similar errors.
Expand Down Expand Up @@ -642,6 +643,19 @@ firebaseui.auth.widget.handler.common.federatedSignIn = function(
provider['addScope'](additionalScopes[i]);
}
}
// If Google provider is requested and email is specified, pass OAuth
// parameter login_hint with that email.
if (provider &&
// In case the Firebase Auth version used is too old.
provider.setCustomParameters &&
// Only Google supports this parameter.
providerId == firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
// Only pass login_hint when email available.
opt_email) {
provider.setCustomParameters({
'login_hint': opt_email
});
}
// Redirect processor.
var processRedirect = function() {
app.registerPending(component.executePromiseRequest(
Expand Down
4 changes: 3 additions & 1 deletion javascript/widgets/handler/federatedlinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ firebaseui.auth.widget.handler.handleFederatedLinking = function(
// We sign in the user through the normal federated sign-in flow,
// and the callback handler will take care of linking the
// pending credential to the successfully signed in user.
// Pass the email since some OAuth providers support OAuth flow
// with a specified email.
firebaseui.auth.widget.handler.common.federatedSignIn(app, component,
providerId);
providerId, email);
});
component.render(container);
// Set current UI component.
Expand Down
60 changes: 49 additions & 11 deletions javascript/widgets/handler/federatedlinking_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function setPendingEmailCredentials() {
function testHandleFederatedLinking() {
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand All @@ -67,12 +69,30 @@ function testHandleFederatedLinking() {
}


function testHandleFederatedLinking_noLoginHint() {
// Add additional scopes to test they are properly passed to the sign-in
// method.
// As this is not google.com, no customParameters will be set.
var expectedProvider =
getExpectedProviderWithCustomParameters('github.com');
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'github.com');
assertFederatedLinkingPage(federatedAccount.getEmail());
submitForm();
testAuth.assertSignInWithRedirect([expectedProvider]);
return testAuth.process();
}


function testHandleFederatedLinking_popup_success() {
// Test successful federated linking in popup flow.
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -124,7 +144,9 @@ function testHandleFederatedLinking_popup_success_multipleClicks() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -216,7 +238,9 @@ function testHandleFederatedLinking_noPendingCredential_popup() {
function testHandleFederatedLinking_error() {
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand All @@ -237,7 +261,9 @@ function testHandleFederatedLinking_popup_recoverableError() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -268,7 +294,9 @@ function testHandleFederatedLinking_popup_userCancelled() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -299,7 +327,9 @@ function testHandleFederatedLinking_popup_unrecoverableError() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand All @@ -326,7 +356,9 @@ function testHandleFederatedLinking_popup_popupBlockedError() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -357,7 +389,9 @@ function testHandleFederatedLinking_popup_popupBlockedError_redirectError() {
app.updateConfig('signInFlow', 'popup');
// Add additional scopes to test they are properly passed to the sign-in
// method.
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -392,7 +426,9 @@ function testHandleFederatedLinking_inProcessing() {
// Add additional scopes to test they are properly passed to the sign-in
// method.
app.updateConfig('signInOptions', signInOptionsWithScopes);
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down Expand Up @@ -421,7 +457,9 @@ function testHandleFederatedLinking_inProcessing() {
function testHandleFederatedLinking_popup_cancelled() {
// Test sign in with popup flow when the popup is cancelled.
app.updateConfig('signInFlow', 'popup');
var expectedProvider = getExpectedProviderWithScopes();
var expectedProvider = getExpectedProviderWithScopes({
'login_hint': federatedAccount.getEmail()
});
setPendingEmailCredentials();
firebaseui.auth.widget.handler.handleFederatedLinking(
app, container, federatedAccount.getEmail(), 'google.com');
Expand Down
4 changes: 3 additions & 1 deletion javascript/widgets/handler/federatedsignin.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ firebaseui.auth.widget.handler.handleFederatedSignIn = function(
providerId,
// On submit.
function() {
// Pass the email since some OAuth providers support OAuth flow
// with a specified email.
firebaseui.auth.widget.handler.common.federatedSignIn(app, component,
providerId);
providerId, email);
});

component.render(container);
Expand Down
Loading