Skip to content

Commit

Permalink
Merge 06182da into 3b4d9ed
Browse files Browse the repository at this point in the history
  • Loading branch information
willwhite committed Jul 29, 2015
2 parents 3b4d9ed + 06182da commit 6c9c9d4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
20 changes: 20 additions & 0 deletions lib/get_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

function getUser(token) {
var data = token.split('.')[1];
data = data.replace(/-/g, '+').replace(/_/g, '/');

var mod = data.length % 4;
if (mod === 2) data += '==';
if (mod === 3) data += '=';
if (mod === 1 || mod > 3) return null;

try {
data = (new Buffer(data, 'base64')).toString('utf8');
return JSON.parse(data).u;
} catch(err) {
return null;
}
}

module.exports = getUser;
6 changes: 5 additions & 1 deletion lib/make_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

var invariant = require('invariant');
var constants = require('./constants');
var getUser = require('./get_user');

function makeService(name) {

function service(accessToken, options) {
this.name = name;

invariant(typeof accessToken === 'string',
'accessToken required to instantiate MapboxDirections');
'accessToken required to instantiate Mapbox client');

this.user = getUser(accessToken);
invariant(!!this.user, 'could not determine user from provided accessToken');

this.accessToken = accessToken;
this.endpoint = constants.DEFAULT_ENDPOINT;
Expand Down
37 changes: 37 additions & 0 deletions test/get_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint no-shadow: 0 */
'use strict';

var test = require('tap').test;
var getUser = require('../lib/get_user');

test('getUser', function(t) {
t.test('public token', function(assert) {
var token = 'pk.eyJ1Ijoid29yYmx5IiwiYSI6ImQzMjFkZWRkN2IzNzc5M2MzZDgyNTIzZTRhM2E5MDE3In0.IIrNhFTaOiW-Ykw_J-yQbg';
assert.equal(getUser(token), 'worbly', 'success');
assert.end();
});

t.test('secret token', function(assert) {
var token = 'sk.eyJ1Ijoid29yYmx5IiwiYSI6ImQwNTg3OGU2MWI5NTI5MjIyNmI1YzNhNWE4ZGFlMmFiIn0.-47f43O4Cz5-vEd0gXzJ3w';
assert.equal(getUser(token), 'worbly', 'success');
assert.end();
});

t.test('token padding', function(assert) {
var token = 'sk.eyJ1Ijoid29yYmx5IiwiYSI6ImQwNTg3OGU2MWI5NTI5MjIyNmI1YzNhNWE4ZGFlMmFiIn0=.-47f43O4Cz5-vEd0gXzJ3w';
assert.equal(getUser(token), 'worbly', 'success');
token = 'sk.eyJ1Ijoid29yYmx5IiwiYSI6ImQwNTg3OGU2MWI5NTI5MjIyNmI1YzNhNWE4ZGFlMmFiIn0===.-47f43O4Cz5-vEd0gXzJ3w';
assert.equal(getUser(token), 'worbly', 'success');
assert.end();
});

t.test('bogus token', function(assert) {
var token = 'sk.eyJ1Ijoid29yYmx5IiwiYSI6ImQwNTg3OGU2MWI5NTI5MjIyNmI1YzNhNWE4ZGFlMmFiIn0==.-47f43O4Cz5-vEd0gXzJ3w';
assert.notOk(getUser(token), 'bad length success');
token = 'sk.eyJ1Ijoid29yYmx5IiwiYSI6ImQwNTg3OGU2MWI5NTI5MjIyNmI1YzNhNWE4ZGFlMmFiI12.-47f43O4Cz5-vEd0gXzJ3w';
assert.notOk(getUser(token), 'cannot parse success');
assert.end();
});

t.end();
});
12 changes: 7 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ test('prerequisites', function(t) {
t.end();
});

var deadToken = 'pk.eyJ1Ijoid29yYmx5IiwiYSI6ImQzMjFkZWRkN2IzNzc5M2MzZDgyNTIzZTRhM2E5MDE3In0.IIrNhFTaOiW-Ykw_J-yQbg';

test('MapboxClient', function(t) {
t.throws(function() {
var client = new MapboxClient();
t.notOk(client);
}, /accessToken required to instantiate MapboxClient/);
var client = new MapboxClient('token');
var client = new MapboxClient(deadToken);
t.ok(client);
t.equal(client.accessToken, 'token');
t.equal(client.accessToken, deadToken);
t.end();
});

test('MapboxClient - custom endpoint', function(t) {
t.throws(function() {
var client = new MapboxClient('foo', 1);
var client = new MapboxClient(deadToken, 1);
t.notOk(client);
}, /options/);
t.throws(function() {
var client = new MapboxClient('foo', { endpoint: 1 });
var client = new MapboxClient(deadToken, { endpoint: 1 });
t.notOk(client);
}, /endpoint/);
var customClient = new MapboxClient('foo', { endpoint: 'foo.bar' });
var customClient = new MapboxClient(deadToken, { endpoint: 'foo.bar' });
t.equal(customClient.endpoint, 'foo.bar', 'receives an endpoint from options');
t.end();
});

0 comments on commit 6c9c9d4

Please sign in to comment.