Module: LogtoUtils
- Defined in:
- lib/logto/core/utils.rb
Class Method Summary collapse
- .build_access_token_key(resource:, organization_id: nil) ⇒ Object
- .generate_code_challenge(code_verifier) ⇒ Object
- .generate_code_verifier ⇒ Object
- .generate_state ⇒ Object
-
.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.
-
.with_reserved_scopes(scopes) ⇒ Array<String>
The scopes with reserved scopes added.
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_verifier ⇒ Object
31 32 33 |
# File 'lib/logto/core/utils.rb', line 31 def self.generate_code_verifier SecureRandom.urlsafe_base64(32) end |
.generate_state ⇒ Object
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.
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.
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 |