1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-13 06:40:06 +02:00

bell auth example

This commit is contained in:
Sérgio Ramos 2016-11-17 14:16:10 +00:00
parent 875ca384a0
commit af60c141f5
10 changed files with 182 additions and 0 deletions

3
spikes/auth/bell/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/node_modules
/npm-debug.log
.idea

View File

@ -0,0 +1,16 @@
{
"name": "bell-auth-spike",
"version": "1.0.0",
"scripts": {
"start": "node src/index.js"
},
"main": "src/index.js",
"private": true,
"dependencies": {
"bell": "^8.3.0",
"hapi": "^15.2.0",
"hapi-auth-cookie": "^6.1.1",
"inert": "^4.0.2",
"require-dir": "^0.3.1"
}
}

View File

@ -0,0 +1,46 @@
# hapi
Integrations to consider:
- [x] [Github](https://github.com/hapijs/bell/blob/master/Providers.md#github)
- [x] [Bitbucket](https://github.com/hapijs/bell/blob/master/Providers.md#bitbucket)
- [x] [Gitlab](https://github.com/hapijs/bell/blob/master/Providers.md#gitlab) (hosted and on-premise)
- [x] [Facebook](https://github.com/hapijs/bell/blob/master/Providers.md#facebook)
- [x] [Twitter](https://github.com/hapijs/bell/blob/master/Providers.md#twitter)
Aspects to consider:
- [x] Hapi integration
- [x] Consistency
- [x] Battle tested
Regarding "Battle tested":
[Bell](https://github.com/hapijs/bell) has 383 stars and 8664 downloads in the last month. It's nowhere near the 757640 downloads that passport has. However [Eran Hammer](https://github.com/hueniverse) was the lead author and editor of the OAuth2 spec and although he isn't the official maintainer of [Bell](https://github.com/hapijs/bell), he was the creator of it and the second most active contributor.
I wasn't able to find a list of companies using it, I found that developers from the following companies contributed to it:
- Joyent
- Walmart Labs
- Booking.com
- Microsoft
- Expedia
- Yahoo
Being the official Hapi module for third-party authentication, I think it's safe to assume that most companies using Hapi that have this need use this module.
## example `stratagies.json`
```json
[{
"provider": "twitter",
"password": "YChZVgVJQyG0Te3lpYzc+9Ag0PuQfUX0ilG3nHIvIlU=",
"clientId": "",
"clientSecret": "",
"isSecure": false
}, {
"provider": "github",
"password": "YChZVgVJQyG0Te3lpYzc+9Ag0PuQfUX0ilG3nHIvIlU=",
"clientId": "",
"clientSecret": "",
"isSecure": false
}]
```

View File

@ -0,0 +1,32 @@
const requireDir = require('require-dir');
const strategies = require('./strategies');
const plugins = require('./plugins');
const routes = requireDir('./routes');
const Hapi = require('hapi');
const path = require('path');
const fs = require('fs');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
server.register(plugins, (err) => {
if (err) {
throw err;
}
strategies.forEach((s) => server.auth.strategy(s.provider, 'bell', s));
Object.keys(routes).forEach((name) => {
routes[name](server);
});
server.start((err) => {
server.connections.forEach((conn) => {
console.log(`started at: ${conn.info.uri}`);
});
});
});

View File

@ -0,0 +1,5 @@
module.exports = [
require('inert'),
require('hapi-auth-cookie'),
require('bell')
];

View File

@ -0,0 +1,35 @@
const path = require('path');
module.exports = (server) => {
server.route({
method: ['GET', 'POST'],
path: '/auth/twitter',
config: {
auth: 'twitter',
handler: (request, reply) => {
if (!request.auth.isAuthenticated) {
return reply('Authentication failed due to: ' + request.auth.error.message);
}
reply(`Welcome ${request.auth.credentials.profile.displayName}`);
}
}
});
server.route({
method: ['GET', 'POST'],
path: '/auth/github',
config: {
auth: 'github',
handler: (request, reply) => {
if (!request.auth.isAuthenticated) {
return reply('Authentication failed due to: ' + request.auth.error.message);
}
console.log(request.auth);
reply(`Welcome ${request.auth.credentials.profile.displayName}`);
}
}
});
};

View File

@ -0,0 +1,11 @@
const path = require('path');
module.exports = (server) => {
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => {
reply.file(path.join(__dirname, '../../static/index.html'));
}
});
};

View File

@ -0,0 +1,18 @@
const Pkg = require('../../package.json');
const internals = {
response: {
version: Pkg.version
}
};
module.exports = (server) => {
server.route({
method: 'GET',
path: '/ops/version',
config: {
description: 'Returns the version of the server',
handler: (request, reply) => reply(internals.response)
}
});
};

View File

@ -0,0 +1 @@
[]

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Hello from the auth example</h1>
<p>you can authenticate using the following services:</p>
<ul>
<li><a href="/auth/twitter">Twitter</a></li>
<li><a href="/auth/github">Github</a></li>
</ul>
</body>
</html>