Class: TokenAuthenticatableStrategies::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/token_authenticatable_strategies/base.rb

Direct Known Subclasses

Digest, Encrypted, Insecure

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

#klassObject (readonly)

Returns the value of attribute klass.


5
6
7
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 5

def klass
  @klass
end

#optionsObject (readonly)

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_fieldObject (readonly)

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


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 75

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


39
40
41
42
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 39

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


45
46
47
48
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 45

def ensure_token!(instance)
  reset_token!(instance) unless token_set?(instance)
  get_token(instance)
end

#expirable?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 67

def expirable?
  !!@options[:expires_at]
end

#expired?(instance) ⇒ Boolean

Returns:

  • (Boolean)

60
61
62
63
64
65
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 60

def expired?(instance)
  return false unless expirable? && token_expiration_enforced?

  exp = expires_at(instance)
  !!exp && Time.current > exp
end

#expires_at(instance) ⇒ Object


56
57
58
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 56

def expires_at(instance)
  instance.read_attribute(@expires_at_field)
end

#find_token_authenticatable(instance, unscoped = false) ⇒ Object

Raises:

  • (NotImplementedError)

14
15
16
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 14

def find_token_authenticatable(instance, unscoped = false)
  raise NotImplementedError
end

#format_token(instance, token) ⇒ Object

Default implementation returns the token as-is


35
36
37
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 35

def format_token(instance, token)
  instance.send("format_#{@token_field}", token) # rubocop:disable GitlabSecurity/PublicSend
end

#get_token(instance) ⇒ Object

Raises:

  • (NotImplementedError)

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


51
52
53
54
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 51

def reset_token!(instance)
  write_new_token(instance)
  instance.save! if Gitlab::Database.read_write?
end

#set_token(instance, token) ⇒ Object

Raises:

  • (NotImplementedError)

22
23
24
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 22

def set_token(instance, token)
  raise NotImplementedError
end

#token_fieldsObject


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


71
72
73
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 71

def token_with_expiration(instance)
  API::Support::TokenWithExpiration.new(self, instance)
end