Module: BetterAuth::APIKey::Utils

Defined in:
lib/better_auth/api_key/utils.rb

Class Method Summary collapse

Class Method Details

.auth_required?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/better_auth/api_key/utils.rb', line 88

def auth_required?(ctx)
  !!(ctx.request || (ctx.headers && !ctx.headers.empty?))
end

.background_tasks?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/better_auth/api_key/utils.rb', line 84

def background_tasks?(ctx)
  ctx.context.options.advanced.dig(:background_tasks, :handler).respond_to?(:call)
end

.decode_json(value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/better_auth/api_key/utils.rb', line 17

def decode_json(value)
  return nil if value.nil?
  return value if value.is_a?(Hash)

  parsed = JSON.parse(value.to_s)
  parsed.is_a?(String) ? decode_json(parsed) : parsed
rescue JSON::ParserError
  nil
end

.encode_json(value) ⇒ Object



11
12
13
14
15
# File 'lib/better_auth/api_key/utils.rb', line 11

def encode_json(value)
  return nil if value.nil?

  JSON.generate(value)
end

.error_code(error) ⇒ Object



73
74
75
# File 'lib/better_auth/api_key/utils.rb', line 73

def error_code(error)
  BetterAuth::Plugins::API_KEY_ERROR_CODES.key(error.message) || error.code.to_s
end

.error_payload(error) ⇒ Object



77
78
79
80
81
82
# File 'lib/better_auth/api_key/utils.rb', line 77

def error_payload(error)
  payload = error.to_h
  return payload if payload.is_a?(Hash) && payload.key?(:details)

  {message: error.message, code: error_code(error)}
end

.normalize_time(value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/better_auth/api_key/utils.rb', line 27

def normalize_time(value)
  return value if value.is_a?(Time)
  return nil if value.nil?

  Time.parse(value.to_s)
rescue ArgumentError
  nil
end

.public_record(record, reveal_key: nil, include_key_field: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/better_auth/api_key/utils.rb', line 36

def public_record(record, reveal_key: nil, include_key_field: false)
  data = record.transform_keys(&:to_sym)
  output = data.except(:key)
  output[:configId] ||= BetterAuth::APIKey::Types.record_config_id(record)
  output[:referenceId] ||= BetterAuth::APIKey::Types.record_reference_id(record)
  output[:key] = reveal_key if include_key_field && reveal_key
  output[:metadata] = decode_json(data[:metadata])
  output[:permissions] = decode_json(data[:permissions])
  output
end

.sort_records(records, sort_by, direction) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/better_auth/api_key/utils.rb', line 47

def sort_records(records, sort_by, direction)
  return records unless sort_by

  key = BetterAuth::Schema.storage_key(sort_by)
  sorted = records.sort_by { |record| record[key] || record[key.to_sym] || "" }
  if direction.to_s.downcase == "desc"
    sorted.reverse
  else
    sorted
  end
end

.validate_list_query!(query) ⇒ Object

Raises:

  • (BetterAuth::APIError)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/better_auth/api_key/utils.rb', line 59

def validate_list_query!(query)
  %i[limit offset].each do |key|
    next unless query.key?(key)

    value = query[key]
    raise BetterAuth::APIError.new("BAD_REQUEST", message: "Invalid #{key}") unless value.to_s.match?(/\A\d+\z/)
  end

  direction = query[:sort_direction]
  return if direction.nil? || %w[asc desc].include?(direction.to_s.downcase)

  raise BetterAuth::APIError.new("BAD_REQUEST", message: "Invalid sortDirection")
end