Module: Firebase::Admin::Auth::Utils

Included in:
CertificatesFetcher, UserManager
Defined in:
lib/firebase/admin/auth/utils.rb

Constant Summary collapse

AUTH_EMULATOR_HOST_VAR =
"FIREBASE_AUTH_EMULATOR_HOST"
INVALID_CHARS_PATTERN =
/[^a-z0-9:\/?#\[\\\]@!$&'()*+,;=.\-_~%]/i
HOSTNAME_PATTERN =
/^[a-zA-Z0-9]+[\w-]*([.]?[a-zA-Z0-9]+[\w-]*)*$/
PATHNAME_PATTERN =
/^(\/[\w\-.~!$'()*+,;=:@%]+)*\/?$/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_emulator_hostObject



72
73
74
75
76
77
78
79
80
# File 'lib/firebase/admin/auth/utils.rb', line 72

def get_emulator_host
  emulator_host = ENV[AUTH_EMULATOR_HOST_VAR]&.strip
  return nil unless emulator_host && !emulator_host.empty?
  if emulator_host.include?("//")
    msg = "Invalid #{AUTH_EMULATOR_HOST_VAR}: \"#{emulator_host}\". It must follow the format \"host:post\""
    raise ArgumentError, msg
  end
  emulator_host
end

.get_emulator_v1_urlObject



82
83
84
85
# File 'lib/firebase/admin/auth/utils.rb', line 82

def get_emulator_v1_url
  return nil unless (emulator_host = get_emulator_host)
  "http://#{emulator_host}/identitytoolkit.googleapis.com/v1"
end

.is_emulated?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/firebase/admin/auth/utils.rb', line 87

def is_emulated?
  !!get_emulator_host
end

.validate_url(url) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/firebase/admin/auth/utils.rb', line 59

def validate_url(url)
  return false unless url.is_a?(String) && !url.empty? && !url.match?(INVALID_CHARS_PATTERN)
  begin
    uri = URI.parse(url)
    return false unless %w[https http].include?(uri.scheme)
    return false unless uri.hostname&.match?(HOSTNAME_PATTERN)
    return false unless uri.path.empty? || uri.path == "/" || uri.path.match?(PATHNAME_PATTERN)
    true
  rescue
    false
  end
end

Instance Method Details

#to_boolean(val) ⇒ Object



53
54
55
# File 'lib/firebase/admin/auth/utils.rb', line 53

def to_boolean(val)
  !!val unless val.nil?
end

#validate_display_name(name, required: false) ⇒ Object

Raises:



47
48
49
50
51
# File 'lib/firebase/admin/auth/utils.rb', line 47

def validate_display_name(name, required: false)
  return nil if name.nil? && !required
  raise ArgumentError, "display_name must be a non-empty string" unless name.is_a?(String) && !name.empty?
  name
end

#validate_email(email, required: false) ⇒ Object

Raises:



18
19
20
21
22
23
24
# File 'lib/firebase/admin/auth/utils.rb', line 18

def validate_email(email, required: false)
  return nil if email.nil? && !required
  raise ArgumentError, "email must be a non-empty string" unless email.is_a?(String) && !email.empty?
  parts = email.split("@")
  raise ArgumentError, "email is malformed #{email}" unless parts.length == 2 && !parts[0].empty? && !parts[1].empty?
  email
end

#validate_password(password, required: false) ⇒ Object

Raises:



33
34
35
36
37
38
# File 'lib/firebase/admin/auth/utils.rb', line 33

def validate_password(password, required: false)
  return nil if password.nil? && !required
  raise ArgumentError, "password must a string" unless password.is_a?(String)
  raise ArgumentError, "password must be at least 6 characters long" unless password.length >= 6
  password
end

#validate_phone_number(phone_number, required: false) ⇒ Object

Raises:



26
27
28
29
30
31
# File 'lib/firebase/admin/auth/utils.rb', line 26

def validate_phone_number(phone_number, required: false)
  return nil if phone_number.nil? && !required
  raise ArgumentError, "phone_number must be a non-empty string" unless phone_number.is_a?(String)
  raise ArgumentError, "phone_number must be an E.164 identifier" unless phone_number.match?(/^\+\d{1,14}$/)
  phone_number
end

#validate_photo_url(url, required: false) ⇒ Object

Raises:



40
41
42
43
44
45
# File 'lib/firebase/admin/auth/utils.rb', line 40

def validate_photo_url(url, required: false)
  return nil if url.nil? && !required
  raise ArgumentError, "photo_url must be a valid url" unless url.is_a?(String) && !url.empty?
  raise ArgumentError, "photo_url must be a valid url" unless validate_url(url)
  url
end

#validate_uid(uid, required: false) ⇒ Object

Raises:



11
12
13
14
15
16
# File 'lib/firebase/admin/auth/utils.rb', line 11

def validate_uid(uid, required: false)
  return nil if uid.nil? && !required
  raise ArgumentError, "uid must be a string" unless uid.is_a?(String)
  raise ArgumentError, "uid must be non-empty with no more than 128 chars" unless uid.length.between?(1, 128)
  uid
end