Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
sinatra auth with public org access
Browse files Browse the repository at this point in the history
  • Loading branch information
atmos committed Sep 21, 2011
1 parent e747714 commit 2916955
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/sinatra/auth/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def github_request(path)
JSON.parse(github_raw_request(path))
end

# See if the user is a public member of the named organization
#
# name - the organization name
#
# Returns: true if the user is public access, false otherwise
def github_public_organization_access?(name)
orgs = github_request("orgs/#{name}/public_members")
orgs.map { |org| org["login"] }.include?(github_user.login)
rescue RestClient::Forbidden, RestClient::Unauthorized, RestClient::ResourceNotFound => e
false
end

# See if the user is a member of the named organization
#
# name - the organization name
Expand Down Expand Up @@ -114,6 +126,16 @@ def github_team_access?(team_id)
# name - the organization to test membership against
#
# Returns an execution halt if the user is not a member of the named org
def github_public_organization_authenticate!(name)
authenticate!
halt([401, "Unauthorized User"]) unless github_public_organization_access?(name)
end

# Enforce user membership to the named organization if membership is publicized
#
# name - the organization to test membership against
#
# Returns an execution halt if the user is not a member of the named org
def github_organization_authenticate!(name)
authenticate!
halt([401, "Unauthorized User"]) unless github_organization_access?(name)
Expand Down
3 changes: 2 additions & 1 deletion sinatra_auth_github.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "sinatra_auth_github"
s.version = "0.1.3"
s.version = "0.1.4"
s.platform = Gem::Platform::RUBY
s.authors = ["Corey Donohoe"]
s.email = ["atmos@atmos.org"]
Expand All @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~>1.3.0"
s.add_development_dependency "shotgun"
s.add_development_dependency "ZenTest", "~>4.5.0"
s.add_development_dependency "bundler", "~>1.0"
s.add_development_dependency "randexp", "~>0.1.5"
s.add_development_dependency "rack-test", "~>0.5.3"
Expand Down

0 comments on commit 2916955

Please sign in to comment.