diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-10-31 17:28:42 +0000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-10-31 17:28:42 +0000 |
commit | 69b9a879a12e3eeaae1394534247c97e5ad14495 (patch) | |
tree | cd1e791cfadff850f012858083cb20157357d3be | |
parent | f68037ea71bb515ea0a19f3061e712d1b1427f37 (diff) | |
download | gitlab-ce-69b9a879a12e3eeaae1394534247c97e5ad14495.tar.gz |
TokenAuthenticatable allows non-unique tokensce-jej/group-saml-discovery-token
Avoids needing an index to repeatedly check that
the token doesn't already exist when saving.
-rw-r--r-- | app/models/concerns/token_authenticatable.rb | 7 | ||||
-rw-r--r-- | app/models/concerns/token_authenticatable_strategies/base.rb | 6 |
2 files changed, 10 insertions, 3 deletions
diff --git a/app/models/concerns/token_authenticatable.rb b/app/models/concerns/token_authenticatable.rb index 66db4bd92de..23a43aec677 100644 --- a/app/models/concerns/token_authenticatable.rb +++ b/app/models/concerns/token_authenticatable.rb @@ -10,6 +10,7 @@ module TokenAuthenticatable def add_authentication_token_field(token_field, options = {}) @token_fields = [] unless @token_fields + unique = options.fetch(:unique, true) if @token_fields.include?(token_field) raise ArgumentError.new("#{token_field} already configured via add_authentication_token_field") @@ -25,8 +26,10 @@ module TokenAuthenticatable TokenAuthenticatableStrategies::Insecure.new(self, token_field, options) end - define_singleton_method("find_by_#{token_field}") do |token| - strategy.find_token_authenticatable(token) + if unique + define_singleton_method("find_by_#{token_field}") do |token| + strategy.find_token_authenticatable(token) + end end define_method(token_field) do diff --git a/app/models/concerns/token_authenticatable_strategies/base.rb b/app/models/concerns/token_authenticatable_strategies/base.rb index f0f7107d627..413721d3e6c 100644 --- a/app/models/concerns/token_authenticatable_strategies/base.rb +++ b/app/models/concerns/token_authenticatable_strategies/base.rb @@ -43,10 +43,14 @@ module TokenAuthenticatableStrategies set_token(instance, new_token) end + def unique + @options.fetch(:unique, true) + end + def generate_available_token loop do token = generate_token - break token unless find_token_authenticatable(token, true) + break token unless unique && find_token_authenticatable(token, true) end end |