Skip to content
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

Fix passwordless #315

Merged
merged 3 commits into from
Jan 17, 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
45 changes: 41 additions & 4 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ <h2>Login with database connection:</h2>
<input type="button" class="login-db" value="login" />
</div>

<div>
<h2>Login with passwordless connection:</h2>
<div>
<input class="passwordless-login-username" value="" />
<input type="button" class="passwordless-login-db" value="login" />
</div>
<div>
<input class="passwordless-login-code" value="Code" />
<input type="button" class="passwordless-login-verify" value="verify" />
</div>
</div>

<div>
<h2>Login with database connection (popup):</h2>
<input class="popup-login-username" value="johnfoo@gmail.com" />
Expand Down Expand Up @@ -167,11 +179,18 @@ <h2>Console:</h2>
});

var webAuth = new auth0.WebAuth({
domain: 'auth0-tests-auth0js.auth0.com',
domain: 'brucke.auth0.com',
redirectUri: 'http://localhost:3000/example',
clientID: '3GGMIEuBPZ28lb6NBDNARaEZisqFakAs',
audience: 'https://auth0-tests-auth0js.auth0.com/userinfo',
responseType: 'token id_token'
clientID: 'k5u3o2fiAA8XweXEEX604KCwCjzjtMU6',
audience: 'https://brucke.auth0.com/userinfo',
responseType: 'token'
});

var webAuthPasswordless = new auth0.WebAuth({
domain: 'brucke.auth0.com',
redirectUri: 'http://localhost:3000/example',
clientID: 'k5u3o2fiAA8XweXEEX604KCwCjzjtMU6',
responseType: 'token'
});

webAuth.parseHash(function(err, data) {
Expand Down Expand Up @@ -213,6 +232,24 @@ <h2>Console:</h2>
}, htmlConsole.dumpCallback.bind(htmlConsole));
});

$('.passwordless-login-verify').click(function (e) {
e.preventDefault();
webAuthPasswordless.passwordlessVerify({
connection: 'email',
email: $('.passwordless-login-username').val(),
verificationCode: $('.passwordless-login-code').val()
}, htmlConsole.dumpCallback.bind(htmlConsole));
});

$('.passwordless-login-db').click(function (e) {
e.preventDefault();
webAuthPasswordless.passwordlessStart({
connection: 'email',
email: $('.passwordless-login-username').val(),
send: 'code'
}, htmlConsole.dumpCallback.bind(htmlConsole));
});

var popupHandler;

