gatekeeper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/gatekeeper.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gatekeeper}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Dinn"]
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "gatekeeper.gemspec",
27
27
  "lib/gatekeeper.rb",
28
+ "lib/gatekeeper/helpers/authentication.rb",
28
29
  "lib/gatekeeper/helpers/rack.rb",
29
30
  "lib/gatekeeper/middleware.rb",
30
31
  "lib/gatekeeper/sso.rb",
@@ -0,0 +1,17 @@
1
+ module Gatekeeper
2
+ module Helpers
3
+ module Authentication
4
+ def current_user
5
+ session[:sso] && session[:sso][:user_id]
6
+ end
7
+
8
+ def is_admin?
9
+ (session[:sso][:is_admin?]=='true')
10
+ end
11
+
12
+ def is_manager_for?(hotink_account_id)
13
+ (session[:sso]["account_#{hotink_account_id.to_s}_manager".to_sym]=='true')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,42 +1,40 @@
1
1
  module Gatekeeper
2
- module Client
3
- module Helpers
4
- module Rack
5
- def sso_logged_in?
6
- session[:sso] && sso_user_id
7
- end
2
+ module Helpers
3
+ module Rack
4
+ def sso_logged_in?
5
+ session[:sso] && sso_user_id
6
+ end
8
7
 
9
- def sso_login_as(user_id, sreg_params)
10
- session.delete(:last_oidreq)
11
- session.delete('OpenID::Consumer::last_requested_endpoint')
12
- session.delete('OpenID::Consumer::DiscoveredServices::OpenID::Consumer::')
8
+ def sso_login_as(user_id, sreg_params)
9
+ session.delete(:last_oidreq)
10
+ session.delete('OpenID::Consumer::last_requested_endpoint')
11
+ session.delete('OpenID::Consumer::DiscoveredServices::OpenID::Consumer::')
13
12
 
14
- session[:sso] ||= { }
15
- session[:sso][:user_id] = user_id
16
- sreg_params.each { |key, value| session[:sso][key.to_sym] = value.to_s }
17
- end
13
+ session[:sso] ||= { }
14
+ session[:sso][:user_id] = user_id
15
+ sreg_params.each { |key, value| session[:sso][key.to_sym] = value.to_s }
16
+ end
18
17
 
19
- def sso_user_id
20
- session[:sso][:user_id]
21
- end
18
+ def sso_user_id
19
+ session[:sso][:user_id]
20
+ end
22
21
 
23
- def sso_user_email
24
- session[:sso][:email]
25
- end
22
+ def sso_user_email
23
+ session[:sso][:email]
24
+ end
26
25
 
27
- def absolute_url(suffix = nil)
28
- port_part = case request.scheme
29
- when "http"
30
- request.port == 80 ? "" : ":#{request.port}"
31
- when "https"
32
- request.port == 443 ? "" : ":#{request.port}"
33
- end
34
- "#{request.scheme}://#{request.host}#{port_part}#{suffix}"
35
- end
26
+ def absolute_url(suffix = nil)
27
+ port_part = case request.scheme
28
+ when "http"
29
+ request.port == 80 ? "" : ":#{request.port}"
30
+ when "https"
31
+ request.port == 443 ? "" : ":#{request.port}"
32
+ end
33
+ "#{request.scheme}://#{request.host}#{port_part}#{suffix}"
34
+ end
36
35
 
37
- def excluded_path?
38
- options.exclude_paths && options.exclude_paths.include?(request.path_info)
39
- end
36
+ def excluded_path?
37
+ options.exclude_paths && options.exclude_paths.include?(request.path_info)
40
38
  end
41
39
  end
42
40
  end
@@ -1,17 +1,15 @@
1
1
  module Gatekeeper
2
- module Client
3
- class Middleware < Sinatra::Base
4
- enable :raise_errors
5
- disable :show_exceptions
2
+ class Middleware < Sinatra::Base
3
+ enable :raise_errors
4
+ disable :show_exceptions
6
5
 
7
- set :sso_url, nil
8
- set :exclude_paths, nil
6
+ set :sso_url, nil
7
+ set :exclude_paths, nil
9
8
 
10
- def sso_url=(url)
11
- options.sso_url = url
12
- end
13
-
14
- register ::Gatekeeper::Client::SSO
9
+ def sso_url=(url)
10
+ options.sso_url = url
15
11
  end
12
+
13
+ register ::Gatekeeper::SSO
16
14
  end
17
15
  end
@@ -1,62 +1,60 @@
1
1
  module Gatekeeper
2
- module Client
3
- module SSO
4
- def self.registered(app)
5
- app.use(Rack::OpenID, OpenID::Store::Filesystem.new("#{Dir.tmpdir}/openid"))
6
- app.helpers Gatekeeper::Client::Helpers::Rack
2
+ module SSO
3
+ def self.registered(app)
4
+ app.use(Rack::OpenID, OpenID::Store::Filesystem.new("#{Dir.tmpdir}/openid"))
5
+ app.helpers Gatekeeper::Helpers::Rack
7
6
 
