Skip to content

Commit

Permalink
Merge pull request #306 from auth0/fix-passwordless-params
Browse files Browse the repository at this point in the history
Passwordless start: map params to authParams and fix tests
  • Loading branch information
glena authored Jan 16, 2017
2 parents ead2e64 + def26fc commit 49d42bb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/authentication/passwordless-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ PasswordlessAuthentication.prototype.start = function (options, cb) {
phoneNumber: { required: true, type: 'string', message: 'phoneNumber option is required',
condition: function (o) { return o.type === 'sms'; } },
email: { required: true, type: 'string', message: 'email option is required',
condition: function (o) { return o.type === 'email'; } }
condition: function (o) { return o.type === 'email'; } },
authParams: { optional: true, type: 'object', message: 'authParams option is required' }
});
/* eslint-enable */

Expand All @@ -93,10 +94,33 @@ PasswordlessAuthentication.prototype.start = function (options, cb) {

url = urljoin(this.baseOptions.rootUrl, 'passwordless', 'start');

body = objectHelper.merge(this.baseOptions, ['clientID'])
.with(cleanOption);
body = objectHelper.merge(this.baseOptions, [
'clientID',
'responseType',
'redirectUri',
'scope'
]).with(cleanOption);

if (body.scope) {
body.authParams = body.authParams || {};
body.authParams.scope = body.scope;
}

if (options.type === 'email' && body.redirectUri) {
body.authParams = body.authParams || {};
body.authParams.redirect_uri = body.redirectUri;
}

if (options.type === 'email' && body.responseType) {
body.authParams = body.authParams || {};
body.authParams.response_type = body.responseType;
}

delete body.redirectUri;
delete body.responseType;
delete body.scope;

body = objectHelper.toSnakeCase(body, ['auth0Client']);
body = objectHelper.toSnakeCase(body, ['auth0Client', 'authParams']);

return this.request
.post(url)
Expand Down
92 changes: 92 additions & 0 deletions test/authentication/passwordless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,98 @@ describe('auth0.authentication', function () {
});
});

context('passwordless start', function () {
before(function () {
this.auth0 = new Authentication({
domain: 'me.auth0.com',
clientID: '...',
redirectUri: 'http://page.com/callback',
responseType: 'code',
_sendTelemetry: false
});
});

afterEach(function () {
request.post.restore();
});

it('should call passwordless start', function (done) {
stub(request, 'post', function (url) {
expect(url).to.be('https://me.auth0.com/passwordless/start');
return new RequestMock({
body: {
client_id: '...',
connection: 'the_connection',
email: '[email protected]',
authParams: {
redirect_uri: 'http://page.com/callback',
response_type: 'code'
}
},
headers: {
'Content-Type': 'application/json'
},
cb: function (cb) {
cb(null, {
body: {}
});
}
});
});

this.auth0.passwordless.start({
connection: 'the_connection',
email: '[email protected]',
type: 'email'
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({

});
done();
});
});

it('should call passwordless start with authParams', function (done) {
stub(request, 'post', function (url) {
expect(url).to.be('https://me.auth0.com/passwordless/start');
return new RequestMock({
body: {
client_id: '...',
connection: 'the_connection',
email: '[email protected]',
authParams: {
scope: 'openid email',
redirect_uri: 'http://page.com/callback',
response_type: 'code'
}
},
headers: {
'Content-Type': 'application/json'
},
cb: function (cb) {
cb(null, {
body: {}
});
}
});
});

this.auth0.passwordless.start({
connection: 'the_connection',
email: '[email protected]',
type: 'email',
scope: 'openid email'
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({

});
done();
});
});
});

context('passwordless verify', function () {
before(function () {
this.auth0 = new Authentication({
Expand Down
6 changes: 5 additions & 1 deletion test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,11 @@ describe('auth0.WebAuth', function () {
body: {
client_id: '...',
connection: 'the_connection',
email: '[email protected]'
email: '[email protected]',
authParams: {
redirect_uri: 'http://page.com/callback',
response_type: 'code'
}
},
headers: {
'Content-Type': 'application/json'
Expand Down

0 comments on commit 49d42bb

Please sign in to comment.