Class: TokenAuthenticatableStrategies::Base

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

Direct Known Subclasses

Digest, Encrypted, Insecure

Constant Summary collapse

TRUE_PROC =
->(_) { true }

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.



9
10
11
12
13
14
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 9

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

#expires_at_fieldObject (readonly)

Returns the value of attribute expires_at_field.



7
8
9
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 7

def expires_at_field
  @expires_at_field
end

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 7

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 7

def options
  @options
end

#token_fieldObject (readonly)

Returns the value of attribute token_field.



7
8
9
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 7

def token_field
  @token_field
end

Class Method Details

.fabricate(model, field, options) ⇒ Object



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

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(token_owner_record) ⇒ Object



41
42
43
44
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 41

def ensure_token(token_owner_record)
  write_new_token(token_owner_record) unless token_set?(token_owner_record)
  get_token(token_owner_record)
end

#ensure_token!(token_owner_record) ⇒ Object

Returns a token, but only saves when the database is in read & write mode



47
48
49
50
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 47

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

#expirable?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 69

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

#expired?(token_owner_record) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  exp = expires_at(token_owner_record)
  !!exp && exp.past?
end

#expires_at(token_owner_record) ⇒ Object



58
59
60
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 58

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

#find_token_authenticatable(token_owner_record, unscoped = false) ⇒ Object

Raises:

  • (NotImplementedError)


16
17
18
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 16

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

#get_token(token_owner_record) ⇒ Object

Raises:

  • (NotImplementedError)


20
21
22
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 20

def get_token(token_owner_record)
  raise NotImplementedError
end

#reset_token!(token_owner_record) ⇒ Object

Resets the token, but only saves when the database is in read & write mode



53
54
55
56
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 53

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

#sensitive_fieldsObject

The expires_at field is not considered sensitive



37
38
39
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 37

def sensitive_fields
  token_fields - [@expires_at_field]
end

#set_token(token_owner_record, token) ⇒ Object

Raises:

  • (NotImplementedError)


24
25
26
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 24

def set_token(token_owner_record, token)
  raise NotImplementedError
end

#token_fieldsObject



28
29
30
31
32
33
34
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 28

def token_fields
  result = [token_field]

  result << @expires_at_field if expirable?

  result
end

#token_with_expiration(token_owner_record) ⇒ Object



73
74
75
# File 'app/models/concerns/token_authenticatable_strategies/base.rb', line 73

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