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

Polyfill functions #285

Merged
merged 10 commits into from
Jan 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
27 changes: 17 additions & 10 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
if (err) {
return this.dump(err, 'error');
}
if (data.error) {
if (data && data.error) {
return this.dump(data, 'error');
}
if (data) {
Expand Down Expand Up @@ -175,13 +175,20 @@ <h2>Console:</h2>
});

webAuth.parseHash(function(err, data) {
console.log(err,data);
if (err) {
return htmlConsole.dumpCallback(err);
}

htmlConsole.dumpCallback(null, data);
if (data) {
htmlConsole.dumpCallback(null, data);

if (data.accessToken) {
webAuth.client.userInfo(data.accessToken, htmlConsole.dumpCallback.bind(htmlConsole));
}
}

window.location.hash = '';
webAuth.client.userInfo(data.accessToken, htmlConsole.dumpCallback.bind(htmlConsole));
});

$('#clear-console').click(function () {
Expand All @@ -198,7 +205,7 @@ <h2>Console:</h2>

$('.login-db').click(function (e) {
e.preventDefault();
webAuth.redirect.login({
webAuth.redirect.loginWithCredentials({
connection: 'tests',
username: $('.login-username').val(),
password: $('.login-password').val(),
Expand All @@ -216,7 +223,7 @@ <h2>Console:</h2>
$('.popup-login-db').click(function (e) {
e.preventDefault();

webAuth.popup.login({
webAuth.popup.loginWithCredentials({
connection: 'tests',
username: $('.popup-login-username').val(),
password: $('.popup-login-password').val(),
Expand All @@ -235,28 +242,28 @@ <h2>Console:</h2>
audience: 'urn:test'
}, function(err, data) {
htmlConsole.dumpCallback.bind(htmlConsole)(err, data);
this.webAuth.client.userInfo(data.access_token, htmlConsole.dumpCallback.bind(htmlConsole));
this.webAuth.client.userInfo(data.accessToken, htmlConsole.dumpCallback.bind(htmlConsole));
});
});

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

$('.login-hosted').click(function (e) {
e.preventDefault();
webAuth.login({});
webAuth.authorize({});
});

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

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

$('.popup-login-facebook').click(function (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"license": "MIT",
"dependencies": {
"base64-js": "^1.2.0",
"idtoken-verifier": "^1.0.0",
"idtoken-verifier": "^1.0.1",
"superagent": "^3.3.1",
"url-join": "^1.1.0",
"winchan": "^0.1.4"
Expand Down
8 changes: 6 additions & 2 deletions src/helper/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function check(o, config, attributes) {
variable(o, config.type, config.message);
}
if (config.type === 'object' && attributes) {
Object.keys(attributes).forEach(function (a) { // eslint-disable-line
var keys = Object.keys(attributes);

for (var index = 0; index < keys.length; index++ ) {
var a = keys[index];
if (!attributes[a].optional || o[a]) {
if (!attributes[a].condition || attributes[a].condition(o)) {
attribute(o, a, attributes[a].type, attributes[a].message);
Expand All @@ -32,7 +35,8 @@ function check(o, config, attributes) {
}
}
}
});
}

}
}

Expand Down
10 changes: 0 additions & 10 deletions src/helper/jwt.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/helper/object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function get() {
if (!Object.assign) {
return objectAssignPolyfill;
}

return Object.assign;
}

function objectAssignPolyfill(target) {
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert first argument to object');
}

var to = Object(target);
for (var i = 1; i < arguments.length; i++) {
var nextSource = arguments[i];
if (nextSource === undefined || nextSource === null) {
continue;
}

var keysArray = Object.keys(Object(nextSource));
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
var nextKey = keysArray[nextIndex];
var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
if (desc !== undefined && desc.enumerable) {
to[nextKey] = nextSource[nextKey];
}
}
}
return to;
}

module.exports = {
get: get,
objectAssignPolyfill: objectAssignPolyfill
};
14 changes: 12 additions & 2 deletions src/helper/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-param-reassign */

var objectAssign = require('./object-assign');

function pick(object, keys) {
return keys.reduce(function (prev, key) {
if (object[key]) {
Expand All @@ -9,10 +11,18 @@ function pick(object, keys) {
}, {});
}

function objectValues(obj) {
var values = [];
for (key in obj) {
values.push(obj[key]);
}
return values;
}

function extend() {
var params = Array.from(arguments);
var params = objectValues(arguments);
params.unshift({});
return Object.assign.apply(undefined, params);
return objectAssign.get().apply(undefined, params);
}

function merge(object, keys) {
Expand Down
6 changes: 3 additions & 3 deletions src/helper/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function randomString(length) {

var random = cryptoObj.getRandomValues(bytes);

random.forEach(function (c) {
result.push(charset[c % charset.length]);
});
for (var a = 0; a < random.length; a++) {
result.push(charset[random[a] % charset.length]);
}

return result.join('');
}
Expand Down
10 changes: 7 additions & 3 deletions src/helper/request-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ RequestBuilder.prototype.setCommonConfiguration = function (ongoingRequest, opti

var headers = this.headers;
ongoingRequest = ongoingRequest.set('Content-Type', 'application/json');
Object.keys(this.headers).forEach(function (header) {
ongoingRequest = ongoingRequest.set(header, headers[header]);
});

var keys = Object.keys(this.headers);

for (var a = 0; a < keys.length; a++) {
ongoingRequest = ongoingRequest.set(keys[a], headers[keys[a]]);
}

if (this._sendTelemetry) {
ongoingRequest = ongoingRequest.set('Auth0-Client', this.getTelemetryData());
}
Expand Down
1 change: 0 additions & 1 deletion src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var IdTokenVerifier = require('idtoken-verifier');

var assert = require('../helper/assert');
var error = require('../helper/error');
var jwt = require('../helper/jwt');
var qs = require('../helper/qs');
var windowHelper = require('../helper/window');
var objectHelper = require('../helper/object');
Expand Down
4 changes: 0 additions & 4 deletions src/web-auth/redirect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var UsernamePassword = require('./username-password');
var TransactionManager = require('./transaction-manager');
var objectHelper = require('../helper/object');
var Warn = require('../helper/warn');
var assert = require('../helper/assert');
Expand All @@ -8,7 +7,6 @@ function Redirect(client, options) {
this.baseOptions = options;
this.client = client;

this.transactionManager = new TransactionManager(this.baseOptions.transaction);
this.warn = new Warn({
disableWarnings: !!options._disableDeprecationWarnings
});
Expand Down Expand Up @@ -41,8 +39,6 @@ Redirect.prototype.loginWithCredentials = function (options, cb) {
responseType: { type: 'string', message: 'responseType option is required' }
});

params = this.transactionManager.process(params);

usernamePassword = new UsernamePassword(this.baseOptions);
return usernamePassword.login(params, function (err, data) {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions src/web-auth/transaction-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TransactionManager.prototype.process = function (options) {
return options;
}

transaction = this.generateTransaction(options.appState, options.state);
transaction = this.generateTransaction(options.appState, options.state, options.nonce);

options.state = transaction.state;

Expand All @@ -31,12 +31,12 @@ TransactionManager.prototype.process = function (options) {
return options;
};

TransactionManager.prototype.generateTransaction = function (appState, state) {
TransactionManager.prototype.generateTransaction = function (appState, state, nonce) {
var transaction;
var nonce;

transaction = state || random.randomString(this.keyLength);
nonce = random.randomString(this.keyLength);
nonce = nonce || random.randomString(this.keyLength);

storage.setItem(this.namespace + transaction, {
nonce:nonce,
Expand Down
37 changes: 37 additions & 0 deletions test/helper/object.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var expect = require('expect.js');
var stub = require('sinon').stub;

var objectAssign = require('../../src/helper/object-assign');
var objectHelper = require('../../src/helper/object');

describe('helpers', function () {
Expand Down Expand Up @@ -73,6 +75,41 @@ describe('helpers', function () {
});
});

it('shold merge objects attributes with polyfill', function () {

stub(objectAssign, 'get', function() {
return objectAssign.objectAssignPolyfill;
});

var object1 = {
attr1: 'attribute_1',
attr2: 'attribute_2'
};

var object2 = {
attr3: 'attribute_3'
};

var newObject = objectHelper.extend(object1, object2);

expect(newObject).to.eql({
attr1: 'attribute_1',
attr2: 'attribute_2',
attr3: 'attribute_3'
});

expect(object1).to.eql({
attr1: 'attribute_1',
attr2: 'attribute_2'
});

expect(object2).to.eql({
attr3: 'attribute_3'
});

objectAssign.get.restore();
});

it('shold merge objects attributes and override the first object ones', function () {
var object1 = {
attr1: 'attribute_1',
Expand Down
6 changes: 1 addition & 5 deletions test/web-auth/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ describe('auth0.WebAuth.redirect', function () {
response_type: 'id_token',
scope: 'openid',
tenant: 'me',
username: 'me@example.com',
state: 'ABCDEFGHIJ',
nonce: 'ABCDEFGHIJ'
username: 'me@example.com'
},
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -176,7 +174,6 @@ describe('auth0.WebAuth.redirect', function () {
response_type: 'token',
scope: 'openid',
tenant: 'me',
state: 'ABCDEFGHIJ',
username: 'me@example.com'
},
headers: {
Expand Down Expand Up @@ -261,7 +258,6 @@ describe('auth0.WebAuth.redirect', function () {
client_id: '...',
connection: 'the_connection',
password: '123456',
state: 'ABCDEFGHIJ',
redirect_uri: 'http://page.com/callback',
response_type: 'token',
scope: 'openid',
Expand Down