2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-07-09 08:42:25 -04:00
|
|
|
module TokenAuthenticatable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2015-12-10 04:48:37 -05:00
|
|
|
class_methods do
|
2020-06-11 08:08:54 -04:00
|
|
|
def encrypted_token_authenticatable_fields
|
|
|
|
@encrypted_token_authenticatable_fields ||= []
|
|
|
|
end
|
|
|
|
|
2019-07-25 11:59:31 -04:00
|
|
|
private
|
2015-12-10 04:48:37 -05:00
|
|
|
|
2018-10-29 12:06:45 -04:00
|
|
|
def add_authentication_token_field(token_field, options = {})
|
2018-11-15 10:12:07 -05:00
|
|
|
if token_authenticatable_fields.include?(token_field)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ArgumentError, "#{token_field} already configured via add_authentication_token_field"
|
2018-10-29 12:06:45 -04:00
|
|
|
end
|
|
|
|
|
2018-11-15 10:12:07 -05:00
|
|
|
token_authenticatable_fields.push(token_field)
|
2020-06-11 08:08:54 -04:00
|
|
|
encrypted_token_authenticatable_fields.push(token_field) if options[:encrypted]
|
2014-07-09 08:42:25 -04:00
|
|
|
|
2018-10-29 12:06:45 -04:00
|
|
|
attr_accessor :cleartext_tokens
|
|
|
|
|
2018-11-15 10:08:28 -05:00
|
|
|
strategy = TokenAuthenticatableStrategies::Base
|
|
|
|
.fabricate(self, token_field, options)
|
2018-10-29 12:06:45 -04:00
|
|
|
|
2018-11-15 10:12:07 -05:00
|
|
|
if options.fetch(:unique, true)
|
2018-10-31 13:28:42 -04:00
|
|
|
define_singleton_method("find_by_#{token_field}") do |token|
|
|
|
|
strategy.find_token_authenticatable(token)
|
|
|
|
end
|
2015-12-10 04:48:37 -05:00
|
|
|
end
|
|
|
|
|
2019-03-08 10:28:59 -05:00
|
|
|
mod = token_authenticatable_module
|
|
|
|
|
|
|
|
mod.define_method(token_field) do
|
2018-10-29 12:06:45 -04:00
|
|
|
strategy.get_token(self)
|
2015-12-23 04:47:18 -05:00
|
|
|
end
|
|
|
|
|
2019-03-08 10:28:59 -05:00
|
|
|
mod.define_method("set_#{token_field}") do |token|
|
2018-10-29 12:06:45 -04:00
|
|
|
strategy.set_token(self, token)
|
|
|
|
end
|
|
|
|
|
2019-03-08 10:28:59 -05:00
|
|
|
mod.define_method("ensure_#{token_field}") do
|
2018-10-29 12:06:45 -04:00
|
|
|
strategy.ensure_token(self)
|
2016-10-02 18:30:44 -04:00
|
|
|
end
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
# Returns a token, but only saves when the database is in read & write mode
|
2019-03-08 10:28:59 -05:00
|
|
|
mod.define_method("ensure_#{token_field}!") do
|
2018-10-29 12:06:45 -04:00
|
|
|
strategy.ensure_token!(self)
|
2015-12-10 04:48:37 -05:00
|
|
|
end
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
# Resets the token, but only saves when the database is in read & write mode
|
2019-03-08 10:28:59 -05:00
|
|
|
mod.define_method("reset_#{token_field}!") do
|
2018-10-29 12:06:45 -04:00
|
|
|
strategy.reset_token!(self)
|
2015-12-10 04:48:37 -05:00
|
|
|
end
|
2018-10-17 07:19:00 -04:00
|
|
|
|
2019-03-08 10:28:59 -05:00
|
|
|
mod.define_method("#{token_field}_matches?") do |other_token|
|
2018-10-17 07:19:00 -04:00
|
|
|
token = read_attribute(token_field)
|
2019-06-25 13:54:42 -04:00
|
|
|
token.present? && ActiveSupport::SecurityUtils.secure_compare(other_token, token)
|
2018-10-17 07:19:00 -04:00
|
|
|
end
|
2020-12-15 10:09:59 -05:00
|
|
|
|
|
|
|
# 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-02-07 07:12:27 -05:00
|
|
|
|
|
|
|
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
|
2015-12-10 04:48:37 -05:00
|
|
|
end
|
2018-11-15 10:12:07 -05:00
|
|
|
|
2019-03-08 10:28:59 -05:00
|
|
|
def token_authenticatable_module
|
|
|
|
@token_authenticatable_module ||=
|
|
|
|
const_set(:TokenAuthenticatable, Module.new).tap(&method(:include))
|
|
|
|
end
|
|
|
|
|
2018-11-15 10:12:07 -05:00
|
|
|
def token_authenticatable_fields
|
|
|
|
@token_authenticatable_fields ||= []
|
|
|
|
end
|
2014-07-09 08:42:25 -04:00
|
|
|
end
|
|
|
|
end
|