lib/sinatra/auth/github.rb in sinatra_auth_github-0.0.15 vs lib/sinatra/auth/github.rb in sinatra_auth_github-0.0.16
- old
+ new
@@ -33,26 +33,37 @@
def github_user
warden.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)
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)
+ end
+
+ def github_organization_team_access?(name, team)
+ members = github_request("teams/#{team}/members")["users"]
+ members.map { |user| user["login"] }.include?(github_user.login)
+ rescue RestClient::Unauthorized => e
+ false
+ end
+
+ # Auth only certain individuals
def github_organization_authenticate!(name)
authenticate!
- orgs = github_request("user/show/#{github_user.login}/organizations")["organizations"]
- halt([401, "Unauthorized User"]) unless orgs.map { |org| org["login"] }.include?(name)
+ halt([401, "Unauthorized User"]) unless github_organization_access?(name)
end
def github_organization_team_authenticate!(name, team)
authenticate!
- members = github_request("teams/#{team}/members")["users"]
- halt([401, "Unauthorized User"]) unless members.map { |user| user["login"] }.include?(github_user.login)
- rescue RestClient::Unauthorized => e
- halt([401, "Unauthorized User"])
+ halt([401, "Unauthorized User"]) unless github_organization_team_access?(name, team)
end
def _relative_url_for(path)
request.script_name + path
end