Skip to content

Commit

Permalink
cookie support
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Feb 4, 2017
1 parent 16dbff8 commit 24683f7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
27 changes: 24 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module.exports = (
callbackPath = '/authentication',
callbackRouteMethod = 'get',
serviceValidator = () => true,
useCookie = false,
cookieOptions = {},
app = express(),
} = {}
) => {
const app = express();

app.get(landingPath, [
(req, res, next) => {
const service = handler.parseService(req);
Expand All @@ -23,6 +24,14 @@ module.exports = (
if (!serviceValidator(service)) {
return res.status(403).send('Invalid service');
}

if (useCookie) {
const token = req.cookies.get('paale_token', cookieOptions);
if (token) {
return res.redirect(appendQuery(service, `token=${token}`));
}
}

next();
},
handler.landing(callbackPath),
Expand All @@ -40,11 +49,23 @@ module.exports = (
},
handler.authentication(callbackPath),
tokenStorage.store,
(req, res) => res.redirect(appendQuery(req.paale_service, `token=${req.paale_token}`)),
(req, res) => {
if (useCookie) {
res.cookies.set('paale_token', req.paale_token, cookieOptions);
}
res.redirect(appendQuery(req.paale_service, `token=${req.paale_token}`));
},
]);

app.get(identityPath, [
(req, res, next) => {
if (useCookie) {
req.paale_token = req.cookies.get('paale_token', cookieOptions);
if (req.paale_token) {
return next();
}
}

let parts = req.get('Authorization');
if (!parts) {
return res.status(401).send({ message: 'Unauthenticated' });
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"supertest": "^2.0.0"
},
"devDependencies": {
"lodash": "^4.16.4",
"cookies": "^0.6.2",
"eslint": "^3.8.1",
"eslint-config-airbnb-base": "^9.0.0",
"eslint-plugin-import": "^2.0.1",
"istanbul": "^0.4.5",
"lodash": "^4.16.4",
"mocha": "^3.1.2",
"proxyquire": "^1.7.10",
"should": "^11.1.1"
Expand Down
63 changes: 37 additions & 26 deletions tests/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const appendQuery = require('append-query');
const _ = require('lodash');
const path = require('path');
const jwtModule = require('jsonwebtoken');
const Cookies = require('cookies');
const express = require('express');
const proxyquire = require('proxyquire').noCallThru();
const paale = require('../../index');

Expand Down Expand Up @@ -51,13 +53,13 @@ describe('Paale dai server tests', () => {
});

describe('Post Google Redirection', () => {
let app,
agent,
let agent,
stateEncoder,
OAuth2,
people = {};

const state = 'tgije',
const app = express(),
state = 'tgije',
google = {},
service = 'http://senani.introcept.co',
jwt = {},
Expand Down Expand Up @@ -101,14 +103,20 @@ describe('Paale dai server tests', () => {
const jwtStorage = proxyquire(path.resolve('./storage/jwt'), {
jsonwebtoken: jwt,
});
app = paale(

app.use(Cookies.express());

paale(
handler('GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET'),
jwtStorage(),
{
callbackPath,
serviceValidator: service => !_.startsWith(service, 'http://danger'),
useCookie: true,
app
}
);

agent = request.agent(app);
});

Expand All @@ -130,18 +138,6 @@ describe('Paale dai server tests', () => {
return endRequest(req);
});

// it('should check if email belongs to introcept.co', function () {
// people.get = function (opts, callback) {
// return callback(null, {
// domain: 'gmail.com'
// });
// };
//
// var req = agent.get(appendQuery(callbackPath, 'code=' + code + '&state=' + state))
// .expect(403, 'You must have an email address from introcept.co');
// return endRequest(req);
// });

it('should redirect to original requesting service', () => {
const response = {
domain: 'introcept.co',
Expand All @@ -165,25 +161,42 @@ describe('Paale dai server tests', () => {

const req = agent.get(appendQuery(callbackPath, `code=${code}&state=${state}`))
.expect(302);
return endRequest(req);
return endRequest(req)
.then((res) => {
res.header.location.should.be.exactly(appendQuery(service, `token=${token}`));
});
});

it('should retrieve from cookies the next time and directly redirect to the redirecting service', () => {
const req = agent.get('/?service=http://senani.introcept.co')
.expect(302);
return endRequest(req)
.then((res) => {
res.header.location.should.be.exactly(appendQuery(service, `token=${token}`));
});
});
});

describe('Profile API tests', () => {
let app,
agent,
let agent,
jwt = { JsonWebTokenError: jwtModule.JsonWebTokenError, TokenExpiredError: jwtModule.TokenExpiredError };
const token = 'o35234-o2345';
const token = 'o35234-o2345',
app = express();
before(() => {
const handler = require(path.resolve('./handler/google-oauth2'));
const jwtStorage = proxyquire(path.resolve('./storage/jwt'), {
jsonwebtoken: jwt,
});
app = paale(

app.use(Cookies.express());

paale(
handler('GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET'),
jwtStorage(),
{
identityPath: '/me',
useCookie: true,
app
}
);

Expand Down Expand Up @@ -223,20 +236,18 @@ describe('Paale dai server tests', () => {
});
});

it('should return expired token response for expired token', () => {
it('should use cookie to retrieve token and return expired token response for expired token', () => {
jwt.verify = function (sourceToken, key, opts, callback) {
sourceToken.should.be.exactly(token);
callback(new jwt.TokenExpiredError());
};

const req = agent.get('/me')
.set('Authorization', `Bearer ${token}`)
.set('Cookie', `paale_token=${token}`)
.expect(401);

return endRequest(req)
.then((response) => {
response.body.code.should.be.exactly('expiredToken');
});
.then((response) => response.body.code.should.be.exactly('expiredToken'));
});


Expand Down

0 comments on commit 24683f7

Please sign in to comment.