8
- app.get '/sso/login' do
9
- if contact_id = params['id']
10
- response['WWW-Authenticate'] = Rack::OpenID.build_header(
11
- :identifier => "#{options.sso_url}/users/#{contact_id}",
12
- :trust_root => absolute_url('/sso/login')
13
- )
14
- throw :halt, [401, 'got openid?']
15
- elsif openid = request.env["rack.openid.response"]
16
- if openid.status == :success
17
- if contact_id = openid.display_identifier.split("/").last
18
- sreg_params = openid.message.get_args("http://openid.net/extensions/sreg/1.1")
19
- sso_login_as(contact_id, sreg_params)
20
-
21
- if session['sso_return_to']
22
- begin
23
- return_url = URI.parse(session['sso_return_to'])
24
-
25
- unless return_url.host==request.host
26
- user_token = UserToken.create!(:user_id => sso_user_id)
27
- if return_url.query==nil
28
- return_url.query = "user_token=#{user_token.token}"
29
- else
30
- return_url.query = "user_token=#{user_token.token}&#{return_url.query}"
31
- end
7
+ app.get '/sso/login' do
8
+ if contact_id = params['id']
9
+ response['WWW-Authenticate'] = Rack::OpenID.build_header(
10
+ :identifier => "#{options.sso_url}/users/#{contact_id}",
11
+ :trust_root => absolute_url('/sso/login')
12
+ )
13
+ throw :halt, [401, 'got openid?']
14
+ elsif openid = request.env["rack.openid.response"]
15
+ if openid.status == :success
16
+ if contact_id = openid.display_identifier.split("/").last
17
+ sreg_params = openid.message.get_args("http://openid.net/extensions/sreg/1.1")
18
+ sso_login_as(contact_id, sreg_params)
19
+
20
+ if session['sso_return_to']
21
+ begin
22
+ return_url = URI.parse(session['sso_return_to'])
23
+
24
+ unless return_url.host==request.host
25
+ user_token = UserToken.create!(:user_id => sso_user_id)
26
+ if return_url.query==nil
27
+ return_url.query = "user_token=#{user_token.token}"
28
+ else
29
+ return_url.query = "user_token=#{user_token.token}&#{return_url.query}"
32
30
  end
33
-
34
- redirect return_url.to_s
35
- rescue
36
- redirect '/'
37
- ensure
38
- session['sso_return_to'] = nil
39
31
  end
40
- else
32
+
33
+ redirect return_url.to_s
34
+ rescue
41
35
  redirect '/'
36
+ ensure
37
+ session['sso_return_to'] = nil
42
38
  end
43
-
44
39
  else
45
- raise "No contact could be found for #{openid.display_identifier}"
40
+ redirect '/'
46
41
  end
42
+
47
43
  else
48
- throw :halt, [503, "Error: #{openid.status}"]
44
+ raise "No contact could be found for #{openid.display_identifier}"
49
45
  end
50
46
  else
51
- session['sso_return_to'] = params[:return_to] if params[:return_to]
52
- redirect "#{options.sso_url}/login?return_to=#{absolute_url('/sso/login')}"
47
+ throw :halt, [503, "Error: #{openid.status}"]
53
48
  end
49
+ else
50
+ session['sso_return_to'] = params[:return_to] if params[:return_to]
51
+ redirect "#{options.sso_url}/login?return_to=#{absolute_url('/sso/login')}"
54
52
  end
53
+ end
55
54
 
56
- app.get '/sso/logout' do
57
- session[:sso] = nil
58
- redirect "#{options.sso_url}/logout"
59
- end
55
+ app.get '/sso/logout' do
56
+ session[:sso] = nil
57
+ redirect "#{options.sso_url}/logout"
60
58
  end
61
59
  end
62
60
  end
@@ -5,7 +5,7 @@ SSO_URL = "http://ssourl.local/sso"
5
5
  class TestApp < Sinatra::Base
6
6
  enable :sessions
7
7
 
8
- use Gatekeeper::Client::Middleware do |sso|
8
+ use Gatekeeper::Middleware do |sso|
9
9
  sso.sso_url = SSO_URL
10
10
  end
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gatekeeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Dinn
@@ -40,6 +40,7 @@ files:
40
40
  - VERSION
41
41
  - gatekeeper.gemspec
42
42
  - lib/gatekeeper.rb
43
+ - lib/gatekeeper/helpers/authentication.rb
43
44
  - lib/gatekeeper/helpers/rack.rb
44
45
  - lib/gatekeeper/middleware.rb
45
46
  - lib/gatekeeper/sso.rb