rack-webauth 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ # -*- mode:ruby -*-
2
+
3
+ $: << '../../lib/'
4
+ require 'rack-webauth/test'
5
+
6
+ use Rack::Webauth::Test, :user => "nilclass", :mail => "niklas@brueckenschlaeger.de"
7
+
8
+ use Rack::Webauth
9
+
10
+ run lambda { |env|
11
+ user = Rack::Webauth::User.new(env[Rack::Webauth::NS])
12
+
13
+ $stderr.puts "LOGIN: #{user.login}"
14
+ $stderr.puts "MAIL: #{user[:mail]}"
15
+ [200, { "Content-Type" => "text/html" },
16
+ ['<h1>', "All fine. Check logs.", '</h1>',
17
+ '<pre>', env.inspect,'</pre>']]
18
+ }
data/lib/rack-webauth.rb CHANGED
@@ -71,14 +71,37 @@ class Rack::Webauth
71
71
  (respond_to?(:request) &&
72
72
  request.respond_to?(:env) ?
73
73
  request.env[NS] :
74
- (raise "Neither 'env' nor 'request.env' available. Can't access webauth-info")))
74
+ (raise Rack::Webauth::Info::NotAvailable.new("Neither 'env' nor 'request.env' available. Can't access webauth-info"))))
75
+ end
76
+ end
77
+
78
+ # A default User object, to easily access attributes.
79
+ # Used by WardenStrategy.
80
+ class User
81
+ attr :login
82
+
83
+ def initialize(webauth_info)
84
+ @webauth_info = webauth_info
85
+ @login = @webauth_info.login
86
+ end
87
+
88
+ def [](attribute)
89
+ @webauth_info.attributes[attribute.to_s.upcase]
75
90
  end
76
91
  end
77
92
 
78
93
  # Detects & provides webauth related information conveniently from
79
94
  # the rack environment.
95
+ #
96
+ # See README and Rack::Webauth::Helpers for usage information & examples.
97
+ #
80
98
  class Info
99
+ # Exception raised by Rack::Webauth::Helpers if webauth
100
+ # cannot be accessed / is not available.
101
+ class NotAvailable < Exception ; end
102
+
81
103
  attr :login
104
+ attr :env
82
105
  # explains itself.
83
106
  def logged_in? ; @logged_in ; end
84
107
 
@@ -184,6 +207,7 @@ class Rack::Webauth
184
207
  else
185
208
  # key isn't webauthldap related
186
209
  end
210
+ next(attrs)
187
211
  end
188
212
  end
189
213
  end
