Skip to content

Commit

Permalink
Merge pull request #240 from auth0/change-webauth-structure
Browse files Browse the repository at this point in the history
Change webauth structure + Allow to abort requests
  • Loading branch information
hzalaz committed Nov 17, 2016
2 parents ad43c5e + a31a58c commit 5c4bb6c
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 100 deletions.
70 changes: 54 additions & 16 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}
pre {
margin: 0 0 10px 0;
min-height: 300px;
}
code {
min-height: 300px;
Expand Down Expand Up @@ -39,15 +40,40 @@
<script type="text/javascript">
function HTMLConsole(options) {
this.ele = $(options.selector);
this.data = [];
var _this = this;
var data;
if (data = localStorage.getItem("consoleData")) {
data = JSON.parse(data);
data.forEach(function(d){
_this.dumpCallback(d.error ? d : null, d.error ? null : d);
});
}
}

HTMLConsole.prototype.clear = function () {
this.ele.html('');
localStorage.removeItem("consoleData");
};

HTMLConsole.prototype.dumpCallback = function (err, data) {
if (err) {
return this.dump(err, 'error');
}
if (data.error) {
return this.dump(data, 'error');
}
if (data) {
return this.dump(data);
}
};

HTMLConsole.prototype.dump = function (o, className) {
className = className || '';

this.data.push(o);
localStorage.setItem("consoleData", JSON.stringify(this.data));

function replacer(key, value) {
if (typeof value === 'object') {
return value;
Expand Down Expand Up @@ -84,6 +110,13 @@ <h2>Login with database connection:</h2>
<input type="button" class="login-db" value="login" />
</div>

<div>
<h2>Login with database connection (client login):</h2>
<input class="client-login-username" value="johnfoo@gmail.com" />
<input type="password" class="client-login-password" value="1234" />
<input type="button" class="client-login-db" value="login" />
</div>

<div>
<h2>Login with social connections:</h2>
<input type="button" class="login-facebook" value="Facebook" />
Expand Down Expand Up @@ -123,18 +156,20 @@ <h2>Console:</h2>

var hash = webAuth.parseHash();
if (hash) {
htmlConsole.dump(hash);
htmlConsole.dumpCallback(hash.error ? hash : null, hash.error ? null : hash);
window.location.hash = '';
}

$('#clear-console').click(function () {
$('#clear-console').removeClass('icon-budicon-498');
$('#clear-console').addClass('icon-budicon-495');

htmlConsole.clear();

setTimeout(function () {
$('#clear-console').removeClass('icon-budicon-495');
$('#clear-console').addClass('icon-budicon-498');
}, 250)
}, 250);
});

$('.login-db').click(function (e) {
Expand All @@ -144,40 +179,43 @@ <h2>Console:</h2>
username: $('.login-username').val(),
password: $('.login-password').val(),
scope: 'openid'
}, function (err) {
htmlConsole.dump({err: err, data: data}, 'error');
});
}, htmlConsole.dumpCallback.bind(htmlConsole));
});

$('.client-login-db').click(function (e) {
e.preventDefault();
webAuth.client.login({
connection: 'tests',
username: $('.client-login-username').val(),
password: $('.client-login-password').val(),
scope: 'openid'
}, htmlConsole.dumpCallback.bind(htmlConsole));
});

$('.login-facebook').click(function (e) {
e.preventDefault();
webAuth.redirect.authorize({ connection: 'facebook' });
webAuth.login({ connection: 'facebook' });
});

$('.login-twitter').click(function (e) {
e.preventDefault();
webAuth.redirect.authorize({ connection: 'twitter' });
webAuth.login({ connection: 'twitter' });
});

$('.login-github').click(function (e) {
e.preventDefault();
webAuth.redirect.authorize({ connection: 'github' });
webAuth.login({ connection: 'github' });
});

$('.logout').click(function (e) {
e.preventDefault();
webAuth.redirect.logout({ returnTo: 'http://localhost:3000/example' });
webAuth.logout({ returnTo: 'http://localhost:3000/example' });
});

