Module: Legion::Extensions::Llm::Ledger::Helpers::CallerIdentity
- Defined in:
- lib/legion/extensions/llm/ledger/helpers/caller_identity.rb
Constant Summary collapse
- GENERIC_IDENTITIES =
%w[anonymous process service system user].freeze
Class Method Summary collapse
- .first_present(*values) ⇒ Object
- .hash_value(hash, key) ⇒ Object
- .header_value(headers, key) ⇒ Object
- .integer_header(headers, key) ⇒ Object
- .normalize(caller_raw: nil, identity: nil, headers: {}) ⇒ Object
- .normalize_identity_value(value, type) ⇒ Object
- .present?(value) ⇒ Boolean
Class Method Details
.first_present(*values) ⇒ Object
62 63 64 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 62 def first_present(*values) values.find { |value| present?(value) } end |
.hash_value(hash, key) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 66 def hash_value(hash, key) return nil unless hash.respond_to?(:key?) return hash[key] if hash.key?(key) string_key = key.to_s hash[string_key] if hash.key?(string_key) end |
.header_value(headers, key) ⇒ Object
74 75 76 77 78 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 74 def header_value(headers, key) return nil unless headers.respond_to?(:key?) headers[key] || headers[key.to_sym] end |
.integer_header(headers, key) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 80 def integer_header(headers, key) raw = header_value(headers, key) return nil if raw.nil? int = raw.to_i int.positive? ? int : nil end |
.normalize(caller_raw: nil, identity: nil, headers: {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 13 def normalize(caller_raw: nil, identity: nil, headers: {}) caller_hash = caller_raw.is_a?(Hash) ? caller_raw : {} caller = hash_value(caller_hash, :requested_by) caller = caller_hash unless caller.is_a?(Hash) identity_hash = identity.is_a?(Hash) ? identity : {} extension = hash_value(caller_hash, :extension) type = first_present( hash_value(identity_hash, :type), hash_value(identity_hash, :kind), header_value(headers, 'x-legion-identity-kind'), header_value(headers, 'x-legion-caller-type'), hash_value(caller, :type), hash_value(caller, :kind), extension && 'extension' ) raw_identity = first_present( header_value(headers, 'x-legion-identity-canonical-name'), hash_value(identity_hash, :id), hash_value(identity_hash, :canonical_name), hash_value(identity_hash, :identity), hash_value(identity_hash, :username), header_value(headers, 'x-legion-identity'), header_value(headers, 'x-legion-caller-identity'), hash_value(caller, :id), hash_value(caller, :canonical_name), hash_value(caller, :identity), hash_value(caller, :username), extension && "extension:#{extension}" ) { identity: normalize_identity_value(raw_identity, type), type: type, principal_id: integer_header(headers, 'x-legion-identity-db-principal-id'), identity_id: integer_header(headers, 'x-legion-identity-db-identity-id') }.compact end |
.normalize_identity_value(value, type) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 52 def normalize_identity_value(value, type) return nil unless present?(value) text = value.to_s return text if text.include?(':') || text.include?('@') return "#{type}:#{text}" if type && GENERIC_IDENTITIES.include?(text) text end |
.present?(value) ⇒ Boolean
88 89 90 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 88 def present?(value) !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end |