Skip to content

anvaka/gatekeeper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gatekeeper

Because of some security-related limitations, Github prevents you from implementing the OAuth Web Application Flow on a client-side only application.

This is a real bummer. So we built Gatekeeper, which is the missing piece you need in order to make it work.

Gatekeeper works well with Github.js, which helps you accessing the Github API from the browser.

API

GET http://localhost:9999/authenticate/TEMPORARY_CODE

OAuth Steps

Also see the documentation on Github.

  1. Redirect users to request GitHub access.

    GET https://github.com/login/oauth/authorize
    
  2. GitHub redirects back to your site including a temporary code you need for the next step.

    You can grab it like so:

    var code = window.location.href.match(/\?code=(.*)/)[1];
  3. Request the actual token using your instance of Gatekeeper, which knows your client_secret.

    $.getJSON('http://localhost:9999/authenticate/'+code, function(data) {
      console.log(data.token);
    });

Setup your Gatekeeper

  1. Clone it
git clone git@github.com:anvaka/gatekeeper.git
  1. Install Dependencies
cd gatekeeper && npm install
  1. Adjust config.json
{
  "default" : {
    "client_id": "GITHUB_APPLICATION_CLIENT_ID",
    "client_secret": "GITHUB_APPLICATION_CLIENT_SECRET"
  },
  "oauth_host": "github.com",
  "oauth_port": 443,
  "oauth_path": "/login/oauth/access_token",
  "oauth_method": "POST"
}

If you want to support multiple apps (e.g. one for localhost development, one for beta and one for production), you can adjust your config with use case name:

{
  "local": {
    "client_id": "GITHUB_APPLICATION_LOCAL_CLIENT_ID",
    "client_secret": "GITHUB_APPLICATION_LOCAL_CLIENT_SECRET"
  },
  "beta": {
    "client_id": "GITHUB_APPLICATION_BETA_CLIENT_ID",
    "client_secret": "GITHUB_APPLICATION_BETA_CLIENT_SECRET"
  },
  "default" : {
    "client_id": "GITHUB_APPLICATION_CLIENT_ID",
    "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:

export BETA='{"client_id": "CLIENT_ID", "client_secret": "CLIENT_SECRET"}'
  1. Serve it
$ node server.js

Deploy on Heroku

  1. Install heroku CLI. Login to heroku:
heroku login
  1. Create a new Heroku app
heroku apps:create
  1. Rename it (optional)
heroku apps:rename NEW_NAME
  1. Provide OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET:
heroku config:set DEFAULT='{"client_id": "CLIENT_ID", "client_secret": "CLIENT_SECRET"}'
  1. Push changes to heroku
git push heroku master
  1. Verify it is working:
curl your_app_name.herokuapp.com/

Should return something like Cannot GET /

To actually trade github code for an access token call:

curl your_app_name.herokuapp.com/authenticate/code

If you want to use a particular use case (e.g. beta), declared in your config:

curl your_app_name.herokuapp.com/authenticate/code?case=beta

About

Enables client-side applications to dance OAuth with GitHub.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 61.6%
  • CoffeeScript 38.4%