Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sinatra auth with public org access

  • Loading branch information...
commit 29169559fd9aa1b149a10637018c70a649711836 1 parent e747714
@atmos atmos authored
Showing with 24 additions and 1 deletion.
  1. +22 −0 lib/sinatra/auth/github.rb
  2. +2 −1  sinatra_auth_github.gemspec
View
22 lib/sinatra/auth/github.rb
@@ -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
@@ -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)
View
3  sinatra_auth_github.gemspec
@@ -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"]
@@ -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"
Please sign in to comment.
Something went wrong with that request. Please try again.