Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
[[FEAT]] Add logout support
Browse files Browse the repository at this point in the history
  • Loading branch information
jmendiara committed Aug 29, 2016
1 parent 99fa0df commit e9b2636
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,34 @@ are running your own TAccounts service:
* profileURL
* authorizeParams: supports prompt='login'

### Logout from Telefónica Accounts

The strategy exposes a new method `logout` as an addition to the common passportJS ones. Use this method to create a middleware that logouts the user directly from
Telefónica Accounts and redirects back to the specified `logoutCallbackURL`

```js

var options = {
// Other options
logoutCallbackURL: 'http://localhost/auth/logout'
};

var strategy = new TAccountsStrategy(options, verify);

passport.use(strategy);

app.get('/auth/taccounts/', passport.authenticate('taccounts'));
app.get('/auth/taccounts/callback', passport.authenticate('taccounts'));
app.get('/auth/taccounts/logout', strategy.logout());

app.get('/auth/logout', function(req, res) {
// Gets redirected here after logging out from Telefónica Accounts
req.logout();
res.redirect('/');
});
```


## License

Copyright 2015 [Telefónica Investigación y Desarrollo, S.A.U](http://www.tid.es)
Expand Down
13 changes: 13 additions & 0 deletions lib/index.js
Expand Up @@ -73,6 +73,7 @@ function TAccountsStrategy(options, verify) {
options.authorizationURL = options.authorizationURL || 'https://accounts.telefonica.com/telefonica/oauth/authorize';
options.tokenURL = options.tokenURL || 'https://accounts.telefonica.com/telefonica/oauth/token';
options.profileURL = options.profileURL || 'https://accounts.telefonica.com/api/v1/telefonica/users/me';
options.logoutURL = options.logoutURL || 'https://accounts.telefonica.com/telefonica/logout';

OAuth2Strategy.call(this, options, verify);
this.name = 'taccounts';
Expand All @@ -83,8 +84,20 @@ function TAccountsStrategy(options, verify) {

this.authorizationParams = getAuthorizationParams;

this.logout = logout;

///////////////////

function logout() {
if (!options.logoutCallbackURL) {
throw new TypeError('TAccountsStrategy requires a logoutCallbackURL option');
}

return function logoutMW(req, res) {
res.redirect(options.logoutURL + '?post_logout_redirect_uri=' + options.logoutCallbackURL);
};
}

function getAuthorizationParams() {
return options.authorizeParams ? options.authorizeParams : {};
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -29,17 +29,18 @@
"devDependencies": {
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"eslint": "^0.23.0",
"express": "^4.14.0",
"istanbul": "^0.3.16",
"jscs": "^1.13.1",
"mocha": "^2.2.5",
"nock": "^2.13.0",
"proxyquire": "^1.5.0",
"should": "^7.0.1",
"sinon": "~1.15.3",
"sinon-chai": "^2.8.0",
"supertest": "^1.0.1",
"xunit-file": "^0.0.6",
"jscs": "^1.13.1",
"eslint": "^0.23.0"
"supertest": "^2.0.0",
"xunit-file": "^0.0.6"
},
"dependencies": {
"passport-oauth2": "^1.1.2",
Expand Down
43 changes: 43 additions & 0 deletions test/unit/taccounts-test.js
Expand Up @@ -4,6 +4,8 @@ var events = require('events'),
util = require('util'),
sinon = require('sinon'),
nock = require('nock'),
request = require('supertest'),
express = require('express'),
OAuth2Strategy = require('passport-oauth2'),
TAccountsStrategy = require('../../lib');

Expand Down Expand Up @@ -65,6 +67,7 @@ describe('TAccounts basic tests', function() {
expect(options.authorizationURL).to.exist;
expect(options.tokenURL).to.exist;
expect(options.profileURL).to.exist;
expect(options.logoutURL).to.exist;
expect(options.scope).to.exist;
done();
});
Expand Down Expand Up @@ -130,3 +133,43 @@ describe('TAccounts user profile', function() {
done();
});
});

describe('TAccounts logout', function() {
it('should throw when trying to use logout mw and no option for logoutCallbackURL has been defined', function() {
var options = {
clientID: '2b8672be-5c80-ac91-96da-f4b922105431',
clientSecret: 'f5d689ac-fc2c-4e32-ac8a-321212ca1a8d'
};
function verify() {}

var strategy = new TAccountsStrategy(options, verify);

function createLogout() {
return strategy.logout();
}

expect(createLogout).to.throw(TypeError);
});

it('should redirect to Telefonica when calling logout', function(done) {
var options = {
clientID: '2b8672be-5c80-ac91-96da-f4b922105431',
clientSecret: 'f5d689ac-fc2c-4e32-ac8a-321212ca1a8d',
logoutCallbackURL: 'http://localhost/auth/logout'
};
function verify() {}

var strategy = new TAccountsStrategy(options, verify);

var app = express();
app.get('/auth/taccounts/logout', strategy.logout());

request(app)
.get('/auth/taccounts/logout')
.expect(302)
.end(function(err, res) {
expect(res.header.location).to.be.eql(options.logoutURL + '?post_logout_redirect_uri=' + options.logoutCallbackURL);
done(err);
});
});
});

0 comments on commit e9b2636

Please sign in to comment.