Module: LogtoUtils

Defined in:
lib/logto/core/utils.rb

Class Method Summary collapse

Class Method Details

.build_access_token_key(resource:, organization_id: nil) ⇒ Object



43
44
45
# File 'lib/logto/core/utils.rb', line 43

def self.build_access_token_key(resource:, organization_id: nil)
  "#{organization_id ? "##{organization_id}" : ""}:#{resource || "default"}"
end

.generate_code_challenge(code_verifier) ⇒ Object



35
36
37
# File 'lib/logto/core/utils.rb', line 35

def self.generate_code_challenge(code_verifier)
  Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)).tr("=", "")
end

.generate_code_verifierObject



31
32
33
# File 'lib/logto/core/utils.rb', line 31

def self.generate_code_verifier
  SecureRandom.urlsafe_base64(32)
end

.generate_stateObject



39
40
41
# File 'lib/logto/core/utils.rb', line 39

def self.generate_state
  SecureRandom.urlsafe_base64(32)
end

.parse_json_safe(json_str_or_hash, struct_class) ⇒ Struct

Parses a JSON string and maps it to a given Struct class, handling unknown keys.

Parameters:

  • json_str_or_hash (String, Hash)

    The JSON string or hash to be parsed.

  • struct_class (Class)

    The Struct class to map the JSON data to. The strcut class must have a ‘:unknown_keys` member and a `keyword_init: true` keyword argument.

Returns:

  • (Struct)

    An instance of the given Struct class populated with known keys and a hash of unknown keys.



12
13
14
15
16
17
18
# File 'lib/logto/core/utils.rb', line 12

def self.parse_json_safe(json_str_or_hash, struct_class)
  data = json_str_or_hash.is_a?(String) ? JSON.parse(json_str_or_hash) : json_str_or_hash
  known_keys = struct_class.members - [:unknown_keys]
  known_data = data.select { |key, _| known_keys.include?(key.to_sym) }
  unknown_data = data.reject { |key, _| known_keys.include?(key.to_sym) }
  struct_class.new(**known_data, unknown_keys: unknown_data)
end

.with_reserved_scopes(scopes) ⇒ Array<String>

Returns The scopes with reserved scopes added.

Examples:

LogtoUtils.with_reserved_scopes(['foo', 'bar'])
# => ['foo', 'bar', 'openid', 'offline_access', 'profile']

Parameters:

  • scopes (Array<String>, nil)

    The scopes to be added reserved scopes to.

Returns:

  • (Array<String>)

    The scopes with reserved scopes added.



25
26
27
28
29
# File 'lib/logto/core/utils.rb', line 25

def self.with_reserved_scopes(scopes)
  unique_scopes = scopes || []
  unique_scopes += LogtoCore::RESERVED_SCOPE.values
  unique_scopes.uniq
end