debian-mirror-gitlab/app/models/concerns/token_authenticatable.rb

93 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module TokenAuthenticatable
extend ActiveSupport::Concern
2015-12-23 02:04:40 +05:30
class_methods do
2020-06-23 00:09:42 +05:30
def encrypted_token_authenticatable_fields
@encrypted_token_authenticatable_fields ||= []
end
2022-05-07 20:08:51 +05:30
def token_authenticatable_fields
@token_authenticatable_fields ||= []
end
2019-10-12 21:52:04 +05:30
private
2015-12-23 02:04:40 +05:30
2018-11-18 11:00:15 +05:30
def add_authentication_token_field(token_field, options = {})
2019-02-15 15:39:39 +05:30
if token_authenticatable_fields.include?(token_field)
2021-06-08 01:23:25 +05:30
raise ArgumentError, "#{token_field} already configured via add_authentication_token_field"
2018-11-18 11:00:15 +05:30
end
2019-02-15 15:39:39 +05:30
token_authenticatable_fields.push(token_field)
2020-06-23 00:09:42 +05:30
encrypted_token_authenticatable_fields.push(token_field) if options[:encrypted]
2014-09-02 18:07:02 +05:30
2018-11-18 11:00:15 +05:30
attr_accessor :cleartext_tokens
2019-02-15 15:39:39 +05:30
strategy = TokenAuthenticatableStrategies::Base
.fabricate(self, token_field, options)
2018-11-18 11:00:15 +05:30
2022-05-07 20:08:51 +05:30
prevent_from_serialization(*strategy.token_fields) if respond_to?(:prevent_from_serialization)
2019-02-15 15:39:39 +05:30
if options.fetch(:unique, true)
2018-12-13 13:39:08 +05:30
define_singleton_method("find_by_#{token_field}") do |token|
strategy.find_token_authenticatable(token)
end
2015-12-23 02:04:40 +05:30
end
2019-07-07 11:18:12 +05:30
mod = token_authenticatable_module
mod.define_method(token_field) do
2018-11-18 11:00:15 +05:30
strategy.get_token(self)
end
2019-07-07 11:18:12 +05:30
mod.define_method("set_#{token_field}") do |token|
2018-11-18 11:00:15 +05:30
strategy.set_token(self, token)
end
2019-07-07 11:18:12 +05:30
mod.define_method("ensure_#{token_field}") do
2018-11-18 11:00:15 +05:30
strategy.ensure_token(self)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# Returns a token, but only saves when the database is in read & write mode
2019-07-07 11:18:12 +05:30
mod.define_method("ensure_#{token_field}!") do
2018-11-18 11:00:15 +05:30
strategy.ensure_token!(self)
2015-12-23 02:04:40 +05:30
end
2018-03-17 18:26:18 +05:30
# Resets the token, but only saves when the database is in read & write mode
2019-07-07 11:18:12 +05:30
mod.define_method("reset_#{token_field}!") do
2018-11-18 11:00:15 +05:30
strategy.reset_token!(self)
2015-12-23 02:04:40 +05:30
end
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
mod.define_method("#{token_field}_matches?") do |other_token|
2019-02-15 15:39:39 +05:30
token = read_attribute(token_field)
2019-09-30 21:07:59 +05:30
token.present? && ActiveSupport::SecurityUtils.secure_compare(other_token, token)
2019-02-15 15:39:39 +05:30
end
2021-02-22 17:27:13 +05:30
# Base strategy delegates to this method for formatting a token before
# calling set_token. Can be overridden in models to e.g. add a prefix
# to the tokens
mod.define_method("format_#{token_field}") do |token|
token
end
2022-04-04 11:22:00 +05:30
mod.define_method("#{token_field}_expires_at") do
strategy.expires_at(self)
end
mod.define_method("#{token_field}_expired?") do
strategy.expired?(self)
end
mod.define_method("#{token_field}_with_expiration") do
strategy.token_with_expiration(self)
end
2019-02-15 15:39:39 +05:30
end
2019-07-07 11:18:12 +05:30
def token_authenticatable_module
@token_authenticatable_module ||=
2023-04-23 21:23:45 +05:30
const_set(:TokenAuthenticatable, Module.new).tap { |mod| include mod }
2019-07-07 11:18:12 +05:30
end
2014-09-02 18:07:02 +05:30
end
end