mapbox
Version:
interface to mapbox services
33 lines (27 loc) • 737 B
JavaScript
;
var b64 = require('rest/util/base64');
/**
* Access tokens actually are data, and using them we can derive
* a user's username. This method attempts to do just that,
* decoding the part of the token after the first `.` into
* a username.
*
* @private
* @param {string} token an access token
* @return {string} username
*/
function getUser(token) {
var data = token.split('.')[1];
if (!data) return null;
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 {
return JSON.parse(b64.decode(data)).u;
} catch(err) {
return null;
}
}
module.exports = getUser;