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