$('.renew-auth').click(function (e) {
e.preventDefault();
webAuth.renewAuth({usePostMessage: true, scope:'openid', audience:'https://auth0-tests-auth0js.auth0.com/api/v2/', redirectURI: 'http://localhost:3000/example/callback.html'}, function (err, data) {
if (err) {
htmlConsole.dump(err, 'error');
} else {
htmlConsole.dump(data);
}
}
webAuth.renewAuth({usePostMessage: true, scope:'openid', audience:'https://auth0-tests-auth0js.auth0.com/ api/v2/', redirectURI: 'http://localhost:3000/example/callback.html'},
htmlConsole.dumpCallback.bind(htmlConsole)
);
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/authentication/db-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DBConnection.prototype.signup = function (options, cb) {

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

this.request
return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
Expand All @@ -52,7 +52,7 @@ DBConnection.prototype.changePassword = function (options, cb) {

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

this.request
return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
Expand Down
57 changes: 51 additions & 6 deletions src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,49 @@ Authentication.prototype.buildLogoutUrl = function (options) {
return urljoin(this.baseOptions.rootUrl, 'v2', 'logout', '?' + qString);
};

Authentication.prototype.ro = function (options, cb) {
Authentication.prototype.login = function (options, cb) {
assert.check(options, { type: 'object', message: 'options parameter is not valid' }, {
clientID: { optional: true, type: 'string', message: 'clientID option is required' },
username: { optional: true, type: 'string', message: 'username option is required' },
password: { optional: true, type: 'string', message: 'password option is required' },
scope: { optional: true, type: 'string', message: 'scope option is required' },
audience: { optional: true, type: 'string', message: 'audience option is required' }
});
assert.check(cb, { type: 'function', message: 'cb parameter is not valid' });

options.grantType = 'password';

return this.oauthToken(options, cb);
};

Authentication.prototype.oauthToken = function (options, cb) {
var url;
var body;

assert.check(options, { type: 'object', message: 'options parameter is not valid' }, {
grantType: { optional: true, type: 'string', message: 'grantType option is required' }
});
assert.check(cb, { type: 'function', message: 'cb parameter is not valid' });

url = urljoin(this.baseOptions.rootUrl, 'oauth', 'token');

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

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

body.grant_type = body.grant_type || 'password';

return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
};

Authentication.prototype.loginWithResourceOwner = function (options, cb) {
var url;
var body;

Expand All @@ -106,14 +148,17 @@ Authentication.prototype.ro = function (options, cb) {

url = urljoin(this.baseOptions.rootUrl, 'oauth', 'ro');

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

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

body.grant_type = body.grant_type || 'password';

this.request
return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
Expand All @@ -127,7 +172,7 @@ Authentication.prototype.userInfo = function (accessToken, cb) {

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

this.request
return this.request
.get(url)
.set('Authorization', 'Bearer ' + accessToken)
.end(responseHandler(cb));
Expand All @@ -149,7 +194,7 @@ Authentication.prototype.delegation = function (options, cb) {

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

this.request
return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
Expand Down
4 changes: 2 additions & 2 deletions src/authentication/passwordless-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ PasswordlessAuthentication.prototype.start = function (options, cb) {

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

this.request
return this.request
.post(url)
.send(body)
.end(responseHandler(cb));
Expand Down Expand Up @@ -114,7 +114,7 @@ PasswordlessAuthentication.prototype.verify = function (options, cb) {

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

this.request
return this.request
.post(url)
.send(cleanOption)
.end(responseHandler(cb));
Expand Down
59 changes: 56 additions & 3 deletions src/helper/request-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,59 @@ var request = require('superagent');
var base64Url = require('./base64_url');
var version = require('../version');

// ------------------------------------------------ RequestWrapper

function RequestWrapper(req) {
this.request = req;
this.method = req.method;
this.url = req.url;
this.body = req._data;
this.headers = req._header;
}

RequestWrapper.prototype.abort = function () {
this.request.abort();
};

RequestWrapper.prototype.getMethod = function () {
return this.method;
};

RequestWrapper.prototype.getBody = function () {
return this.body;
};

RequestWrapper.prototype.getUrl = function () {
return this.url;
};

RequestWrapper.prototype.getHeaders = function () {
return this.headers;
};

// ------------------------------------------------ RequestObj

function RequestObj(req) {
this.request = req;
}

RequestObj.prototype.set = function (key, value) {
this.request = this.request.set(key, value);
return this;
};

RequestObj.prototype.send = function (body) {
this.request = this.request.send(body);
return this;
};

RequestObj.prototype.end = function (cb) {
this.request = this.request.end(cb);
return new RequestWrapper(this.request);
};

// ------------------------------------------------ RequestBuilder

function RequestBuilder(options) {
this._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true;
this._telemetryInfo = options._telemetryInfo || null;
Expand All @@ -28,15 +81,15 @@ RequestBuilder.prototype.getTelemetryData = function () {
};

RequestBuilder.prototype.get = function (url) {
return this.setCommonConfiguration(request.get(url));
return new RequestObj(this.setCommonConfiguration(request.get(url)));
};

RequestBuilder.prototype.post = function (url) {
return this.setCommonConfiguration(request.post(url));
return new RequestObj(this.setCommonConfiguration(request.post(url)));
};

RequestBuilder.prototype.patch = function (url) {
return this.setCommonConfiguration(request.patch(url));
return new RequestObj(this.setCommonConfiguration(request.patch(url)));
};

module.exports = RequestBuilder;
23 changes: 22 additions & 1 deletion src/helper/response-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
function wrapCallback(cb) {
return function (err, data) {

if (err) {
return cb(err);
var data = {
original: err
};

if (err.response && err.response.statusCode) {
data.status_code = err.response.statusCode;
}

if (err.response && err.response.statusText) {
data.status_text = err.response.statusText;
}

if (err.response && err.response.body) {
err = err.response.body;
}

data.code = err.error || err.code || err.error_code || null;
data.description = err.error_description || err.description || err.error || null;
data.name = err.name || null;

return cb(data);
}

return cb(null, data.body || data.text);
Expand Down
Loading

0 comments on commit 5c4bb6c

Please sign in to comment.