Class: TokenAuth::ConfigurationToken

Inherits:
ApplicationRecord show all
Defined in:
app/models/token_auth/configuration_token.rb

Overview

A single use human readable token for use with client configuration.

Constant Summary collapse

SAMPLE_SET =
%w[ A B C D E F H J K L M N P Q R S T U V W X Y Z
2 3 4 5 7 8 9
# $ ].freeze
TOKEN_LENGTH =
6

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_match(value) ⇒ Object

Returns case insensitive match. rubocop:disable Rails/FindBy



20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/token_auth/configuration_token.rb', line 20

def self.find_match(value)
  return nil unless value.is_a?(String)

  query = value.gsub(/[\s]+/, "")

  return nil unless query.length == TOKEN_LENGTH

  where(arel_table[:expires_at].gt(Time.zone.now))
    .where(arel_table[:value].matches(query))
    .first
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/token_auth/configuration_token.rb', line 48

def expired?
  Time.zone.now > expires_at
end

#make_authentication_token(client_uuid:) ⇒ Object

rubocop:enable Rails/FindBy



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/token_auth/configuration_token.rb', line 33

def make_authentication_token(client_uuid:)
  return nil if expired?

  authentication_token = AuthenticationToken
                         .new(entity_id: entity_id,
                              client_uuid: client_uuid)
  transaction do
    authentication_token.save! && destroy!
  end

  authentication_token
rescue StandardError
  nil
end