-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters