Comparing version 0.5.0 to 0.6.0
@@ -46,2 +46,42 @@ var request = require('request'); | ||
/** | ||
* Gets user profile using a user's access token (not an app access token). | ||
* | ||
* @param {Object} options Which should contain: | ||
* * userAccessToken: User's access token | ||
* whose data is going to be fetched. | ||
* * domain: Auth0 Tenant domain | ||
* (ie. my-domain.auth0.com | ||
* @param {function} cb Callback with signature function(err, profile) | ||
*/ | ||
Client.getUserInfo = function(options, cb) { | ||
if (!options || !options.userAccessToken || !options.domain) { | ||
throw new Error('Options object should contain userAccessToken and ' + | ||
'domain attributes'); | ||
} | ||
var userInfoUrl = 'https://' + options.domain + '/userinfo'; | ||
var requestOptions = { | ||
url: userInfoUrl + '?access_token=' + options.userAccessToken | ||
}; | ||
request.get(requestOptions, function(err, res, data) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (res.statusCode.toString().substr(0, 1) !== '2') { | ||
return cb(new ApiError(data, res.statusCode)); | ||
} | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
return cb(e); | ||
} | ||
return cb(null, data); | ||
}); | ||
}; | ||
Client.prototype.getAccessToken = function(done){ | ||
@@ -57,6 +97,13 @@ var self = this; | ||
setTimeout(function () { | ||
var timer = setTimeout(function () { | ||
delete self._currentAccessToken; | ||
}, 1000 * 60 * 3); //~ 3 hours | ||
}, 1000 * 60 * 3); //~ 3 minutes | ||
// unref method added in node 0.10.x | ||
if (timer && timer.unref) { | ||
// Make the timer active but if it's the only item left in the event loop | ||
// won't keep the program running. | ||
timer.unref(); | ||
} | ||
done(null, token); | ||
@@ -63,0 +110,0 @@ }); |
{ | ||
"name": "auth0", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Client library for the Auth0 platform", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,2 @@ | ||
Node.js client library for the Auth0 platform. | ||
Node.js client library for the [Auth0](https://auth0.com) platform. | ||
@@ -318,3 +318,20 @@ ## Installation | ||
## Auth0 | ||
### Auth0.getUserInfo | ||
Gets a profile using an user Access Token. For instance, an user access token is returned (together with the id token) by the `/ro` end point. | ||
```js | ||
var options = {domain: 'my-domain.auth0.com', userAccessToken: 'XXXXXX'}; | ||
Auth0.getUserInfo(options, function (err, profile) { | ||
if (err) { throw err; } | ||
// Use user profile here | ||
}); | ||
``` | ||
## Authentication | ||
@@ -321,0 +338,0 @@ |
@@ -234,1 +234,54 @@ var expect = require('chai').expect; | ||
}); | ||
describe('Client', function () { | ||
describe('getUserInfo', function () { | ||
it('should work on request success', function (done) { | ||
var domain = 'my-domain.auth0.com'; | ||
var userAccessToken = 'an-user-access-token'; | ||
var resUserData = {foo: 'bar'}; | ||
var qs = querystring.stringify({ | ||
access_token: userAccessToken | ||
}); | ||
var options = {domain: domain, userAccessToken: userAccessToken}; | ||
var scope = nock('https://' + domain) | ||
.get('/userinfo?' + qs) | ||
.reply(200, resUserData); | ||
Auth0.getUserInfo(options, function (err, profile) { | ||
expect(err).to.not.exist; | ||
expect(profile).to.be.deep.equal(resUserData); | ||
expect(scope.isDone()).to.be.equal(true); | ||
nock.cleanAll(); | ||
done(); | ||
}); | ||
}); | ||
it('should fail when request fail', function (done) { | ||
var domain = 'my-domain.auth0.com'; | ||
var userAccessToken = 'an-user-access-token'; | ||
var resError = 'Invalid access token'; | ||
var qs = querystring.stringify({ | ||
access_token: userAccessToken | ||
}); | ||
var options = {domain: domain, userAccessToken: userAccessToken}; | ||
var scope = nock('https://' + domain) | ||
.get('/userinfo?' + qs) | ||
.reply(401, resError); | ||
Auth0.getUserInfo(options, function (err, profile) { | ||
expect(err.name).to.be.equal('ApiError'); | ||
expect(err.message).to.be.equal(resError); | ||
expect(profile).to.not.exist; | ||
expect(scope.isDone()).to.be.equal(true); | ||
nock.cleanAll(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
41356
941
352