From 68236c64a5ae78aeac0712ec6e83cd21e0405602 Mon Sep 17 00:00:00 2001 From: Antoine RENELEAU Date: Thu, 25 Jun 2015 14:43:51 +0200 Subject: [PATCH] add multiapp compatibility --- README.md | 56 ++++++++++++++++++++++++---------------------------- config.json | 27 ++++++++++++++++++------- package.json | 3 ++- server.js | 34 +++++++++++++++++-------------- 4 files changed, 67 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index e56850d..64991ac 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Gatekeeper works well with [Github.js](http://github.com/michael/github), which ## API ``` -GET http://localhost:9999/authenticate/TEMPORARY_CODE +GET http://localhost:9999/authenticate/CLIENT_ID/TEMPORARY_CODE ``` ## OAuth Steps @@ -34,7 +34,7 @@ Also see the [documentation on Github](http://developer.github.com/v3/oauth/). 3. Request the actual token using your instance of Gatekeeper, which knows your `client_secret`. ```js - $.getJSON('http://localhost:9999/authenticate/'+code, function(data) { + $.getJSON('http://localhost:9999/authenticate/'+clientId+'/'+code, function(data) { console.log(data.token); }); ``` @@ -56,19 +56,29 @@ Also see the [documentation on Github](http://developer.github.com/v3/oauth/). 3. Adjust config.json ```json - { - "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", - "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", - "oauth_host": "github.com", - "oauth_port": 443, - "oauth_path": "/login/oauth/access_token", - "oauth_method": "POST", - "port": 9999 - } + { + "port": 9999, + "apps": [ + { + "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", + "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", + "oauth_host": "github.enterprise.fr", + "oauth_port": 443, + "oauth_path": "/login/oauth/access_token", + "oauth_method": "POST" + }, + { + "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", + "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", + "oauth_host": "github.com", + "oauth_port": 443, + "oauth_path": "/login/oauth/access_token", + "oauth_method": "POST" + } + ] + } ``` - You can also set environment variables to override the settings if you don't want Git to track your adjusted config.json file. Just use UPPER_CASE keys. - 4. Serve it ``` @@ -91,22 +101,13 @@ Use the button below to instantly setup your own Gatekeeper instance on Heroku. heroku apps:create APP_NAME ``` -3. Provide OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET: +2. Adjust config.json - ``` - heroku config:set OAUTH_CLIENT_ID=XXXX OAUTH_CLIENT_SECRET=YYYY - ``` - -4. Push changes to heroku +3. Push changes to heroku ``` git push heroku master ``` -OR - - ``` - heroku restart - ``` ##Deploy on Azure @@ -123,13 +124,8 @@ Use the button below to instantly setup your own Gatekeeper instance on Azure. ``` azure site create SITE_NAME --git ``` - -2. Provide OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET: - ``` - azure site appsetting add OAUTH_CLIENT_ID=XXXX - azure site appsetting add OAUTH_CLIENT_SECRET=YYYY - ``` +2. Adjust config.json 3. Push changes to Azure diff --git a/config.json b/config.json index 2c39adb..229ded9 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,21 @@ { - "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", - "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", - "oauth_host": "github.com", - "oauth_port": 443, - "oauth_path": "/login/oauth/access_token", - "oauth_method": "POST" -} \ No newline at end of file + "port": 9999, + "apps": [ + { + "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", + "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", + "oauth_host": "github.enterprise.fr", + "oauth_port": 443, + "oauth_path": "/login/oauth/access_token", + "oauth_method": "POST" + }, + { + "oauth_client_id": "GITHUB_APPLICATION_CLIENT_ID", + "oauth_client_secret": "GITHUB_APPLICATION_CLIENT_SECRET", + "oauth_host": "github.com", + "oauth_port": 443, + "oauth_path": "/login/oauth/access_token", + "oauth_method": "POST" + } + ] +} diff --git a/package.json b/package.json index ef7960f..bae05ca 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "author": "Michael Aufreiter", "contributors": [], "dependencies": { - "express": "~3.16.0" + "express": "~3.16.0", + "lodash": "^3.9.3" }, "engines": { "node": ">= 0.6.x" diff --git a/server.js b/server.js index a340294..ab4a5f9 100644 --- a/server.js +++ b/server.js @@ -3,16 +3,13 @@ var url = require('url'), https = require('https'), fs = require('fs'), qs = require('querystring'), + _ = require('lodash'), express = require('express'), app = express(); // Load config defaults from JSON file. -// Environment variables override defaults. function loadConfig() { var config = JSON.parse(fs.readFileSync(__dirname+ '/config.json', 'utf-8')); - for (var i in config) { - config[i] = process.env[i.toUpperCase()] || config[i]; - } console.log('Configuration'); console.log(config); return config; @@ -20,18 +17,24 @@ function loadConfig() { var config = loadConfig(); -function authenticate(code, cb) { +function authenticate(clientId, code, cb) { + var appConfig = _.findWhere(config.apps, {'oauth_client_id': clientId}); + if (!appConfig) { + cb('No app configurated for client ID : ' + clientId); + } + console.log('Authenticating for app :'); + console.log(appConfig); var data = qs.stringify({ - client_id: config.oauth_client_id, - client_secret: config.oauth_client_secret, + client_id: appConfig.oauth_client_id, + client_secret: appConfig.oauth_client_secret, code: code }); var reqOptions = { - host: config.oauth_host, - port: config.oauth_port, - path: config.oauth_path, - method: config.oauth_method, + host: appConfig.oauth_host, + port: appConfig.oauth_port, + path: appConfig.oauth_path, + method: appConfig.oauth_method, headers: { 'content-length': data.length } }; @@ -52,16 +55,17 @@ function authenticate(code, cb) { // Convenience for allowing CORS on routes - GET only app.all('*', function (req, res, next) { - res.header('Access-Control-Allow-Origin', '*'); - res.header('Access-Control-Allow-Methods', 'GET, OPTIONS'); + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET, OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); -app.get('/authenticate/:code', function(req, res) { +app.get('/authenticate/:client/:code', function(req, res) { + console.log('client id:' + req.params.client); console.log('authenticating code:' + req.params.code); - authenticate(req.params.code, function(err, token) { + authenticate(req.params.client, req.params.code, function(err, token) { var result = err || !token ? {"error": "bad_code"} : { "token": token }; console.log(result); res.json(result);