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

Webauth redirect login/callback #231

Merged
merged 4 commits into from
Nov 7, 2016
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
File renamed without changes.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/*
**/node_modules/*
/build/*
/test/*
78 changes: 49 additions & 29 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<head>
<title>Auth0.js Demo Examples</title>
<link rel="stylesheet" type="text/css" href="//cdn.auth0.com/styleguide/1/index.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/build/auth0.js"></script>
<script src="/auth0.js"></script>

<script type="text/javascript">
var authentication = new auth0.Authentication({
domain: 'mdocs.auth0.com',
redirect_uri: window.location.href,
client_id: '0HP71GSd6PuoRYJ3DXKdiXCUUdGmBbup'
});
var webAuth = new auth0.WebAuth({
domain: 'mdocs.auth0.com',
redirect_uri: 'http://localhost:3000/example',
client_id: '0HP71GSd6PuoRYJ3DXKdiXCUUdGmBbup',
response_type: 'token'
});

console.log(webAuth.parseHash());

</script>
</head>
<body class="container">
</head>
<body class="container">

<div class="row">
<div class="col-xs-12">
<input class="login-username" value="johnfoo@gmail.com" />
<input type="password" class="login-password" value="1234" />
<input type="button" class="login-db" value="login" />
<script type="text/javascript">
$('.login-db').click(function (e) {
e.preventDefault();
authentication.ro({
connection: 'tests',
username: $('.login-username').val(),
password: $('.login-password').val(),
scope: 'openid'
}, function(err, data) {
console.log(err, data);
});
});
</script>
</div>
<div class="col-xs-12">
<input class="login-username" value="johnfoo@gmail.com" />
<input type="password" class="login-password" value="1234" />
<input type="button" class="login-db" value="login" />
<script type="text/javascript">
$('.login-db').click(function (e) {
e.preventDefault();
webAuth.redirect.login({
connection: 'tests',
username: $('.login-username').val(),
password: $('.login-password').val(),
scope: 'openid'
}, function (err) {
console.log(err);
});
});
</script>
</div>
</div>
</body>

<div class="row">
<div class="col-xs-12">
<input type="button" class="renew-auth" value="renew" />
<script type="text/javascript">
$('.renew-auth').click(function (e) {
e.preventDefault();
webAuth.renewAuth({}, function (err,data) {
console.log(err, data);
});
});
</script>
</div>
</div>

</body>
</html>
40 changes: 16 additions & 24 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
var gulp = require('gulp');
var webpack = require('webpack-stream');
var gutil = require('gulp-util');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');

const webpackConfig = require("./webpack.config.js");
const webpackProdConfig = require("./webpack.prod.config.js");
var webpackConfig = require('./webpack.config.js');
var webpackProdConfig = require('./webpack.prod.config.js');

gulp.task('dev', function() {
gulp.task('build', function () {
return gulp.src('src/index.js')
.pipe(webpack( webpackConfig ))
.pipe(webpack(webpackProdConfig))
.pipe(gulp.dest('build/'));
});

gulp.task('build', function() {
return gulp.src('src/index.js')
.pipe(webpack( webpackProdConfig ))
.pipe(gulp.dest('build/'));
});
gulp.task('dev', function () {
var compiler = webpack(webpackConfig);

gulp.task("webpack-dev-server", function(callback) {
// Start a webpack-dev-server
var compiler = webpack(webpackConfig);

new WebpackDevServer(compiler, {
}).listen(8080, "localhost", function(err) {
if(err) throw new gutil.PluginError("webpack-dev-server", err);
// Server listening
gutil.log("[webpack-dev-server]", "http://localhost:8080/webpack-dev-server/index.html");

// keep the server alive or continue?
// callback();
});
});
new WebpackDevServer(compiler, {}).listen(3000, 'localhost', function (err) {
if (err) {
throw new gutil.PluginError('webpack-dev-server', err);
}
gutil.log('[webpack-dev-server]', 'http://localhost:3000/example/index.html');
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test": "mocha test/**/*.test.js",
"test:coverage": "istanbul cover _mocha -R test/**/*",
"ci": "istanbul cover _mocha --report lcovonly -R test/**/*; codecov",
"lint": "eslint ./src ./test"
"lint": "eslint ./src"
},
"repository": {
"type": "git",
Expand All @@ -37,6 +37,7 @@
"eslint-plugin-import": "1.16.0",
"expect.js": "^0.2.0",
"gulp": "^3.9.1",
"gulp-util": "^3.0.7",
"istanbul": "^0.4.5",
"mocha": "^3.1.2",
"sinon": "^1.17.6",
Expand Down
5 changes: 2 additions & 3 deletions src/authentication/db-connection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var urljoin = require('url-join');

var RequestBuilder = require('../helper/request-builder');
var objectHelper = require('../helper/object');
var assert = require('../helper/assert');
var responseHandler = require('../helper/response-handler');

function DBConnection(options) {
function DBConnection(request, options) {
this.baseOptions = options;
this.request = new RequestBuilder(options);
this.request = request;
}

DBConnection.prototype.signup = function (options, cb) {
Expand Down
27 changes: 15 additions & 12 deletions src/authentication/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var urljoin = require('url-join');

var RequestBuilder = require('../helper/request-builder');
var qsBuilder = require('../helper/qs-builder');
var qs = require('../helper/qs');
var objectHelper = require('../helper/object');
var assert = require('../helper/assert');
var responseHandler = require('../helper/response-handler');
Expand All @@ -19,21 +19,24 @@ function Authentication(options) {
_sendTelemetry: { optional: true, type: 'boolean', message: '_sendTelemetry option is not valid' },
_telemetryInfo: { optional: true, type: 'object', message: '_telemetryInfo option is not valid' }
});

options._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true;
/* eslint-enable */

this.request = new RequestBuilder(options);
this.baseOptions = options;

this.baseOptions._sendTelemetry = this.baseOptions._sendTelemetry === false ?
this.baseOptions._sendTelemetry : true;

this.baseOptions.rootUrl = 'https://' + this.baseOptions.domain;

this.passwordless = new PasswordlessAuthentication(options);
this.dbConnection = new DBConnection(options);
this.request = new RequestBuilder(this.baseOptions);

this.passwordless = new PasswordlessAuthentication(this.request, this.baseOptions);
this.dbConnection = new DBConnection(this.request, this.baseOptions);
}

Authentication.prototype.buildAuthorizeUrl = function (options) {
var params;
var qs;
var qString;

assert.check(options, {
optional: true,
Expand All @@ -52,14 +55,14 @@ Authentication.prototype.buildAuthorizeUrl = function (options) {
params.auth0Client = this.request.getTelemetryData();
}

qs = qsBuilder(params);
qString = qs.build(params);

return urljoin(this.baseOptions.rootUrl, 'authorize', '?' + qs);
return urljoin(this.baseOptions.rootUrl, 'authorize', '?' + qString);
};

Authentication.prototype.buildLogoutUrl = function (options) {
var params;
var qs;
var qString;

assert.check(options, {
optional: true,
Expand All @@ -75,9 +78,9 @@ Authentication.prototype.buildLogoutUrl = function (options) {
params.auth0Client = this.request.getTelemetryData();
}

qs = qsBuilder(params);
qString = qs.build(params);

return urljoin(this.baseOptions.rootUrl, 'v2', 'logout', '?' + qs);
return urljoin(this.baseOptions.rootUrl, 'v2', 'logout', '?' + qString);
};

Authentication.prototype.ro = function (options, cb) {
Expand Down
5 changes: 2 additions & 3 deletions src/authentication/passwordless-authentication.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var urljoin = require('url-join');

var RequestBuilder = require('../helper/request-builder');
var objectHelper = require('../helper/object');
var assert = require('../helper/assert');
var responseHandler = require('../helper/response-handler');

function PasswordlessAuthentication(options) {
function PasswordlessAuthentication(request, options) {
this.baseOptions = options;
this.request = new RequestBuilder(options);
this.request = request;
}

PasswordlessAuthentication.prototype.start = function (options, cb) {
Expand Down
25 changes: 24 additions & 1 deletion src/helper/assert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var toString = Object.prototype.toString;

function attribute(o, attr, type, text) {
if (o && typeof o[attr] !== type) {
throw new Error(text);
Expand Down Expand Up @@ -34,9 +36,30 @@ function check(o, config, attributes) {
}
}

/**
* Wrap `Array.isArray` Polyfill for IE9
* source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
*
* @param {Array} array
* @public
*/
function isArray(array) {
if (this.supportsIsArray()) {
return Array.isArray(array);
}

return toString.call(array) === '[object Array]';
}

function supportsIsArray() {
return (Array.isArray != null);
}

module.exports = {
check: check,
attribute: attribute,
variable: variable,
value: value
value: value,
isArray: isArray,
supportsIsArray: supportsIsArray
};
19 changes: 17 additions & 2 deletions src/helper/base64_url.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
var Base64 = require('Base64');

function Base64UrlEncode(str) {
function encode(str) {
return Base64.btoa(str)
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
}

module.exports = Base64UrlEncode;

function decode(str) {
// Add removed at end '='
str += Array(5 - str.length % 4).join('=');

str = str
.replace(/\-/g, '+') // Convert '-' to '+'
.replace(/_/g, '/'); // Convert '_' to '/'

return Base64.atob(str);
}

module.exports = {
encode: encode,
decode: decode
};
15 changes: 15 additions & 0 deletions src/helper/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function buildResponse(error, description) {
return {
error: error,
error_description: description
};
}

function invalidJwt(description) {
return buildResponse('invalid_token', description);
}

module.exports = {
buildResponse: buildResponse,
invalidJwt: invalidJwt
};
7 changes: 7 additions & 0 deletions src/helper/information.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function error(message) {
console.error(message);
}

module.exports = {
error: error
};
10 changes: 10 additions & 0 deletions src/helper/jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var base64Url = require('./base64_url');

function getPayload(jwt) {
var encoded = jwt && jwt.split('.')[1];
return JSON.parse(base64Url.decode(encoded));
}

module.exports = {
getPayload: getPayload
};
10 changes: 0 additions & 10 deletions src/helper/qs-builder.js

This file was deleted.

21 changes: 21 additions & 0 deletions src/helper/qs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function build(params) {
return Object.keys(params).reduce(function (arr, key) {
if (typeof params[key] !== 'undefined') {
arr.push(key + '=' + params[key]);
}
return arr;
}, []).join('&');
}

function parse(qs) {
return qs.split('&').reduce(function (prev, curr) {
var param = curr.split('=');
prev[param[0]] = param[1];
return prev;
}, {});
}

module.exports = {
build: build,
parse: parse
};
Loading