-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
129 lines (95 loc) · 2.64 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
module Github
class Explorer < Sinatra::Base
get '/' do
@path, resp = explore(params[:path])
@json = JSON.pretty_generate(resp.body) # only if resp.body
erb :index
end
get '/routes' do
@categories = Category.all
erb :routes
end
get '/user' do
@user = current_user
erb :user
end
get '/bouncer' do
erb :bouncer
end
private
def explore(path)
if path.to_s.empty?
path = current_user ? '/user' : '/users/jakubsvehla'
end
# Normalize the path
path = path.prepend '/' unless path.start_with? '/'
# Does the path match any GET route?
route = Route.match(path)
if route && current_user
current_user.explored!(route)
end
resp = connection.get(path)
# Unauthorized!
if resp.status == 401
redirect '/bouncer'
end
[path, resp]
end
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
def connection
return @connection if defined? @connection
@connection = Faraday.new 'https://api.github.com/' do |builder|
builder.use Github::Response::ParseJson
builder.adapter Faraday.default_adapter
end
@connection.params[:access_token] = current_user.access_token if current_user
@connection.headers[:user_agent] = "GitHub Explorer"
@connection
end
end
class Auth < Sinatra::Base
get '/login' do
redirect client.auth_code.authorize_url(redirect_uri: redirect_uri)
end
get '/callback' do
access_token = client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
auth = access_token.get('/user').parsed
user = User.find_or_create_by_auth_hash(auth)
user.access_token = access_token.token
user.save
session[:user_id] = user.id
redirect '/'
end
get '/logout' do
session[:user_id] = nil
redirect '/'
end
private
def client
@client ||= OAuth2::Client.new(ENV['CLIENT_ID'], ENV['CLIENT_SECRET'], {
site: 'https://api.github.com',
authorize_url: 'https://github.com/login/oauth/authorize',
token_url: 'https://github.com/login/oauth/access_token'
})
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/callback'
uri.query = nil
uri.to_s
end
end
module Response
class ParseJson < Faraday::Response::Middleware
def on_complete(env)
if env[:body].empty?
env[:body] = nil
else
env[:body] = MultiJson.load(env[:body])
end
end
end
end
end