Permalink
Browse files

minimal sinatra github app

  • Loading branch information...
0 parents commit efc6189cdf23557c5a0b1e48d49ffad5c92de3ab @schacon committed Jun 21, 2011
Showing with 232 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +5 −0 Gemfile
  3. +39 −0 Gemfile.lock
  4. +20 −0 LICENSE
  5. +78 −0 README.md
  6. +62 −0 app.rb
  7. +12 −0 config.ru
  8. +3 −0 views/app.erb
  9. +7 −0 views/index.erb
@@ -0,0 +1,6 @@
+coverage
+.bundle
+pkg
+.DS_Store
+tmp
+.powrc
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+gem "sinatra"
+gem "sinatra_auth_github"
+
@@ -0,0 +1,39 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.6)
+ faraday (0.6.1)
+ addressable (~> 2.2.4)
+ multipart-post (~> 1.1.0)
+ rack (>= 1.1.0, < 2)
+ json (1.5.3)
+ mime-types (1.16)
+ multi_json (1.0.3)
+ multipart-post (1.1.2)
+ oauth2 (0.4.1)
+ faraday (~> 0.6.1)
+ multi_json (>= 0.0.5)
+ rack (1.3.0)
+ rest-client (1.6.3)
+ mime-types (>= 1.16)
+ sinatra (1.2.6)
+ rack (~> 1.1)
+ tilt (>= 1.2.2, < 2.0)
+ sinatra_auth_github (0.0.16)
+ rest-client (~> 1.6.1)
+ sinatra (~> 1.0)
+ warden-github (~> 0.0.9)
+ tilt (1.3.2)
+ warden (1.0.4)
+ rack (>= 1.0)
+ warden-github (0.0.9)
+ json (~> 1.5)
+ oauth2 (~> 0.4.1)
+ warden (~> 1.0.4)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ sinatra
+ sinatra_auth_github
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Scott Chacon
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
+Minimal Sinatra GitHub Application
+==================================
+
+This is the smallest working Sinatra application that authenticates via the GitHub [OAuth2][oa] stuff. This skeleton app exists simply because it took me a little while to figure out how to put all the pieces together to get to the point that I could start building something. So here you go.
+
+[oa]: http://developer.github.com/v3/oauth/
+
+Basically, to get this app running on Heroku, for instance, you will want to:
+
+* Register a new Heroku instance (you will have to use something other than 'ghmin')
+
+ $ heroku apps:create ghmin
+ Creating ghmin... done, stack is bamboo-mri-1.9.2
+ http://ghmin.heroku.com/ | git@heroku.com:ghmin.git
+ Git remote heroku added
+
+* Add SSL to your Heroku instance, since we are dealing with user tokens here
+
+ $ heroku addons:add piggyback_ssl
+ Adding piggyback_ssl to ghmin... done (free)
+
+* Register your GitHub OAuth2 application with that new URL (but https) at:
+
+ https://github.com/account/applications/new
+
+* Set your Client Secret and Client ID as environment values in your Heroku instance
+
+ $ heroku config:add GITHUB_CLIENT_SECRET=....
+ $ heroku config:add GITHUB_CLIENT_ID=....
+
+* Push to Heroku
+
+ $ git push heroku master
+ Counting objects: 21, done.
+ Delta compression using up to 2 threads.
+ Compressing objects: 100% (19/19), done.
+ Writing objects: 100% (21/21), 3.92 KiB, done.
+ Total 21 (delta 3), reused 0 (delta 0)
+
+ -----> Heroku receiving push
+ -----> Sinatra app detected
+ -----> Gemfile detected, running Bundler version 1.0.7
+ Unresolved dependencies detected; Installing...
+ Using --without development:test
+
+ Fetching source index for http://rubygems.org/
+ Installing addressable (2.2.6)
+ Installing multipart-post (1.1.2)
+ Installing rack (1.3.0)
+ Installing faraday (0.6.1)
+ Installing json (1.5.3) with native extensions
+ Installing mime-types (1.16)
+ Installing multi_json (1.0.3)
+ Installing oauth2 (0.4.1)
+ Installing rest-client (1.6.3)
+ Installing tilt (1.3.2)
+ Installing sinatra (1.2.6)
+ Installing warden (1.0.4)
+ Installing warden-github (0.0.9)
+ Installing sinatra_auth_github (0.0.16)
+ Using bundler (1.0.7)
+ Your bundle is complete! It was installed into ./.bundle/gems/
+ -----> Compiled slug size is 1.4MB
+ -----> Launching... done, v8
+ http://ghmin.heroku.com deployed to Heroku
+
+ To git@heroku.com:ghmin.git
+ * [new branch] master -> master
+
+Boom.
+
+That should be it, you should now have a running application that allows users to authenticate via GitHub so you can now use the extensive API to do whatever you wish.
+
+Next you will probably want to read through the `app.rb` file and start modifying stuff. Good luck!
+
+
+
+
62 app.rb
@@ -0,0 +1,62 @@
+require 'sinatra'
+require 'sinatra_auth_github'
+
+class MinApp < Sinatra::Base
+ enable :sessions
+
+ # You should probably leave the ENV settings as they are and set them in your
+ # environment. If deploying to Heroku, for example, you would [run these][hr]
+ # commands once:
+ #
+ # $ heroku config:add GITHUB_CLIENT_SECRET=....
+ # $ heroku config:add GITHUB_CLIENT_ID=....
+ #
+ # Now when you deploy the app, Heroku will give you the correct values for this
+ #
+ # You will probably also want to choose the correct scope for what you want
+ # to do. See the GitHub [OAuth2 docs][oa] and go to the Scopes section to
+ # see what is available.
+ #
+ # [hr]: http://devcenter.heroku.com/articles/config-vars
+ # [oa]: http://developer.github.com/v3/oauth/
+ #
+ set :github_options, {
+ :secret => ENV['GITHUB_CLIENT_SECRET'],
+ :client_id => ENV['GITHUB_CLIENT_ID'],
+ :scopes => 'user'
+ }
+
+ register Sinatra::Auth::Github
+
+ # initial landing page - if the user is already authenticated, we simply
+ # redirect them to the main application method, otherwise we show them
+ # some sort of login screen
+ get '/' do
+ if github_user
+ redirect '/app'
+ else
+ erb :index
+ end
+ end
+
+ # main application method - you have a logged in user. if not, we redirect
+ # them back to the landing page
+ get '/app' do
+ if github_user
+ @user = github_user
+ erb :app
+ else
+ redirect '/'
+ end
+ end
+
+ get '/auth' do
+ authenticate!
+ redirect '/app'
+ end
+
+ get '/logout' do
+ logout!
+ redirect '/'
+ end
+end
@@ -0,0 +1,12 @@
+require "rubygems"
+require "bundler"
+Bundler.setup
+
+Bundler.require(:runtime)
+
+require './app'
+
+use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
+
+run MinApp
+
@@ -0,0 +1,3 @@
+Hello There, <%= @user.name %>!<br/>
+
+Token: <%= @user.token %>
@@ -0,0 +1,7 @@
+Main landing page. Make me pretty.
+
+<br/>
+
+<a href='/auth'>Login</a>
+
+

0 comments on commit efc6189

Please sign in to comment.