Skip to content

Commit 6897ec7

Browse files
committed
update basics of auth for editable scopes
1 parent 01c647d commit 6897ec7

File tree

9 files changed

+112
-95
lines changed

9 files changed

+112
-95
lines changed

api/ruby/basics-of-authentication/Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ source "http://rubygems.org"
33
gem "json", "1.7.7"
44
gem 'sinatra', '~> 1.3.5'
55
gem 'rest-client', '~> 1.6.3'
6-
gem 'sinatra_auth_github', '~> 0.13.3'
Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4-
addressable (2.3.3)
5-
faraday (0.8.6)
6-
multipart-post (~> 1.1)
7-
faraday_middleware (0.9.0)
8-
faraday (>= 0.7.4, < 0.9)
9-
hashie (1.2.0)
104
json (1.7.7)
115
mime-types (1.21)
12-
multi_json (1.6.1)
13-
multipart-post (1.2.0)
14-
netrc (0.7.7)
15-
octokit (1.23.0)
16-
addressable (~> 2.2)
17-
faraday (~> 0.8)
18-
faraday_middleware (~> 0.9)
19-
hashie (~> 1.2)
20-
multi_json (~> 1.3)
21-
netrc (~> 0.7.7)
226
rack (1.5.2)
237
rack-protection (1.3.2)
248
rack
@@ -28,15 +12,7 @@ GEM
2812
rack (~> 1.4)
2913
rack-protection (~> 1.3)
3014
tilt (~> 1.3, >= 1.3.3)
31-
sinatra_auth_github (0.13.3)
32-
sinatra (~> 1.0)
33-
warden-github (~> 0.13.1)
3415
tilt (1.3.3)
35-
warden (1.2.1)
36-
rack (>= 1.0)
37-
warden-github (0.13.2)
38-
octokit (>= 1.22.0)
39-
warden (> 1.0)
4016

4117
PLATFORMS
4218
ruby
@@ -45,4 +21,3 @@ DEPENDENCIES
4521
json (= 1.7.7)
4622
rest-client (~> 1.6.3)
4723
sinatra (~> 1.3.5)
48-
sinatra_auth_github (~> 0.13.3)

api/ruby/basics-of-authentication/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ It consists of two different servers: one built correctly, and one built less op
99
To run these projects, make sure you have [Bundler][bundler] installed; then type
1010
`bundle install` on the command line.
1111

12-
For the "less optimal" server, type `ruby server.rb` on the command line.
13-
This will run the server at `localhost:4567`.
12+
For the "less optimal" server, type `ruby server.rb` on the command line.
1413

15-
For the correct server, enter `rackup -p 4567` on the command line.
14+
For the correct server, enter `ruby advanced_server.rb` on the command line.
15+
16+
Both commands will run the server at `localhost:4567`.
1617

1718
[basics of auth]: http://developer.github.com/guides/basics-of-authentication/
1819
[bundler]: http://gembundler.com/
Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
1-
require 'sinatra/auth/github'
1+
require 'sinatra'
22
require 'rest_client'
3-
4-
module Example
5-
class MyBasicApp < Sinatra::Base
6-
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
7-
# Instead, set and test environment variables, like below
8-
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
9-
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
10-
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
11-
# end
12-
13-
CLIENT_ID = ENV['GH_BASIC_CLIENT_ID']
14-
CLIENT_SECRET = ENV['GH_BASIC_SECRET_ID']
15-
16-
enable :sessions
17-
18-
set :github_options, {
19-
:scopes => "user",
20-
:secret => CLIENT_SECRET,
21-
:client_id => CLIENT_ID,
22-
:callback_url => "/callback"
23-
}
24-
25-
register Sinatra::Auth::Github
26-
27-
get '/' do
28-
if !authenticated?
29-
authenticate!
30-
else
31-
access_token = github_user["token"]
32-
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
33-
:accept => :json})
34-
35-
auth_result = JSON.parse(auth_result)
36-
37-
erb :advanced, :locals => {:login => auth_result["login"],
38-
:hire_status => auth_result["hireable"] ? "hireable" : "not hireable"}
39-
end
3+
require 'json'
4+
5+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
6+
# Instead, set and test environment variables, like below
7+
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
8+
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
9+
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
10+
# end
11+
12+
CLIENT_ID = ENV['GH_BASIC_CLIENT_ID']
13+
CLIENT_SECRET = ENV['GH_BASIC_SECRET_ID']
14+
15+
use Rack::Session::Cookie, :secret => rand.to_s()
16+
17+
def authenticated?
18+
puts session[:access_token]
19+
session[:access_token]
20+
end
21+
22+
def authenticate!
23+
erb :index, :locals => {:client_id => CLIENT_ID}
24+
end
25+
26+
get '/' do
27+
if !authenticated?
28+
authenticate!
29+
else
30+
access_token = session[:access_token]
31+
scopes = session[:scopes]
32+
33+
auth_result = JSON.parse(RestClient.get("https://api.github.com/user",
34+
{:params => {:access_token => access_token},
35+
:accept => :json}))
36+
37+
if scopes.include? 'user:email'
38+
auth_result['private_emails'] =
39+
JSON.parse(RestClient.get("https://api.github.com/user/emails",
40+
{:params => {:access_token => access_token},
41+
:accept => :json}))
4042
end
4143

