This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
github.rb
65 lines (52 loc) · 1.57 KB
/
github.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'sinatra/base'
require 'warden-github'
module Sinatra
module Auth
module Github
class BadAuthentication < Sinatra::Base
get '/unauthenticated' do
status 403
"Unable to authenticate, sorry bud."
end
end
module Helpers
def warden
env['warden']
end
def authenticate!(*args)
warden.authenticate!(*args)
end
def authenticated?(*args)
warden.authenticated?(*args)
end
def logout!
warden.logout
end
def github_user
warden.user
end
def github_request(path)
response = RestClient.get("https://github.com/api/v2/json/#{path}", {:accept => :json, :params => {:token => github_user.token}})
JSON.parse(response.body)
end
def _relative_url_for(path)
request.script_name + path
end
end
def self.registered(app)
app.use Warden::Manager do |manager|
manager.default_strategies :github
manager.failure_app = app.github_options[:failure_app] || BadAuthentication
manager[:github_secret] = app.github_options[:secret]
manager[:github_client_id] = app.github_options[:client_id]
manager[:github_callback_url] = app.github_options[:callback_url] || '/auth/github/callback'
end
app.helpers Helpers
app.get '/auth/github/callback' do
authenticate!
redirect _relative_url_for('/')
end
end
end
end
end