diff --git a/lib/sinatra/auth/github.rb b/lib/sinatra/auth/github.rb index 8a49eb2..faef877 100644 --- a/lib/sinatra/auth/github.rb +++ b/lib/sinatra/auth/github.rb @@ -36,20 +36,24 @@ def github_user end # API Requests - def github_request(path) - response = RestClient.get "https://github.com/api/v2/json/#{path}", :params => { :access_token => github_user.token }, :accept => :json - JSON.parse(response.body) + def github_request(path, parse_response = true) + response = RestClient.get("https://api.github.com/#{path}", :params => { :access_token => github_user.token }, :accept => :json) + if parse_response + JSON.parse(response.body) + else + response + end end # Access Inquiries def github_organization_access?(name) - orgs = github_request("user/show/#{github_user.login}/organizations")["organizations"] - orgs.map { |org| org["login"] }.include?(name) + response = github_request("orgs/#{name}/members/#{github_user.login}", false) + response.to_i == 204 end - def github_organization_team_access?(name, team) - members = github_request("teams/#{team}/members")["users"] - members.map { |user| user["login"] }.include?(github_user.login) + def github_organization_team_access?(team) + response = github_request("teams/#{team}/members/#{github_user.login}", false) + response.to_i == 204 rescue RestClient::Unauthorized => e false end @@ -60,9 +64,9 @@ def github_organization_authenticate!(name) halt([401, "Unauthorized User"]) unless github_organization_access?(name) end - def github_organization_team_authenticate!(name, team) + def github_organization_team_authenticate!(team) authenticate! - halt([401, "Unauthorized User"]) unless github_organization_team_access?(name, team) + halt([401, "Unauthorized User"]) unless github_organization_team_access?(team) end def _relative_url_for(path) diff --git a/spec/app.rb b/spec/app.rb index 9cf1495..d8151a8 100644 --- a/spec/app.rb +++ b/spec/app.rb @@ -14,7 +14,7 @@ class App < Sinatra::Base helpers do def repos - github_request("repos/show/#{github_user.login}") + github_request("user/repos") end end @@ -28,9 +28,9 @@ def repos "Hello There, #{github_user.name}! You have access to the #{params['id']} organization." end - get '/orgs/:org_id/team/:id' do - github_organization_team_authenticate!(params['org_id'], params['id']) - "Hello There, #{github_user.name}! You have access to the #{params['id']} team under the #{params['org_id']} organization." + get '/teams/:id' do + github_organization_team_authenticate!(params['id']) + "Hello There, #{github_user.name}! You have access to the #{params['id']} team." end get '/logout' do