devise_oam 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@ integration with Oracle Access Manager.
8
8
  ## Installation
9
9
  In **Rails 3**, add this to your Gemfile and run the `bundle` command.
10
10
 
11
- gem "devise_oam", github: "whatthewhat/devise_oam"
11
+ gem "devise_oam", "~> 0.0.3"
12
12
 
13
13
  ## Usage
14
14
  1) Add the `HeaderAuthenticatable` strategy in devise initializer `config/initializers/devise.rb`:
@@ -38,7 +38,10 @@ end
38
38
  * `create_user_if_not_found` - if set to true this will create a new user if no user was found
39
39
  * `create_user_method` - method in the `user_class` to handle new user creation
40
40
  * `ldap_header` - HTTP header for LDAP roles
41
- * `roles_setter` - method in the `user_class` to handle updating user roles
41
+ * `update_user_method` - method in the `user_class` to handle updating user roles and additional attributes
42
+ * `attr_headers` - headers with additional attributes that are passed to `update_user_method`
43
+
44
+ `roles_setter` should still work, but is deprecated
42
45
 
43
46
  ### Automatic user creation
44
47
  If you need to automatically create new users based on `oam_header` you need to do the following:
@@ -54,7 +57,7 @@ To use LDAP roles parsing:
54
57
 
55
58
  1. Set `ldap_header` setting to the HTTP header with roles (should be a comma separated string)
56
59
  2. Add a method to your user class that will accept an array with roles and update the user
57
- 3. In the initializer set `roles_setter` setting to the method you've just created
60
+ 3. In the initializer set `update_user_method` setting to the method you've just created
58
61
 
59
62
  For an example see `test/dummy` app.
60
63
 
@@ -1,10 +1,11 @@
1
1
  module DeviseOam
2
2
  class AuthenticatableEntity
3
- attr_accessor :login, :ldap_roles
3
+ attr_accessor :login, :ldap_roles, :attributes
4
4
 
5
- def initialize(login, ldap_roles = nil)
5
+ def initialize(login, ldap_roles = nil, attributes = {})
6
6
  @login = login
7
7
  @ldap_roles = parse_ldap_roles(ldap_roles) if ldap_roles
8
+ @attributes = attributes
8
9
  end
9
10
 
10
11
  private
@@ -12,4 +13,4 @@ module DeviseOam
12
13
  ldap_roles.strip.downcase.split(',')
13
14
  end
14
15
  end
15
- end
16
+ end
@@ -4,24 +4,20 @@ module DeviseOam
4
4
  class HeaderAuthenticatable < ::Devise::Strategies::Base
5
5
  attr_reader :authenticatable
6
6
 
7
+ # strategy is only valid if there is a DeviseOam.oam_header header in the request
7
8
  def valid?
8
- # this strategy is only valid if there is a DeviseOam.oam_header header in the request
9
9
  request.headers[DeviseOam.oam_header]
10
10
  end
11
11
 
12
12
  def authenticate!
13
- failure_message = "OAM authentication failed"
14
-
15
13
  oam_data = request.headers[DeviseOam.oam_header]
16
- if DeviseOam.ldap_header
17
- ldap_data = request.headers[DeviseOam.ldap_header] || ""
18
- end
14
+ ldap_data = request.headers[DeviseOam.ldap_header] if DeviseOam.ldap_header
15
+ attributes = get_attributes if DeviseOam.attr_headers
19
16
 
20
17
  if oam_data.blank?
21
- fail!(failure_message)
18
+ fail!("OAM authentication failed")
22
19
  else
23
- @authenticatable = AuthenticatableEntity.new(oam_data, ldap_data)
24
-
20
+ @authenticatable = AuthenticatableEntity.new(oam_data, ldap_data, attributes)
25
21
  user = find_or_create_user
26
22
  success!(user)
27
23
  end
@@ -34,17 +30,42 @@ module DeviseOam
34
30
  private
35
31
 
36
32
  def find_or_create_user
37
- user = DeviseOam.user_class.where({ DeviseOam.user_login_field.to_sym => @authenticatable.login }).first
38
-
33
+ user = find_user
39
34
  if user.nil? && DeviseOam.create_user_if_not_found
40
- user = DeviseOam.user_class.send(DeviseOam.create_user_method, { DeviseOam.user_login_field.to_sym => @authenticatable.login, :roles => @authenticatable.ldap_roles })
35
+ user = create_user
41
36
  elsif user && set_roles?
42
- user.send(DeviseOam.roles_setter, @authenticatable.ldap_roles)
37
+ update_user(user)
43
38
  end
44
39
 
45
40
  user
46
41
  end
