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
|
|
|
|
|
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
|
|
|
|
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)
|
2016-01-14 18:37:52 +05:30
|
|
|
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
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def token_authenticatable_module
|
|
|
|
@token_authenticatable_module ||=
|
|
|
|
const_set(:TokenAuthenticatable, Module.new).tap(&method(:include))
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def token_authenticatable_fields
|
|
|
|
@token_authenticatable_fields ||= []
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|