Skip to content

Commit bdc274a

Browse files
author
David Inga
committed
first commit and first version
1 parent ebcceca commit bdc274a

File tree

7 files changed

+2816
-0
lines changed

7 files changed

+2816
-0
lines changed

Diff for: .editorconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true
12+
13+
[*.md]
14+
trim_trailing_whitespace = false

Diff for: .eslintrc.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"node": true,
5+
"mocha": true,
6+
"es6": true
7+
},
8+
"globals": {
9+
},
10+
"parserOptions": {
11+
"ecmaVersion": 6,
12+
"sourceType": "module"
13+
},
14+
"extends": "vizzuality"
15+
}

Diff for: .gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Mac OS
2+
.DS_Store
3+
4+
# Text editors
5+
*.sublime*
6+
.sublime*
7+
8+
# Vendors
9+
/node_modules
10+
11+
# Logs
12+
*.log*

Diff for: README.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Passport control tower Strategy
2+
3+
A Passport's strategy for Control Tower
4+
5+
## Installation
6+
7+
```bash
8+
npm install --save passport-control-tower
9+
```
10+
11+
## Example of use
12+
13+
```js
14+
const express = require('express');
15+
const passport = require('passport');
16+
const ControlTowerStrategy = require('passport-control-tower');
17+
const cookieParser = require('cookie-parser');
18+
const bodyParser = require('body-parser');
19+
const app = express();
20+
21+
function isAuthenticated(req, res, next) {
22+
if (req.isAuthenticated()) return next();
23+
// if they aren't redirect them to the home page
24+
res.redirect('/login');
25+
}
26+
27+
// Use the Control Tower Strategy within Passport.
28+
passport.use(new ControlTowerStrategy({
29+
apiUrl: '[CONTROL_TOWER_API_URL]',
30+
callbackUrl: '[YOUR_CALLBACK_URL]'
31+
}));
32+
33+
// Passport session setup.
34+
// To support persistent login sessions, Passport needs to be able to
35+
// serialize users into and deserialize users out of the session. Typically,
36+
// this will be as simple as storing the user ID when serializing, and finding
37+
// the user by ID when deserializing. However, since this example does not
38+
// have a database of user records, the complete spotify profile is serialized
39+
// and deserialized.
40+
passport.serializeUser(function(user, done) {
41+
done(null, user);
42+
});
43+
passport.deserializeUser(function(obj, done) {
44+
done(null, obj);
45+
});
46+
47+
// configure Express
48+
app.use(cookieParser());
49+
app.use(bodyParser.urlencoded({ extended: false }));
50+
app.use(bodyParser.json());
51+
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
52+
// Initialize Passport! Also use passport.session() middleware, to support
53+
// persistent login sessions (recommended).
54+
app.use(passport.initialize());
55+
app.use(passport.session());
56+
57+
app.get('/', function(req, res) {
58+
res.send('Welcome!');
59+
});
60+
61+
app.get('/private', isAuthenticated, function(req, res) {
62+
res.send('Success!');
63+
});
64+
65+
app.get('/login', passport.authenticate('control-tower'), function(req, res) {
66+
// Success
67+
res.redirect('/private');
68+
});
69+
70+
app.get('/logout', function(req, res) {
71+
req.session.destroy();
72+
req.logout();
73+
// Success
74+
res.redirect('/');
75+
});
76+
77+
app.listen(3000);
78+
```

Diff for: lib/auth.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const request = require('superagent');
2+
const passport = require('passport-strategy');
3+
const util = require('util');
4+
5+
function Strategy(options = {}) {
6+
this.name = 'control-tower';
7+
if (!options.apiUrl) throw new TypeError('apiUrl param is required');
8+
if (!options.callbackUrl) throw new TypeError('callbackUrl param is required');
9+
passport.Strategy.call(this);
10+
this.apiUrl = options.apiUrl;
11+
this.callbackUrl = options.callbackUrl;
12+
}
13+
14+
// Inherits from Passport Strategy
15+
util.inherits(Strategy, passport.Strategy);
16+
17+
/**
18+
* Authentication method
19+
*/
20+
Strategy.prototype.authenticate = function authenticate(req) {
21+
passport.Strategy.call(this);
22+
if (!req.isAuthenticated() && !req.query.token) {
23+
this.redirect(`${this.apiUrl}/auth?callbackUrl=${this.callbackUrl}&token=true`);
24+
} else {
25+
this.verify({ token: req.query.token }, (err, user, info) => {
26+
if (err) return this.error(err);
27+
if (!user) return this.fail(info);
28+
return this.success(user, info);
29+
});
30+
}
31+
};
32+
33+
/**
34+
* Verify using Control Tower gateway
35+
*/
36+
Strategy.prototype.verify = function verify(user, done) {
37+
request(`${this.apiUrl}/auth/check-logged`)
38+
.set('Authorization', `Bearer ${user.token}`)
39+
.end((err, res) => {
40+
if (err) {
41+
done(err, null, 'User unathorized');
42+
} else {
43+
done(null, res.body, 'User authenticated correctly');
44+
}
45+
});
46+
};
47+
48+
Strategy.prototype.error = function error(err) {
49+
throw err || 'An error happened';
50+
};
51+
52+
module.exports = Strategy;

Diff for: package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "passport-control-tower",
3+
"version": "0.1.0",
4+
"description": "A Passport's strategy for Control Tower",
5+
"main": "lib/auth.js",
6+
"scripts": {
7+
"lint": "eslint .",
8+
"test": "npm run lint"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/davidsingal/passport-control-tower.git"
13+
},
14+
"author": "David Inga",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/davidsingal/passport-control-tower/issues"
18+
},
19+
"homepage": "https://github.com/davidsingal/passport-control-tower#readme",
20+
"devDependencies": {
21+
"eslint": "^3.15.0",
22+
"eslint-config-vizzuality": "^1.0.3"
23+
},
24+
"dependencies": {
25+
"passport-strategy": "^1.0.0",
26+
"superagent": "^3.4.3"
27+
}
28+
}

0 commit comments

Comments
 (0)