42
+
43
+ def find_user
44
+ DeviseOam.user_class.where({ DeviseOam.user_login_field.to_sym => @authenticatable.login }).first
45
+ end
46
+
47
+ def create_user
48
+ DeviseOam.user_class.send(DeviseOam.create_user_method, {
49
+ DeviseOam.user_login_field.to_sym => @authenticatable.login,
50
+ roles: @authenticatable.ldap_roles
51
+ })
52
+ end
53
+
54
+ def update_user(user)
55
+ if @authenticatable.attributes.any?
56
+ user.send(DeviseOam.update_user_method, @authenticatable.ldap_roles, @authenticatable.attributes)
57
+ else
58
+ user.send(DeviseOam.update_user_method, @authenticatable.ldap_roles)
59
+ end
60
+ end
61
+
62
+ def get_attributes
63
+ hash = DeviseOam.attr_headers.inject({}) {|attr_hash, attr_header|
64
+ attr_hash[attr_header.underscore] = request.headers[attr_header] if request.headers[attr_header]
65
+ attr_hash
66
+ }
67
+ end
47
68
  end
48
69
  end
49
70
  end
50
- end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module DeviseOam
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/devise_oam.rb CHANGED
@@ -3,7 +3,6 @@ require "devise_oam/strategies/header_authenticatable"
3
3
  require "devise_oam/authenticatable_entity"
4
4
 
5
5
  module DeviseOam
6
-
7
6
  # Settings
8
7
  mattr_accessor :oam_header
9
8
  mattr_accessor :user_class
@@ -12,6 +11,10 @@ module DeviseOam
12
11
  mattr_accessor :create_user_method
13
12
  mattr_accessor :ldap_header
14
13
  mattr_accessor :roles_setter
14
+ mattr_accessor :attr_headers
15
+ mattr_writer :update_user_method
16
+
17
+ @@update_user_method = nil
15
18
 
16
19
  def self.setup
17
20
  yield self
@@ -20,4 +23,8 @@ module DeviseOam
20
23
  def self.user_class
21
24
  @@user_class.constantize
22
25
  end
26
+
27
+ def self.update_user_method
28
+ @@update_user_method || @@roles_setter
29
+ end
23
30
  end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+ include TestHelpers
3
+
4
+ class DeviseOamTest < ActiveSupport::TestCase
5
+ test "correctly parses ldap roles" do
6
+ ldap_roles = 'role-1,Role-2'
7
+ roles = ["role-1", "role-2"]
8
+
9
+ authenticatable = DeviseOam::AuthenticatableEntity.new("login", ldap_roles)
10
+
11
+ assert_equal authenticatable.ldap_roles, roles
12
+ end
13
+
14
+ test "login is case sensitive" do
15
+ auth1 = DeviseOam::AuthenticatableEntity.new("Login")
16
+ auth2 = DeviseOam::AuthenticatableEntity.new("loGin")
17
+
18
+ assert_equal auth1.login, "Login"
19
+ assert_equal auth2.login, "loGin"
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+ include TestHelpers
3
+
4
+ class DeviseOamTest < ActiveSupport::TestCase
5
+ test "truth" do
6
+ assert_kind_of Module, DeviseOam
7
+ end
8
+
9
+ test "setup block yields self" do
10
+ DeviseOam.setup do |config|
11
+ assert_equal DeviseOam, config
12
+ end
13
+ end
14
+
15
+ test "update_user_method is set to roles_setter by default" do
16
+ DeviseOam.update_user_method = nil
17
+ DeviseOam.roles_setter = :roles_setter
18
+ assert_equal DeviseOam.update_user_method, :roles_setter
19
+ end
20
+ end
@@ -32,4 +32,10 @@ class User < ActiveRecord::Base
32
32
  self.roles = roles
33
33
  self.save validate:false
34
34
  end
35
+
36
+ def update_user(roles, additional_attributes)
37
+ self.roles = roles
38
+ self.email = additional_attributes['user_email']
39
+ self.save validate:false
40
+ end
35
41
  end
