Skip to content

Commit dc544bb

Browse files
authored
Use strings for class names (#185)
Rails 6 introduces the zeitwerk autoloader, and is warning us that using constants during initialization is not supported. We can avoid that by setting class *names* instead of the constants directly, and loading the constants at runtime.
1 parent 5c6a067 commit dc544bb

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ In `config/initializers/devise.rb`:
8989
# If you don't set it then email will be extracted from SAML assertation attributes.
9090
config.saml_use_subject = true
9191

92-
# You can support multiple IdPs by setting this value to a class that implements a #settings method which takes
93-
# an IdP entity id as an argument and returns a hash of idp settings for the corresponding IdP.
94-
config.idp_settings_adapter = nil
92+
# You can support multiple IdPs by setting this value to the name of a class that implements a ::settings method
93+
# which takes an IdP entity id as an argument and returns a hash of idp settings for the corresponding IdP.
94+
# config.idp_settings_adapter = "MyIdPSettingsAdapter"
9595

9696
# You provide you own method to find the idp_entity_id in a SAML message in the case of multiple IdPs
97-
# by setting this to a custom reader class, or use the default.
98-
# config.idp_entity_id_reader = DeviseSamlAuthenticatable::DefaultIdpEntityIdReader
97+
# by setting this to the name of a custom reader class, or use the default.
98+
# config.idp_entity_id_reader = "DeviseSamlAuthenticatable::DefaultIdpEntityIdReader"
9999

100100
# You can set a handler object that takes the response for a failed SAML request and the strategy,
101101
# and implements a #handle method. This method can then redirect the user, return error messages, etc.
@@ -169,7 +169,7 @@ If you only have one IdP, you can use the config file above, or just return a si
169169
...
170170
# ==> Configuration for :saml_authenticatable
171171
172-
config.saml_attribute_map_resolver = MyAttributeMapResolver
172+
config.saml_attribute_map_resolver = "MyAttributeMapResolver"
173173
end
174174
```
175175

lib/devise_saml_authenticatable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Devise
5656

5757
# Reader that can parse entity id from a SAMLMessage
5858
mattr_accessor :idp_entity_id_reader
59-
@@idp_entity_id_reader ||= ::DeviseSamlAuthenticatable::DefaultIdpEntityIdReader
59+
@@idp_entity_id_reader ||= "::DeviseSamlAuthenticatable::DefaultIdpEntityIdReader"
6060

6161
# Implements a #handle method that takes the response and strategy as an argument
6262
mattr_accessor :saml_failed_callback
@@ -69,7 +69,7 @@ module Devise
6969

7070
# Instead of storing the attribute_map in attribute-map.yml, store it in the database, or set it programatically
7171
mattr_accessor :saml_attribute_map_resolver
72-
@@saml_attribute_map_resolver ||= ::DeviseSamlAuthenticatable::DefaultAttributeMapResolver
72+
@@saml_attribute_map_resolver ||= "::DeviseSamlAuthenticatable::DefaultAttributeMapResolver"
7373

7474
# Implements a #validate method that takes the retrieved resource and response right after retrieval,
7575
# and returns true if it's valid. False will cause authentication to fail.

lib/devise_saml_authenticatable/model.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ def find_for_shibb_authentication(conditions)
8282
end
8383

8484
def attribute_map(saml_response = nil)
85-
Devise.saml_attribute_map_resolver.new(saml_response).attribute_map
85+
attribute_map_resolver.new(saml_response).attribute_map
86+
end
87+
88+
def attribute_map_resolver
89+
if Devise.saml_attribute_map_resolver.respond_to?(:new)
90+
Devise.saml_attribute_map_resolver
91+
else
92+
Devise.saml_attribute_map_resolver.constantize
93+
end
8694
end
8795
end
8896
end

lib/devise_saml_authenticatable/saml_config.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def file_based_config
2222
def adapter_based_config(idp_entity_id)
2323
config = Marshal.load(Marshal.dump(Devise.saml_config))
2424

25-
Devise.idp_settings_adapter.settings(idp_entity_id).each do |k,v|
25+
idp_settings_adapter.settings(idp_entity_id).each do |k,v|
2626
acc = "#{k.to_s}=".to_sym
2727

2828
if config.respond_to? acc
@@ -33,7 +33,23 @@ def adapter_based_config(idp_entity_id)
3333
end
3434

3535
def get_idp_entity_id(params)
36-
Devise.idp_entity_id_reader.entity_id(params)
36+
idp_entity_id_reader.entity_id(params)
37+
end
38+
39+
def idp_entity_id_reader
40+
if Devise.idp_entity_id_reader.respond_to?(:entity_id)
41+
Devise.idp_entity_id_reader
42+
else
43+
@idp_entity_id_reader ||= Devise.idp_entity_id_reader.constantize
44+
end
45+
end
46+
47+
def idp_settings_adapter
48+
if Devise.idp_settings_adapter.respond_to?(:settings)
49+
Devise.idp_settings_adapter
50+
else
51+
@idp_settings_adapter ||= Devise.idp_settings_adapter.constantize
52+
end
3753
end
3854
end
3955
end

spec/features/saml_authentication_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
context "when the idp_settings_adapter key is set" do
142142
before(:each) do
143143
create_app('idp', 'INCLUDE_SUBJECT_IN_ATTRIBUTES' => "false")
144-
create_app('sp', 'USE_SUBJECT_TO_AUTHENTICATE' => "true", 'IDP_SETTINGS_ADAPTER' => "IdpSettingsAdapter", 'IDP_ENTITY_ID_READER' => "OurEntityIdReader")
144+
create_app('sp', 'USE_SUBJECT_TO_AUTHENTICATE' => "true", 'IDP_SETTINGS_ADAPTER' => '"IdpSettingsAdapter"', 'IDP_ENTITY_ID_READER' => '"OurEntityIdReader"')
145145

146146
# use a different port for this entity ID; configured in spec/support/idp_settings_adapter.rb.erb
147147
@idp_pid = start_app('idp', 8010)
@@ -204,7 +204,7 @@
204204
)
205205
create_app(
206206
"sp",
207-
"ATTRIBUTE_MAP_RESOLVER" => "AttributeMapResolver",
207+
"ATTRIBUTE_MAP_RESOLVER" => '"AttributeMapResolver"',
208208
"USE_SUBJECT_TO_AUTHENTICATE" => "true",
209209
)
210210
@idp_pid = start_app("idp", idp_port)

spec/support/sp_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
saml_session_index_key = ENV.fetch('SAML_SESSION_INDEX_KEY', ":session_index")
77
use_subject_to_authenticate = ENV.fetch('USE_SUBJECT_TO_AUTHENTICATE')
88
idp_settings_adapter = ENV.fetch('IDP_SETTINGS_ADAPTER', "nil")
9-
idp_entity_id_reader = ENV.fetch('IDP_ENTITY_ID_READER', "DeviseSamlAuthenticatable::DefaultIdpEntityIdReader")
9+
idp_entity_id_reader = ENV.fetch('IDP_ENTITY_ID_READER', '"DeviseSamlAuthenticatable::DefaultIdpEntityIdReader"')
1010
saml_failed_callback = ENV.fetch('SAML_FAILED_CALLBACK', "nil")
1111

1212
if Rails::VERSION::MAJOR < 5 || (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR < 2)

0 commit comments

Comments
 (0)