$('.popup-login-db-preload').click(function (e) {
Expand Down
53 changes: 18 additions & 35 deletions src/authentication/passwordless-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@ PasswordlessAuthentication.prototype.buildVerifyUrl = function (options) {
/* eslint-disable */
assert.check(options, { type: 'object', message: 'options parameter is not valid' }, {
connection: { type: 'string', message: 'connection option is required' },
type: { type: 'string', message: 'type option is required', values: ['sms', 'email'],
value_message: 'type is not valid ([email,sms])' },
verificationCode: { type: 'string', message: 'verificationCode option is required' },
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'; } }
phoneNumber: { optional: false, type: 'string', message: 'phoneNumber option is required',
condition: function (o) { return !o.email; } },
email: { optional: false, type: 'string', message: 'email option is required',
condition: function (o) { return !o.phoneNumber; } }
});
/* eslint-enable */

assert.check(options, {
optional: true,
type: 'object',
message: 'options parameter is not valid'
});

params = objectHelper.merge(this.baseOptions, [
'clientID',
'responseType',
Expand All @@ -49,8 +41,6 @@ PasswordlessAuthentication.prototype.buildVerifyUrl = function (options) {
'audience'
]).with(options);

params = objectHelper.blacklist(params, ['type']);

// eslint-disable-next-line
if (this.baseOptions._sendTelemetry) {
params.auth0Client = this.request.getTelemetryData();
Expand All @@ -73,45 +63,42 @@ PasswordlessAuthentication.prototype.buildVerifyUrl = function (options) {
PasswordlessAuthentication.prototype.start = function (options, cb) {
var url;
var body;
var cleanOption;

/* eslint-disable */
assert.check(options, { type: 'object', message: 'options parameter is not valid' }, {
connection: { type: 'string', message: 'connection option is required' },
type: { type: 'string', message: 'type option is required', values: ['sms', 'email'],
value_message: 'type is not valid ([email,sms])' },
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'; } },
send: { type: 'string', message: 'send option is required', values: ['link', 'code'],
value_message: 'send is not valid ([link, code])' },
phoneNumber: { optional: true, type: 'string', message: 'phoneNumber option is required',
condition: function (o) { return o.send === 'code' || !o.email; } },
email: { optional: true, type: 'string', message: 'email option is required',
condition: function (o) { return o.send === 'link' || !o.phoneNumber; } },
authParams: { optional: true, type: 'object', message: 'authParams option is required' }
});
/* eslint-enable */

assert.check(cb, { type: 'function', message: 'cb parameter is not valid' });

cleanOption = objectHelper.blacklist(options, ['type']);

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

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

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

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

if (options.type === 'email' && body.responseType) {
if (body.responseType) {
body.authParams = body.authParams || {};
body.authParams.response_type = body.responseType;
}
Expand Down Expand Up @@ -142,21 +129,17 @@ PasswordlessAuthentication.prototype.verify = function (options, cb) {
/* eslint-disable */
assert.check(options, { type: 'object', message: 'options parameter is not valid' }, {
connection: { type: 'string', message: 'connection option is required' },
type: { type: 'string', message: 'type option is required', values: ['sms', 'email'],
value_message: 'type is not valid ([email,sms])' },
verificationCode: { type: 'string', message: 'verificationCode option is required' },
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'; } }
phoneNumber: { optional: false, type: 'string', message: 'phoneNumber option is required',
condition: function (o) { return !o.email; } },
email: { optional: false, type: 'string', message: 'email option is required',
condition: function (o) { return !o.phoneNumber; } }
});
/* eslint-enable */

assert.check(cb, { type: 'function', message: 'cb parameter is not valid' });

cleanOption = objectHelper.blacklist(options, ['type']);

cleanOption = objectHelper.toSnakeCase(cleanOption, ['auth0Client']);
cleanOption = objectHelper.toSnakeCase(options, ['auth0Client']);

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

Expand Down
54 changes: 18 additions & 36 deletions test/authentication/passwordless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,32 @@ describe('auth0.authentication', function () {
});
});

it('should check that options.type is passed', function () {
it('should check that options.send is passed', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.start({ connection: 'bla' });
}).to.throwException(function (e) {
expect(e.message).to.be('type option is required');
expect(e.message).to.be('send option is required');
});
});

it('should check that options.type is valid', function () {
it('should check that options.send is valid', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.start({ connection: 'bla', type: 'blabla' });
_this.auth0.passwordless.start({ connection: 'bla', send: 'blabla' });
}).to.throwException(function (e) {
expect(e.message).to.be('type is not valid ([email,sms])');
expect(e.message).to.be('send is not valid ([link, code])');
});
});

it('should check that cb is valid', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.start({ connection: 'bla', type: 'email', email: 'me@example.com' });
_this.auth0.passwordless.start({ connection: 'bla', send: 'code', email: 'me@example.com' });
}).to.throwException(function (e) {
expect(e.message).to.be('cb parameter is not valid');
});
});

it('should check that email is sent', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.start({ connection: 'bla', type: 'email' }, function () {});
}).to.throwException(function (e) {
expect(e.message).to.be('email option is required');
});
});

it('should check that phoneNumber is sent', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.start({ connection: 'bla', type: 'sms' }, function () {});
}).to.throwException(function (e) {
expect(e.message).to.be('phoneNumber option is required');
});
});
});

context('passwordless verify options', function () {
Expand Down Expand Up @@ -117,14 +99,14 @@ describe('auth0.authentication', function () {
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla' });
}).to.throwException(function (e) {
expect(e.message).to.be('type option is required');
expect(e.message).to.be('verificationCode option is required');
});
});

it('should check that options.verificationCode is passed', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla', type: 'email' });
_this.auth0.passwordless.verify({ connection: 'bla', send: 'code' });
}).to.throwException(function (e) {
expect(e.message).to.be('verificationCode option is required');
});
Expand All @@ -133,16 +115,16 @@ describe('auth0.authentication', function () {
it('should check that options.type is valid', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla', type: 'blabla', verificationCode: 'asdfasd' });
_this.auth0.passwordless.verify({ connection: 'bla', verificationCode: 'asdfasd' });
}).to.throwException(function (e) {
expect(e.message).to.be('type is not valid ([email,sms])');
expect(e.message).to.be('phoneNumber option is required');
});
});

