Skip to content

Commit

Permalink
Added support to Authorization Code flow and to basic Access Token fu…
Browse files Browse the repository at this point in the history
…nctionalities
  • Loading branch information
andreareginato committed Jan 21, 2013
1 parent 5bb9ae6 commit 8302489
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 82 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ var redirect = Oauth2.AuthCode.authorizeURL({ redirectURI: 'http://localhost:300

// Get the access token object
vat params = { code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }
client.authCode.getToken(params, function(error, token){
// save the token object
client.authCode.getToken(params, function(error, result){
// save the token
})
```
### Refresh the Access Token
```javascript

token = OAuth2.AccessToken.create(json_token);
if (token.expired()) {
token.refresh(function(error, newToken) { token = newToken; })
token.refresh(function(error, refreshedToken) { token = refreshedToken; })
}
```
Expand Down
29 changes: 29 additions & 0 deletions lib/client/access-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Access Token class
//
module.exports = function(config) {

var core = require('./../core')(config);
require('date-utils');

// Returns the OAuth2.AccessToken instance.
//
// * `token` - An object containing the token object returned from the OAuth2 server.
// maintain state between the request and the callback.
//
function create(token) {
this.token = token;
this.token.expires_at = (new Date).addSeconds(7200);
return this
}

function expired() {
return (Date.compare(this.token.expires_at, new Date) == -1) ? false : true
}

return {
'create' : create,
'token': this.token,
'expired': expired
}
};
26 changes: 19 additions & 7 deletions lib/client/auth-code.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// A class to implement the Authorization Code flow.
// Authorization Code flow implementation
//
module.exports = function(config) {

Expand All @@ -9,12 +9,11 @@ module.exports = function(config) {
// Returns the OAuth2 authorization URI where the user decides to
// grant or deny the resources' access.
//
// * `redirectURI` - A String that represents the callback uri.
// * `scope` - A String that represents the application privileges.
// * `state` - A String that represents an optional opaque value used by the client to
// * `params.redirectURI` - A String that represents the registered application URI where the
// user is redirected after authorization.
// * `params.scope` - A String that represents the application privileges.
// * `params.state` - A String that represents an optional opaque value used by the client to
// maintain state between the request and the callback.
// * `callback` - The callback function returning the results.
// An error object is passed as first argument and the result as last.
//
function authorizeURL(params) {
params.response_type = 'code';
Expand All @@ -23,9 +22,22 @@ module.exports = function(config) {
return config.client.site + config.authorizationPath + '?' + qs.stringify(params);
}

//core.api('GET', '/devices/' + id, {}, callback);
//
// Returns the Access Token object.
//
// * `params.code` - Authorization code (from the authorization step).
// * `params.redirectURI` - A String that represents the callback uri.
// * `callback` - The callback function returning the results.
// An error object is passed as first argument and the result as last.
//
function getToken(params, callback) {
params.grant_type = 'authorization_code';
core.api('POST', config.tokenPath, params, callback);
}


return {
'authorizeURL' : authorizeURL,
'getToken' : getToken
}
};
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
'authorizationPath' : '/oauth/authorization',
'tokenPath' : '/oauth/token',
'client': {}
'client': {},
}
6 changes: 2 additions & 4 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var exports = module.exports,


//
// Construct the Core module.
// Core module.

module.exports = function(config) {

Expand Down Expand Up @@ -46,9 +46,7 @@ module.exports = function(config) {
if (error) throw new Error('Simple OAuth2: something went worng during the request');
if (process.env.DEBUG) console.log('Simple OAuth2: checking response body', body);

try { body = JSON.parse(body); }
catch(e) { body = errorResponse(response); }

if (response.statusCode >= 500) body = errorResponse(response);
if (response.statusCode >= 400) return callback(new HTTPError(body), null)

callback(error, body);
Expand Down
1 change: 1 addition & 0 deletions lib/simple-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ module.exports = function(config) {

return {
'AuthCode': require('./client/auth-code')(config),
'AccessToken': require('./client/access-token')(config),
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"dependencies": {
"request": "~2.12.0",
"querystring": "~0.1.0"
"querystring": "~0.1.0",
"date-utils": "~1.2.12"
},
"devDependencies": {
"should": "~1.2.1",
Expand Down
57 changes: 57 additions & 0 deletions test/access_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'https://example.org' } },
OAuth2 = require('./../lib/simple-oauth2.js')(credentials),
qs = require('querystring'),
nock = require('nock');

var request, result, token, error;


describe.only('OAuth2.AccessToken',function() {

beforeEach(function(done) {
var params = { 'code': 'code', 'redirect_uri': 'http://callback.com', 'grant_type': 'authorization_code' };
request = nock('https://example.org:443').post('/oauth/token', params).replyWithFile(200, __dirname + '/fixtures/access_token.json');
done();
})

beforeEach(function(done) {
var params = { 'code': 'code', 'redirect_uri': 'http://callback.com' }
OAuth2.AuthCode.getToken(params, function(e, r) {
error = e; result = r; done();
})
})

beforeEach(function(done) {
token = OAuth2.AccessToken.create(result);
done();
});

describe('#create',function() {
it('creates an access token',function() {
token.should.have.property('token');
});
});


describe('#expired',function() {

describe('when not expired', function() {

it('returns false',function() {
token.expired().should.be.true
});
});

describe('when expired', function() {

beforeEach(function(done) {
token.token.expires_at = Date.yesterday();
done();
});

it('returns false',function() {
token.expired().should.be.false
});
});
});
});
38 changes: 20 additions & 18 deletions test/auth-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'h

var request, result, error;

describe.only('OAuth2.AuthCode',function() {
describe('OAuth2.AuthCode',function() {

describe('#authorizeURL', function(){

Expand All @@ -21,25 +21,27 @@ describe.only('OAuth2.AuthCode',function() {
})
});

//describe('#getToken',function() {
describe('#getToken',function() {

//beforeEach(function(done) {
//request = nock('http://api.lelylan.com').get('/devices/1').replyWithFile(200, __dirname + '/fixtures/device.json');
//done();
//})
beforeEach(function(done) {
var params = { 'code': 'code', 'redirect_uri': 'http://callback.com', 'grant_type': 'authorization_code' };
request = nock('https://example.org:443').post('/oauth/token', params).replyWithFile(200, __dirname + '/fixtures/access_token.json');
done();
})

//beforeEach(function(done) {
//Lelylan.Device.find('1', function(e, r) {
//error = e; response = r; done();
//})
//})
beforeEach(function(done) {
var params = { 'code': 'code', 'redirect_uri': 'http://callback.com' }
OAuth2.AuthCode.getToken(params, function(e, r) {
error = e; result = r; done();
})
})

//it('makes the HTTP request', function() {
//request.isDone();
//});
it('makes the HTTP request', function() {
request.isDone();
});

//it('return a json array',function() {
//response.should.be.a('object');
//});
//});
it('returns an access token',function() {
result.should.have.property('access_token');
});
});
});
6 changes: 6 additions & 0 deletions test/fixtures/access_token.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"access_token": "4adc339e06c20e84c41d0c04c8ad5daf89cc3655d79b399930d112f5f7fXXXXX",
"refresh_token": "ec1a59d298aa51b3f133b6135b817bb19eb917aac5bc7821d410ffbf5ebXXXXX",
"token_type": "bearer",
"expires_in": 7200
}
24 changes: 0 additions & 24 deletions test/fixtures/device.json

This file was deleted.

24 changes: 0 additions & 24 deletions test/fixtures/devices.json

This file was deleted.

0 comments on commit 8302489

Please sign in to comment.