Class: TokenAuthenticatableStrategies::Base
- Inherits:
-
Object
- Object
- TokenAuthenticatableStrategies::Base
show all
- Defined in:
- app/models/concerns/token_authenticatable_strategies/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, token_field, options) ⇒ Base
Returns a new instance of Base.
7
8
9
10
11
12
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 7
def initialize(klass, token_field, options)
@klass = klass
@token_field = token_field
@expires_at_field = "#{token_field}_expires_at"
@options = options
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
5
6
7
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 5
def klass
@klass
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 5
def options
@options
end
|
#token_field ⇒ Object
Returns the value of attribute token_field.
5
6
7
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 5
def token_field
@token_field
end
|
Class Method Details
.fabricate(model, field, options) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 79
def self.fabricate(model, field, options)
if options[:digest] && options[:encrypted]
raise ArgumentError, _('Incompatible options set!')
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
|
Instance Method Details
#ensure_token(instance) ⇒ Object
43
44
45
46
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 43
def ensure_token(instance)
write_new_token(instance) unless token_set?(instance)
get_token(instance)
end
|
#ensure_token!(instance) ⇒ Object
Returns a token, but only saves when the database is in read & write mode
49
50
51
52
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 49
def ensure_token!(instance)
reset_token!(instance) unless token_set?(instance)
get_token(instance)
end
|
#expirable? ⇒ Boolean
71
72
73
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 71
def expirable?
!!@options[:expires_at]
end
|
#expired?(instance) ⇒ Boolean
64
65
66
67
68
69
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 64
def expired?(instance)
return false unless expirable? && token_expiration_enforced?
exp = expires_at(instance)
!!exp && Time.current > exp
end
|
#expires_at(instance) ⇒ Object
60
61
62
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 60
def expires_at(instance)
instance.read_attribute(@expires_at_field)
end
|
#find_token_authenticatable(instance, unscoped = false) ⇒ Object
14
15
16
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 14
def find_token_authenticatable(instance, unscoped = false)
raise NotImplementedError
end
|
If a ‘format_with_prefix` option is provided, it applies and returns the formatted token. Otherwise, default implementation returns the token as-is
36
37
38
39
40
41
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 36
def format_token(instance, token)
prefix = prefix_for(instance)
prefixed_token = prefix ? "#{prefix}#{token}" : token
instance.send("format_#{@token_field}", prefixed_token) end
|
#get_token(instance) ⇒ Object
18
19
20
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 18
def get_token(instance)
raise NotImplementedError
end
|
#reset_token!(instance) ⇒ Object
Resets the token, but only saves when the database is in read & write mode
55
56
57
58
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 55
def reset_token!(instance)
write_new_token(instance)
instance.save! if Gitlab::Database.read_write?
end
|
#set_token(instance, token) ⇒ Object
22
23
24
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 22
def set_token(instance, token)
raise NotImplementedError
end
|
#token_fields ⇒ Object
26
27
28
29
30
31
32
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 26
def token_fields
result = [token_field]
result << @expires_at_field if expirable?
result
end
|
#token_with_expiration(instance) ⇒ Object
75
76
77
|
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 75
def token_with_expiration(instance)
API::Support::TokenWithExpiration.new(self, instance)
end
|