it('should check that cb is valid', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla', type: 'email', verificationCode: 'asdfasd', email: 'me@example.com' });
_this.auth0.passwordless.verify({ connection: 'bla', send: 'link', verificationCode: 'asdfasd', email: 'me@example.com' });
}).to.throwException(function (e) {
expect(e.message).to.be('cb parameter is not valid');
});
Expand All @@ -151,16 +133,16 @@ describe('auth0.authentication', function () {
it('should check that email is sent', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla', type: 'email', verificationCode: 'asdfasd' }, function () {});
_this.auth0.passwordless.verify({ connection: 'bla', send: 'code', verificationCode: 'asdfasd' }, function () {});
}).to.throwException(function (e) {
expect(e.message).to.be('email option is required');
expect(e.message).to.be('phoneNumber option is required');
});
});

it('should check that phoneNumber is sent', function () {
var _this = this;
expect(function () {
_this.auth0.passwordless.verify({ connection: 'bla', type: 'sms', verificationCode: 'asdfasd' }, function () {});
_this.auth0.passwordless.verify({ connection: 'bla', send: 'code', verificationCode: 'asdfasd' }, function () {});
}).to.throwException(function (e) {
expect(e.message).to.be('phoneNumber option is required');
});
Expand Down Expand Up @@ -190,6 +172,7 @@ describe('auth0.authentication', function () {
client_id: '...',
connection: 'the_connection',
email: 'me@example.com',
send: 'link',
authParams: {
redirect_uri: 'http://page.com/callback',
response_type: 'code'
Expand All @@ -209,7 +192,7 @@ describe('auth0.authentication', function () {
this.auth0.passwordless.start({
connection: 'the_connection',
email: 'me@example.com',
type: 'email'
send: 'link'
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
Expand All @@ -227,6 +210,7 @@ describe('auth0.authentication', function () {
client_id: '...',
connection: 'the_connection',
email: 'me@example.com',
send: 'code',
authParams: {
scope: 'openid email',
redirect_uri: 'http://page.com/callback',
Expand All @@ -247,7 +231,7 @@ describe('auth0.authentication', function () {
this.auth0.passwordless.start({
connection: 'the_connection',
email: 'me@example.com',
type: 'email',
send: 'code',
scope: 'openid email'
}, function (err, data) {
expect(err).to.be(null);
Expand Down Expand Up @@ -297,7 +281,6 @@ describe('auth0.authentication', function () {
this.auth0.passwordless.verify({
connection: 'the_connection',
phoneNumber: '123456',
type: 'sms',
verificationCode: 'abc'
}, function (err, data) {
expect(err).to.be(null);
Expand Down Expand Up @@ -331,7 +314,6 @@ describe('auth0.authentication', function () {
this.auth0.passwordless.verify({
connection: 'the_connection',
email: 'me@example.com',
type: 'email',
verificationCode: 'abc'
}, function (err, data) {
expect(err).to.be(null);
Expand Down
2 changes: 1 addition & 1 deletion test/helper/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe('helpers', function () {
});
});

it.only('should not breack the string', function () {
it('should not breack the string', function () {
var object = "some random string";

var newObject = objectHelper.toCamelCase(object);
Expand Down
3 changes: 0 additions & 3 deletions test/web-auth/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ describe('auth0.WebAuth.popup', function () {
});

this.auth0.popup.passwordlessVerify({
type: 'sms',
connection: 'the_connection',
phoneNumber: '+5491178786555',
verificationCode: '123'
Expand Down Expand Up @@ -298,7 +297,6 @@ describe('auth0.WebAuth.popup', function () {
});

this.auth0.popup.passwordlessVerify({
type: 'email',
connection: 'the_connection',
email: 'test@example.com',
verificationCode: '123'
Expand Down Expand Up @@ -339,7 +337,6 @@ describe('auth0.WebAuth.popup', function () {
});

this.auth0.popup.passwordlessVerify({
type: 'sms',
connection: 'the_connection',
phoneNumber: '+5491178786555',
verificationCode: '123'
Expand Down
Loading