Class: Authn::TokenField::Generator::RoutableToken

Inherits:
Object
  • Object
show all
Defined in:
lib/authn/token_field/generator/routable_token.rb

Constant Summary collapse

TOKEN_VERSION =
1
TOKEN_VERSION_LENGTH =
2
RANDOM_BYTES_LENGTH =
16
BASE64_PAYLOAD_LENGTH_HOLDER_BYTES =
2
CRC_BYTES =
7
VALID_ROUTING_KEYS =
%i[c g o p u t].freeze
REQUIRED_ROUTING_KEYS =
%i[o].freeze
MAXIMUM_SIZE_OF_ROUTING_PAYLOAD =
159
DEFAULT_ROUTING_PAYLOAD_HASH =
{
  c: ->(_) { Gitlab.config.cell.id }
}.freeze
PayloadTooLarge =
Class.new(RuntimeError)
MissingRequiredRoutingKeys =
Class.new(ArgumentError)
InvalidRoutingKeys =
Class.new(ArgumentError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_owner_record, routing_payload:, prefix: '') ⇒ RoutableToken

Returns a new instance of RoutableToken.



34
35
36
37
38
39
40
# File 'lib/authn/token_field/generator/routable_token.rb', line 34

def initialize(token_owner_record, routing_payload:, prefix: '')
  @token_owner_record = token_owner_record
  @routing_payload = routing_payload
  @prefix = prefix

  validate_routing_keys!
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



32
33
34
# File 'lib/authn/token_field/generator/routable_token.rb', line 32

def prefix
  @prefix
end

#routing_payloadObject (readonly)

Returns the value of attribute routing_payload.



32
33
34
# File 'lib/authn/token_field/generator/routable_token.rb', line 32

def routing_payload
  @routing_payload
end

#token_owner_recordObject (readonly)

Returns the value of attribute token_owner_record.



32
33
34
# File 'lib/authn/token_field/generator/routable_token.rb', line 32

def token_owner_record
  @token_owner_record
end

Class Method Details

.crc_of(encoded) ⇒ Object



28
29
30
# File 'lib/authn/token_field/generator/routable_token.rb', line 28

def self.crc_of(encoded)
  Zlib.crc32(encoded).to_s(36).rjust(CRC_BYTES, '0')
end

.random_bytes(length) ⇒ Object



24
25
26
# File 'lib/authn/token_field/generator/routable_token.rb', line 24

def self.random_bytes(length)
  SecureRandom.random_bytes(length)
end

Instance Method Details

#generate_tokenObject



42
43
44
45
46
47
48
# File 'lib/authn/token_field/generator/routable_token.rb', line 42

def generate_token
  routing_hash
    .then { |routing_hash| build_payload(routing_hash) }
    .then { |payload| check_payload_size!(payload) }
    .then { |payload| encode_payload(payload, self.class.random_bytes(RANDOM_BYTES_LENGTH)) }
    .then { |encoded_payload| append_crc(encoded_payload) }
end