@@ -0,0 +1,56 @@
1
+ require 'rack-webauth'
2
+
3
+ # Middleware to use for testing in situations where WebAuth is not
4
+ # available, such as development environments.
5
+ #
6
+ # Example:
7
+ # use(Rack::Webauth::Test,
8
+ # :user => "test-user",
9
+ # :mail => "someone@example.com")
10
+ #
11
+ # use(Rack::Webauth)
12
+ #
13
+ # run lambda {|env|
14
+ # env["WEBAUTH_USER"] #=> "test-user"
15
+ # env["WEBAUTH_LDAP_MAIL"] #=> "someone@example.com"
16
+ # env[Rack::Webauth::NS].login #=> "test-user"
17
+ # env[Rack::Webauth::NS].attributes['mail'] #=> "someone@example.com"
18
+ # }
19
+ #
20
+ #
21
+ # In order to work correctly, Rack::Webauth::Test must come before
22
+ # Rack::Webauth in the middleware stack.
23
+ #
24
+ class Rack::Webauth::Test
25
+ attr_reader :app, :env_vars
26
+
27
+ def initialize(app, env_vars)
28
+ @app, @env_vars = app, env_vars
29
+ end
30
+
31
+ def call(env)
32
+ env_vars.each_pair do |key, value|
33
+ add_to_env(env, key, value)
34
+ end
35
+ app.call(env)
36
+ end
37
+
38
+ private
39
+
40
+ def add_to_env(env, key, value)
41
+ normalized_key = key.to_s.upcase
42
+ if %w(USER TOKEN_LASTUSED TOKEN_EXPIRATION
43
+ LDAPAUTHRULE LDAPPRIVGROUP).include?(normalized_key)
44
+ # regular setting
45
+ env["WEBAUTH_#{normalized_key}"] = value
46
+ elsif value.kind_of?(Array)
47
+ # multi-valued LDAP attribute
48
+ value.each_with_index do |val, index|
49
+ env["WEBAUTH_LDAP_#{normalized_key}#{index + 1}"] = val
50
+ end
51
+ else
52
+ # signle-valued LDAP attribute
53
+ env["WEBAUTH_LDAP_#{normalized_key}"] = value
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,92 @@
1
+ require 'rack-webauth'
2
+
3
+ unless defined?(Warden)
4
+ raise "Can't define warden strategy, as Warden isn't available. Get it from https://github.com/hassox/warden/"
5
+ end
6
+
7
+ # Basic strategy for Warden, a authentication framework for Rack.
8
+ #
9
+ # For more information about warden, see https://github.com/hassox/warden/
10
+ #
11
+ # You can either use this "as is", which will give you a
12
+ # Rack::Webauth::User object to work with, or tie it to
13
+ # your own User objects, by setting the finder.
14
+ #
15
+ # See Rack::Webauth::WardenStrategy.finder for more information.
16
+ #
17
+ # For information on how to use this in Devise, see documentation
18
+ # of Rack::Webauth::WardenStrategy::InstanceMethods
19
+ #
20
+ class Rack::Webauth::WardenStrategy < Warden::Strategies::Base
21
+ #
22
+ # Actual functionality of WardenStrategy, so it can be used
23
+ # within other classes as well, without the need to inherit
24
+ # from Warden::Strategies::Base.
25
+ #
26
+ # Especially useful when using devise:
27
+ #
28
+ # class MyWebauthStrategy < Devise::Strategies::Authenticatable
29
+ # include Rack::Webauth::WardenStrategy::InstanceMethods
30
+ #
31
+ # self.finder = lambda {
32
+ # mapping.to.find_by_email(webauth.attributes['mail'])
33
+ # }
34
+ # end
35
+ #
36
+ # For more information about Devise see https://github.com/plataformatec/devise
37
+ #
38
+ module InstanceMethods
39
+ def self.included(base)
40
+ base.extend(ClassMethods)
41
+ end
42
+
43
+ include Rack::Webauth::Helpers
44
+
45
+ def valid?
46
+ webauth
47
+ true
48
+ rescue Rack::Webauth::Info::NotAvailable
49
+ false
50
+ end
51
+
52
+ def authenticate!
53
+ if webauth.logged_in?
54
+ if user = instance_eval(&self.class.finder)
55
+ success!(user)
56
+ else
57
+ fail!(:invalid)
58
+ end
59
+ else
60
+ fail!
61
+ end
62
+ end
63
+ end
64
+
65
+ module ClassMethods
66
+ def self.extended(base)
67
+ class << base
68
+ attr_writer :finder
69
+ end
70
+ end
71
+
72
+ # Default user finder. By default initializes a
73
+ # Rack::Webauth::User. You can set it to something
74
+ # else:
75
+ #
76
+ # Rack::Webauth::WardenStrategy.finder = lambda {
77
+ # MyUserModel.find_by_email_address(webauth.attributes['mail'])
78
+ # }
79
+ #
80
+ # The finder will be evaluated inside the strategy instance,
81
+ # so you have access to "webauth", "env", ...
82
+ def finder
83
+ @finder ||= lambda {
84
+ Rack::Webauth::User.new(webauth)
85
+ }
86
+ end
87
+ end
88
+
89
+ include(InstanceMethods)
90
+ end
91
+
92
+ Warden::Strategies.add(:webauth, Rack::Webauth::WardenStrategy)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-webauth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Niklas E. Cathor
@@ -46,6 +46,9 @@ files:
46
46
  - README.textile
47
47
  - COPYING
48
48
  - COPYING.LESSER
49
+ - lib/rack-webauth/test.rb
50
+ - lib/rack-webauth/warden_strategy.rb
51
+ - examples/test/config.ru
49
52
  has_rdoc: true
50
53
  homepage:
51
54
  licenses: []