Binary file
@@ -0,0 +1,153 @@
1
+ Connecting to database specified by database.yml
2
+
3
+
4
+ Started GET "/" for 127.0.0.1 at 2012-09-19 17:26:30 +0400
5
+ Processing by UsersController#index as HTML
6
+ Completed 401 Unauthorized in 9ms
7
+
8
+
9
+ Started GET "/users/sign_in" for 127.0.0.1 at 2012-09-19 17:26:30 +0400
10
+ Processing by Devise::SessionsController#new as HTML
11
+ Completed 500 Internal Server Error in 46ms
12
+
13
+ ActiveRecord::StatementInvalid (Could not find table 'users'):
14
+ activerecord (3.2.5) lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
15
+ activerecord (3.2.5) lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
16
+ activerecord (3.2.5) lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
17
+ activerecord (3.2.5) lib/active_record/model_schema.rb:228:in `yield'
18
+ activerecord (3.2.5) lib/active_record/model_schema.rb:228:in `default'
19
+ activerecord (3.2.5) lib/active_record/model_schema.rb:228:in `columns'
20
+ activerecord (3.2.5) lib/active_record/model_schema.rb:243:in `column_defaults'
21
+ activerecord (3.2.5) lib/active_record/base.rb:482:in `initialize'
22
+ devise (2.1.2) app/controllers/devise_controller.rb:102:in `new'
23
+ devise (2.1.2) app/controllers/devise_controller.rb:102:in `build_resource'
24
+ devise (2.1.2) app/controllers/devise/sessions_controller.rb:8:in `new'
25
+ actionpack (3.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
26
+ actionpack (3.2.5) lib/abstract_controller/base.rb:167:in `process_action'
27
+ actionpack (3.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
28
+ actionpack (3.2.5) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
29
+ activesupport (3.2.5) lib/active_support/callbacks.rb:458:in `_run__3322301534199686528__process_action__3654599336353534351__callbacks'
30
+ activesupport (3.2.5) lib/active_support/callbacks.rb:405:in `__run_callback'
31
+ activesupport (3.2.5) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
32
+ activesupport (3.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
33
+ actionpack (3.2.5) lib/abstract_controller/callbacks.rb:17:in `process_action'
34
+ actionpack (3.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
35
+ actionpack (3.2.5) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
36
+ activesupport (3.2.5) lib/active_support/notifications.rb:123:in `block in instrument'
37
+ activesupport (3.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
38
+ activesupport (3.2.5) lib/active_support/notifications.rb:123:in `instrument'
39
+ actionpack (3.2.5) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
40
+ actionpack (3.2.5) lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
41
+ activerecord (3.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
42
+ actionpack (3.2.5) lib/abstract_controller/base.rb:121:in `process'
43
+ actionpack (3.2.5) lib/abstract_controller/rendering.rb:45:in `process'
44
+ actionpack (3.2.5) lib/action_controller/metal.rb:203:in `dispatch'
45
+ actionpack (3.2.5) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
46
+ actionpack (3.2.5) lib/action_controller/metal.rb:246:in `block in action'
47
+ actionpack (3.2.5) lib/action_dispatch/routing/route_set.rb:73:in `call'
48
+ actionpack (3.2.5) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
49
+ actionpack (3.2.5) lib/action_dispatch/routing/route_set.rb:36:in `call'
50
+ actionpack (3.2.5) lib/action_dispatch/routing/mapper.rb:42:in `call'
51
+ journey (1.0.3) lib/journey/router.rb:68:in `block in call'
52
+ journey (1.0.3) lib/journey/router.rb:56:in `each'
53
+ journey (1.0.3) lib/journey/router.rb:56:in `call'
54
+ actionpack (3.2.5) lib/action_dispatch/routing/route_set.rb:600:in `call'
55
+ warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
56
+ warden (1.2.1) lib/warden/manager.rb:34:in `catch'
57
+ warden (1.2.1) lib/warden/manager.rb:34:in `call'
58
+ actionpack (3.2.5) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
59
+ rack (1.4.1) lib/rack/etag.rb:23:in `call'
60
+ rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
61
+ actionpack (3.2.5) lib/action_dispatch/middleware/head.rb:14:in `call'
62
+ actionpack (3.2.5) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
63
+ actionpack (3.2.5) lib/action_dispatch/middleware/flash.rb:238:in `call'
64
+ rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
65
+ rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
66
+ actionpack (3.2.5) lib/action_dispatch/middleware/cookies.rb:338:in `call'
67
+ activerecord (3.2.5) lib/active_record/query_cache.rb:64:in `call'
68
+ activerecord (3.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
69
+ actionpack (3.2.5) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
70
+ activesupport (3.2.5) lib/active_support/callbacks.rb:405:in `_run__2963724873740439576__call__167442707437893209__callbacks'
71
+ activesupport (3.2.5) lib/active_support/callbacks.rb:405:in `__run_callback'
72
+ activesupport (3.2.5) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
73
+ activesupport (3.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
74
+ actionpack (3.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
75
+ actionpack (3.2.5) lib/action_dispatch/middleware/reloader.rb:65:in `call'
76
+ actionpack (3.2.5) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
77
+ actionpack (3.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
78
+ actionpack (3.2.5) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
79
+ railties (3.2.5) lib/rails/rack/logger.rb:26:in `call_app'
80
+ railties (3.2.5) lib/rails/rack/logger.rb:16:in `call'
81
+ actionpack (3.2.5) lib/action_dispatch/middleware/request_id.rb:22:in `call'
82
+ rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
83
+ rack (1.4.1) lib/rack/runtime.rb:17:in `call'
84
+ activesupport (3.2.5) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
85
+ rack (1.4.1) lib/rack/lock.rb:15:in `call'
86
+ actionpack (3.2.5) lib/action_dispatch/middleware/static.rb:62:in `call'
87
+ railties (3.2.5) lib/rails/engine.rb:479:in `call'
88
+ railties (3.2.5) lib/rails/application.rb:220:in `call'
89
+ rack (1.4.1) lib/rack/content_length.rb:14:in `call'
90
+ railties (3.2.5) lib/rails/rack/log_tailer.rb:17:in `call'
91
+ rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
92
+ /Users/whatthewhat/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
93
+ /Users/whatthewhat/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
94
+ /Users/whatthewhat/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
95
+
96
+
97
+ Rendered /Users/whatthewhat/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.8ms)
98
+ Rendered /Users/whatthewhat/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
99
+ Rendered /Users/whatthewhat/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.3ms)
100
+ Connecting to database specified by database.yml
101
+  (0.1ms) select sqlite_version(*)
102
+  (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
103
+  (0.0ms) PRAGMA index_list("schema_migrations")
104
+  (1.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
105
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
106
+ Migrating to DeviseCreateUsers (20120514070218)
107
+  (0.0ms) begin transaction
108
+  (0.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255) DEFAULT '' NOT NULL, "encrypted_password" varchar(255) DEFAULT '' NOT NULL, "reset_password_token" varchar(255), "reset_password_sent_at" datetime, "remember_created_at" datetime, "sign_in_count" integer DEFAULT 0, "current_sign_in_at" datetime, "last_sign_in_at" datetime, "current_sign_in_ip" varchar(255), "last_sign_in_ip" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
109
+  (0.1ms) PRAGMA index_list("users")
110
+  (0.4ms) CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")
111
+  (0.1ms) PRAGMA index_list("users")
112
+  (0.0ms) PRAGMA index_info('index_users_on_email')
113
+  (0.2ms) CREATE UNIQUE INDEX "index_users_on_reset_password_token" ON "users" ("reset_password_token")
114
+  (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ('20120514070218')
115
+  (1.1ms) commit transaction
116
+ Migrating to AddRolesMaskToUsers (20120521064519)
117
+  (0.0ms) begin transaction
118
+  (0.9ms) ALTER TABLE "users" ADD "roles_mask" integer
119
+  (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ('20120521064519')
120
+  (0.8ms) commit transaction
121
+  (0.4ms) select sqlite_version(*)
122
+  (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
123
+  (0.1ms) PRAGMA index_list("users")
124
+  (0.0ms) PRAGMA index_info('index_users_on_reset_password_token')
125
+  (0.0ms) PRAGMA index_info('index_users_on_email')
126
+ Connecting to database specified by database.yml
127
+
128
+
129
+ Started GET "/users/sign_in" for 127.0.0.1 at 2012-09-19 17:27:00 +0400
130
+ Processing by Devise::SessionsController#new as HTML
131
+ Rendered /Users/whatthewhat/.rvm/gems/ruby-1.9.3-p125/gems/devise-2.1.2/app/views/devise/shared/_links.erb (1.9ms)
132
+ Rendered /Users/whatthewhat/.rvm/gems/ruby-1.9.3-p125/gems/devise-2.1.2/app/views/devise/sessions/new.html.erb within layouts/application (16.6ms)
133
+ Compiled application.css (0ms) (pid 5431)
134
+ Compiled jquery.js (3ms) (pid 5431)
135
+ Compiled jquery_ujs.js (0ms) (pid 5431)
136
+ Compiled application.js (51ms) (pid 5431)
137
+ Completed 200 OK in 247ms (Views: 187.3ms | ActiveRecord: 2.3ms)
138
+
139
+
140
+ Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-09-19 17:27:00 +0400
141
+ Served asset /application.css - 200 OK (4ms)
142
+
143
+
144
+ Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-09-19 17:27:00 +0400
145
+ Served asset /application.js - 200 OK (9ms)
146
+
147
+
148
+ Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-09-19 17:27:00 +0400
149
+ Served asset /jquery.js - 200 OK (5ms)
150
+
151
+
152
+ Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2012-09-19 17:27:00 +0400
153
+ Served asset /jquery_ujs.js - 200 OK (4ms)