Skip to content

Commit

Permalink
Adding getAccessToken method to api.
Browse files Browse the repository at this point in the history
  • Loading branch information
pose committed Jan 30, 2014
1 parent 2660574 commit 404566a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 9 deletions.
37 changes: 37 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"strict": false,
"smarttabs": true,
"expr": true,


"evil": true,
"browser": true,
"regexdash": true,
"wsh": true,
"trailing": true,
"sub": true,
"unused": true,
"laxcomma": true,

"globals": {
"after": false,
"before": false,
"afterEach": false,
"beforeEach": false,
"describe": false,
"it": false
}
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ api.getUsers({connection: 'a-waad-connection'}, function (err, firstPageOfResult
The same than ```getUsers``` but this method returns users for all social connections, ie: not enterprise connections.
### api.getAccessToken(callback)
Retrieves an Access Token to make direct HTTP calls to Auth0 API.
```js
api.getAccessToken(function (err, token) {
if (err) {
console.log('Error fetching token: ' + err);
return;
}
// Do something with token
...
});
```
### api.updateUserMetadata(userId, metadata, callback)
This method updates the metadata for a user. `metadata` is an object, and the fields in that object will be set on the user referenced by `userId`.
Expand Down
10 changes: 5 additions & 5 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Client.getAccessToken = function (options, done) {
});
};

Client.prototype._getAccessToken = function(done){
Client.prototype.getAccessToken = function(done){
var self = this;

if (this._currentAccessToken) return done(null, this._currentAccessToken);
Expand All @@ -56,7 +56,7 @@ Client.prototype._getAccessToken = function(done){
setTimeout(function () {
delete self._currentAccessToken;
}, 1000 * 60 * 3); //~ 3 hours

done(null, token);
});
};
Expand All @@ -65,17 +65,17 @@ Object.keys(api).forEach(function (m) {
Client.prototype[m] = function (){
var args = [].slice.call(arguments);
var callback = args[args.length - 1];

if (this.options.accessToken) {
api[m].apply(this, [this.options.accessToken].concat(args));
}
else {
this._getAccessToken(function (err, accessToken) {
this.getAccessToken(function (err, accessToken) {
if (err) return callback(err);
api[m].apply(this, [accessToken].concat(args));
}.bind(this));
}
};
});

module.exports = Client;
module.exports = Client;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Client library for the Auth0 platform",
"main": "lib/index.js",
"scripts": {
"test": "mocha"
"test": "mocha test"
},
"repository": {
"type": "git",
Expand All @@ -20,8 +20,9 @@
],
"license": "MIT",
"devDependencies": {
"mocha": "~1.7.0",
"should": "~1.2.1"
"mocha": "~1.17.1",
"chai": "~1.9.0",
"nock": "~0.27.2"
},
"dependencies": {
"xtend": "~1.0.3",
Expand Down
80 changes: 80 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var expect = require('chai').expect;

var nock = require('nock');
var querystring = require('querystring');

var Auth0 = require('../lib');


describe('API', function () {

describe('getAccessToken', function () {
it('should work on request success', function (done) {
var scope;

var domain = 'mydomain.auth0.com',
clientID = 'SOME_SECRET_ID',
clientSecret = 'SOME_SECRET_SHHHH',
accessToken = 'OMG_SO_TOKEN_SUCH_CRYPTO';

var qs = querystring.stringify({
client_id: clientID,
client_secret: clientSecret,
grant_type: 'client_credentials'
});

scope = nock('https://' + domain)
.post('/oauth/token', qs)
.reply(200, {
'access_token': accessToken,
'token_type': 'bearer'
});

var api = new Auth0({
domain: domain,
clientID: clientID,
clientSecret: clientSecret
});

api.getAccessToken(function (err, token) {
expect(err).to.not.exist;
expect(token).to.be.equal(accessToken);
done();
});
});

it('should fail when request fails', function (done) {
var scope;

var error = 'Error: Token could not be retrieved';

var domain = 'mydomain.auth0.com',
clientID = 'SOME_SECRET_ID',
clientSecret = 'SOME_SECRET_SHHHH';

var qs = querystring.stringify({
client_id: clientID,
client_secret: clientSecret,
grant_type: 'client_credentials'
});

scope = nock('https://' + domain)
.post('/oauth/token', qs)
.reply(401, error);

var api = new Auth0({
domain: domain,
clientID: clientID,
clientSecret: clientSecret
});

api.getAccessToken(function (err, token) {
expect(err).not.to.be.equal(null);
expect(err.name).to.be.equal('ApiError');
expect(err.statusCode).to.be.equal(401);
expect(token).to.not.exist;
done();
});
});
});
});
1 change: 0 additions & 1 deletion test/mocha.opts

This file was deleted.

0 comments on commit 404566a

Please sign in to comment.