2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TokenAuthenticatableStrategies
|
|
|
|
class Base
|
2019-02-15 15:39:39 +05:30
|
|
|
attr_reader :klass, :token_field, :options
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def initialize(klass, token_field, options)
|
|
|
|
@klass = klass
|
|
|
|
@token_field = token_field
|
2022-04-04 11:22:00 +05:30
|
|
|
@expires_at_field = "#{token_field}_expires_at"
|
2018-11-18 11:00:15 +05:30
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_token_authenticatable(instance, unscoped = false)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(instance)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def set_token(instance, token)
|
2018-11-18 11:00:15 +05:30
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def token_fields
|
|
|
|
result = [token_field]
|
|
|
|
|
|
|
|
result << @expires_at_field if expirable?
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
# Default implementation returns the token as-is
|
|
|
|
def format_token(instance, token)
|
|
|
|
instance.send("format_#{@token_field}", token) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def ensure_token(instance)
|
|
|
|
write_new_token(instance) unless token_set?(instance)
|
2019-02-15 15:39:39 +05:30
|
|
|
get_token(instance)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a token, but only saves when the database is in read & write mode
|
|
|
|
def ensure_token!(instance)
|
|
|
|
reset_token!(instance) unless token_set?(instance)
|
|
|
|
get_token(instance)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Resets the token, but only saves when the database is in read & write mode
|
|
|
|
def reset_token!(instance)
|
|
|
|
write_new_token(instance)
|
|
|
|
instance.save! if Gitlab::Database.read_write?
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def expires_at(instance)
|
|
|
|
instance.read_attribute(@expires_at_field)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expired?(instance)
|
|
|
|
return false unless expirable? && token_expiration_enforced?
|
|
|
|
|
|
|
|
exp = expires_at(instance)
|
|
|
|
!!exp && Time.current > exp
|
|
|
|
end
|
|
|
|
|
|
|
|
def expirable?
|
|
|
|
!!@options[:expires_at]
|
|
|
|
end
|
|
|
|
|
|
|
|
def token_with_expiration(instance)
|
|
|
|
API::Support::TokenWithExpiration.new(self, instance)
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def self.fabricate(model, field, options)
|
|
|
|
if options[:digest] && options[:encrypted]
|
2019-07-31 22:56:46 +05:30
|
|
|
raise ArgumentError, _('Incompatible options set!')
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
if options[:digest]
|
|
|
|
TokenAuthenticatableStrategies::Digest.new(model, field, options)
|
|
|
|
elsif options[:encrypted]
|
|
|
|
TokenAuthenticatableStrategies::Encrypted.new(model, field, options)
|
|
|
|
else
|
|
|
|
TokenAuthenticatableStrategies::Insecure.new(model, field, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
protected
|
|
|
|
|
|
|
|
def write_new_token(instance)
|
|
|
|
new_token = generate_available_token
|
2021-02-22 17:27:13 +05:30
|
|
|
formatted_token = format_token(instance, new_token)
|
|
|
|
set_token(instance, formatted_token)
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
if expirable?
|
|
|
|
instance[@expires_at_field] = @options[:expires_at].to_proc.call(instance)
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def unique
|
|
|
|
@options.fetch(:unique, true)
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def generate_available_token
|
|
|
|
loop do
|
|
|
|
token = generate_token
|
2018-12-13 13:39:08 +05:30
|
|
|
break token unless unique && find_token_authenticatable(token, true)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_token
|
|
|
|
@options[:token_generator] ? @options[:token_generator].call : Devise.friendly_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def relation(unscoped)
|
2022-04-04 11:22:00 +05:30
|
|
|
unscoped ? @klass.unscoped : @klass.where(not_expired)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def token_set?(instance)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def token_expiration_enforced?
|
|
|
|
return true unless @options[:expiration_enforced?]
|
|
|
|
|
|
|
|
@options[:expiration_enforced?].to_proc.call(@klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_expired
|
|
|
|
Arel.sql("#{@expires_at_field} IS NULL OR #{@expires_at_field} >= NOW()") if expirable? && token_expiration_enforced?
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|