42-
get '/callback' do
43-
if authenticated?
44-
redirect "/"
45-
else
46-
authenticate!
47-
end
48-
end
44+
erb :advanced, :locals => {:login => auth_result["login"],
45+
:public_email => auth_result["email"],
46+
:private_emails => auth_result["private_emails"]}
4947
end
50-
end
48+
end
49+
50+
get '/callback' do
51+
session_code = request.env['rack.request.query_hash']["code"]
52+
53+
result = RestClient.post("https://github.com/login/oauth/access_token",
54+
{:client_id => CLIENT_ID,
55+
:client_secret => CLIENT_SECRET,
56+
:code => session_code},
57+
:accept => :json)
58+
59+
session[:access_token] = JSON.parse(result)["access_token"]
60+
session[:scopes] = JSON.parse(result)["scope"].split(",")
61+
62+
redirect '/'
63+
end

api/ruby/basics-of-authentication/config.ru

Lines changed: 0 additions & 7 deletions
This file was deleted.

api/ruby/basics-of-authentication/server.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,30 @@
1818
get '/callback' do
1919
# get temporary GitHub code...
2020
session_code = request.env['rack.request.query_hash']["code"]
21+
2122
# ... and POST it back to GitHub
2223
result = RestClient.post("https://github.com/login/oauth/access_token",
2324
{:client_id => CLIENT_ID,
2425
:client_secret => CLIENT_SECRET,
2526
:code => session_code},
2627
:accept => :json)
28+
29+
# extract token and granted scopes
2730
access_token = JSON.parse(result)["access_token"]
31+
scopes = JSON.parse(result)["scope"].split(",")
2832

29-
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
30-
:accept => :json})
33+
# fetch user information
34+
auth_result = JSON.parse(RestClient.get("https://api.github.com/user",
35+
{:params => {:access_token => access_token},
36+
:accept => :json}))
3137

32-
auth_result = JSON.parse(auth_result)
38+
# if the user authorized it, fetch private emails
39+
if scopes.include? 'user:email'
40+
auth_result['private_emails'] =
41+
JSON.parse(RestClient.get("https://api.github.com/user/emails",
42+
{:params => {:access_token => access_token},
43+
:accept => :json}))
44+
end
3345

34-
erb :basic, :locals => {:login => auth_result["login"],
35-
:hire_status => auth_result["hireable"] ? "hireable" : "not hireable"
36-
}
37-
end
46+
erb :basic, :locals => auth_result
47+
end

api/ruby/basics-of-authentication/views/advanced.erb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
</head>
77
<body>
8-
<p>Well, well, well, <%= login %>! It looks like you're <em>still</em> <%= hire_status %>!</p>
8+
<p>Well, well, well, <%= login %>!</p>
9+
<p>
10+
<% if !public_email.empty? %> It looks like your public email address is <%= public_email %>.
11+
<% else %> It looks like you don't have a public email. That's cool.
12+
<% end %>
13+
</p>
14+
<p>
15+
<% if defined? private_emails %>
16+
With your permission, we were also able to dig up your private email addresses:
17+
<%= private_emails.join(", ") %>
18+
<% else %>
19+
Also, you're a bit secretive about your private email addresses.
20+
<% end %>
21+
</p>
922
</body>
10-
</html>
23+
</html>

api/ruby/basics-of-authentication/views/basic.erb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
</head>
77
<body>
8-
<p>Hello, <%= login %>! It looks like you're <%= hire_status %>.</p>
8+
<p>Hello, <%= login %>!</p>
9+
<p>
10+
<% if !email.empty? %> It looks like your public email address is <%= email %>.
11+
<% else %> It looks like you don't have a public email. That's cool.
12+
<% end %>
13+
</p>
14+
<p>
15+
<% if defined? private_emails %>
16+
With your permission, we were also able to dig up your private email addresses:
17+
<%= private_emails.join(", ") %>
18+
<% else %>
19+
Also, you're a bit secretive about your private email addresses.
20+
<% end %>
21+
</p>
922
</body>
10-
</html>
23+
</html>

api/ruby/basics-of-authentication/views/index.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<p>Well, hello there!</p>
9-
<p>We're going to now talk to the GitHub API. Ready? <a href="https://github.com/login/oauth/authorize?client_id=<%= client_id %>">Click here</a> to begin!</a></p>
9+
<p>We're going to now talk to the GitHub API. Ready? <a href="https://github.com/login/oauth/authorize?scope=user:email&client_id=<%= client_id %>">Click here</a> to begin!</a></p>
1010
<p>If that link doesn't work, remember to provide your own <a href="http://developer.github.com/v3/oauth/#web-application-flow">Client ID</a>!</p>
1111
</body>
12-
</html>
12+
</html>

0 commit comments

Comments
 (0)