diff --git a/spikes/auth/bell/.gitignore b/spikes/auth/bell/.gitignore new file mode 100644 index 00000000..32712815 --- /dev/null +++ b/spikes/auth/bell/.gitignore @@ -0,0 +1,3 @@ +/node_modules +/npm-debug.log +.idea diff --git a/spikes/auth/bell/package.json b/spikes/auth/bell/package.json new file mode 100644 index 00000000..31d4f4e6 --- /dev/null +++ b/spikes/auth/bell/package.json @@ -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" + } +} diff --git a/spikes/auth/bell/readme.md b/spikes/auth/bell/readme.md new file mode 100644 index 00000000..7f5f45e4 --- /dev/null +++ b/spikes/auth/bell/readme.md @@ -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 +}] +``` \ No newline at end of file diff --git a/spikes/auth/bell/src/index.js b/spikes/auth/bell/src/index.js new file mode 100644 index 00000000..a8f1249f --- /dev/null +++ b/spikes/auth/bell/src/index.js @@ -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}`); + }); + }); +}); diff --git a/spikes/auth/bell/src/plugins.js b/spikes/auth/bell/src/plugins.js new file mode 100644 index 00000000..74ad209c --- /dev/null +++ b/spikes/auth/bell/src/plugins.js @@ -0,0 +1,5 @@ +module.exports = [ + require('inert'), + require('hapi-auth-cookie'), + require('bell') +]; diff --git a/spikes/auth/bell/src/routes/auth.js b/spikes/auth/bell/src/routes/auth.js new file mode 100644 index 00000000..84edade7 --- /dev/null +++ b/spikes/auth/bell/src/routes/auth.js @@ -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}`); + } + } + }); +}; diff --git a/spikes/auth/bell/src/routes/home.js b/spikes/auth/bell/src/routes/home.js new file mode 100644 index 00000000..48ead969 --- /dev/null +++ b/spikes/auth/bell/src/routes/home.js @@ -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')); + } + }); +}; diff --git a/spikes/auth/bell/src/routes/version.js b/spikes/auth/bell/src/routes/version.js new file mode 100644 index 00000000..987747cb --- /dev/null +++ b/spikes/auth/bell/src/routes/version.js @@ -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) + } + }); +}; diff --git a/spikes/auth/bell/src/strategies.json b/spikes/auth/bell/src/strategies.json new file mode 100644 index 00000000..0637a088 --- /dev/null +++ b/spikes/auth/bell/src/strategies.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/spikes/auth/bell/static/index.html b/spikes/auth/bell/static/index.html new file mode 100644 index 00000000..96cd5b8d --- /dev/null +++ b/spikes/auth/bell/static/index.html @@ -0,0 +1,15 @@ + + + + + + + +

Hello from the auth example

+

you can authenticate using the following services:

+ + +