Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiapp #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
```
Expand All @@ -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

```
Expand All @@ -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

Expand All @@ -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

Expand Down
27 changes: 20 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
@@ -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"
}
"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"
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 19 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ 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;
}

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 }
};

Expand All @@ -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);
Expand Down