Skip to content
Browse files

wip

  • Loading branch information...
1 parent 99c09e1 commit 00bbafcc769e72f7b3f6999545d34eada198ab9b @atmos committed Jun 3, 2010
Showing with 38 additions and 10 deletions.
  1. +3 −1 Gemfile
  2. +22 −3 lib/sinatra/auth/github.rb
  3. +13 −6 spec/app.rb
View
4 Gemfile
@@ -2,7 +2,8 @@ source :gemcutter
group :runtime do
gem 'sinatra', '~>1.0'
- gem 'warden-github', '~>0.0.4'
+ gem 'emk-sinatra-url-for', '~>0.2.1', :require => 'sinatra/url_for'
+ gem 'warden-github', '~>0.0.5'
end
group :test do
@@ -14,6 +15,7 @@ group :test do
gem 'fakeweb', '>=1.2.8'
gem 'randexp', '>=0.1.4'
gem 'rack-test', '~>0.5.3', :require => 'rack/test'
+ gem 'ruby-debug'
end
# vim:ft=ruby
View
25 lib/sinatra/auth/github.rb
@@ -1,4 +1,5 @@
require 'sinatra/base'
+require 'sinatra/url_for'
require 'warden-github'
module Sinatra
@@ -32,8 +33,25 @@ def github_user
warden.user
end
- def relative_url_for(path)
- request.script_name + path
+ def github_oauth_proxy
+ @github_oauth_proxy ||=
+ Warden::Github::Oauth::Proxy.new(_github_client, _github_secret, _oauth_callback_url)
+ end
+
+ def _github_client
+ options.github_options[:client_id]
+ end
+
+ def _github_secret
+ options.github_options[:secret]
+ end
+
+ def _callback_url
+ options.github_options[:callback_url] || '/auth/github/callback'
+ end
+
+ def _oauth_callback_url
+ url_for _callback_url, :full
end
end
@@ -49,10 +67,11 @@ def self.registered(app)
end
app.helpers Helpers
+ app.helpers Sinatra::UrlForHelper
app.get '/auth/github/callback' do
authenticate!
- redirect relative_url_for('/')
+ redirect url_for '/'
end
end
end
View
19 spec/app.rb
@@ -1,22 +1,29 @@
+require 'pp'
+
module Example
class App < Sinatra::Base
enable :sessions
- set :github_options, {:client_id => ENV['GH_CLIENT_ID'],
- :secret => ENV['GH_SECRET'] }
+ set :github_options, {:client_id => ENV['GH_CLIENT_ID'], :secret => ENV['GH_SECRET'] }
register Sinatra::Auth::Github
before do
authenticate!
end
- get '/' do
- "Hello There, #{github_user.name}!"
+ helpers do
+ def repos
+ JSON.parse(access_token.get("/api/v2/json/repos/show/#{github_user.attribs['login']}"))
+ end
+
+ def access_token
+ @access_token ||= github_oauth_proxy.access_token_for(github_user.token)
+ end
end
- get '/another_route' do
- "Hello There, #{github_user.name}!"
+ get '/' do
+ "Hello There, #{github_user.name}!\n#{repos.inspect}"
end
get '/logout' do

0 comments on commit 00bbafc

Please sign in to comment.
Something went wrong with that request. Please try again.