Module: Descope::Mixins::Validation

Overview

Module to provide validation for specific data structures.

Constant Summary

Constants included from Common

Common::COOKIE_DATA_NAME, Common::DEFAULT_BASE_URL, Common::DEFAULT_JWT_VALIDATION_LEEWAY, Common::DEFAULT_TIMEOUT_SECONDS, Common::PHONE_REGEX, Common::REDIRECT_LOCATION_COOKIE_NAME, Common::REFRESH_SESSION_COOKIE_NAME, Common::REFRESH_SESSION_TOKEN_NAME, Common::SESSION_COOKIE_NAME, Common::SESSION_TOKEN_NAME

Instance Method Summary collapse

Methods included from Common

#deep_copy, #get_method_string

Instance Method Details

#validate_code(code) ⇒ Object

Raises:



82
83
84
# File 'lib/descope/mixins/validation.rb', line 82

def validate_code(code)
  raise AuthException.new('Code cannot be empty', code: 400) unless code.is_a?(String) && !code.empty?
end

#validate_email(email) ⇒ Object

Raises:



37
38
39
# File 'lib/descope/mixins/validation.rb', line 37

def validate_email(email)
  raise AuthException.new('email cannot be empty', code: 400) unless email.is_a?(String) && !email.empty?
end

#validate_login_id(login_id) ⇒ Object

Raises:



25
26
27
# File 'lib/descope/mixins/validation.rb', line 25

def ()
  raise AuthException, 'login_id cannot be empty' unless .is_a?(String) && !.empty?
end

#validate_password(password) ⇒ Object

Raises:



33
34
35
# File 'lib/descope/mixins/validation.rb', line 33

def validate_password(password)
  raise AuthException, 'password cannot be empty' unless password.is_a?(String) && !password.empty?
end

#validate_phone(method, phone) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/descope/mixins/validation.rb', line 51

def validate_phone(method, phone)
  phone_number_is_invalid = !phone.match?(PHONE_REGEX) unless phone.nil?

  raise AuthException.new('Phone number cannot be empty', code: 400) unless phone.is_a?(String) && !phone.empty?
  raise AuthException.new("Invalid pattern for phone number: #{phone}", code: 400) if phone_number_is_invalid

  valid_methods = DeliveryMethod.constants.map { |constant| DeliveryMethod.const_get(constant) }

  # rubocop:disable Style/LineLength
  unless valid_methods.include?(method)
    valid_methods_names = valid_methods.map { |m| "DeliveryMethod::#{DeliveryMethod.constants[valid_methods.index(m)]}" }.join(', ')
    raise AuthException.new("Delivery method should be one of the following: #{valid_methods_names}", code: 400)
  end
end

#validate_redirect_url(return_url) ⇒ Object

Raises:



76
77
78
79
80
# File 'lib/descope/mixins/validation.rb', line 76

def validate_redirect_url(return_url)
  return if return_url.is_a?(String) && !return_url.empty?

  raise AuthException.new('Return_url cannot be empty', code: 400)
end

#validate_refresh_token_not_nil(refresh_token) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/descope/mixins/validation.rb', line 45

def validate_refresh_token_not_nil(refresh_token)
  return unless refresh_token.nil? || refresh_token.empty?

  raise AuthException.new('Refresh token is required to refresh a session', code: 400)
end

#validate_scim_group_id(group_id) ⇒ Object

Raises:



86
87
88
89
90
91
# File 'lib/descope/mixins/validation.rb', line 86

def validate_scim_group_id(group_id)
  return if group_id.is_a?(String) && !group_id.empty?

  raise AuthException.new('SCIM Group ID cannot be empty', code: 400)

end

#validate_tenant(tenant) ⇒ Object

Raises:



72
73
74
# File 'lib/descope/mixins/validation.rb', line 72

def validate_tenant(tenant)
  raise AuthException.new('Tenant cannot be empty', code: 400) unless tenant.is_a?(String) && !tenant.empty?
end

#validate_tenants(key_tenants) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/descope/mixins/validation.rb', line 10

def validate_tenants(key_tenants)
  raise ArgumentError, 'key_tenants should be an Array of hashes' unless key_tenants.is_a? Array

  key_tenants.each do |tenant|
    unless tenant.is_a? Hash
      raise ArgumentError,
            'Each tenant should be a Hash of tenant_id and optional role_names array'
    end

    tenant_symbolized = tenant.transform_keys(&:to_sym)

    raise ArgumentError, "Missing tenant_id key in tenant: #{tenant}" unless tenant_symbolized.key?(:tenant_id)
  end
end

#validate_token_not_empty(token) ⇒ Object

Raises:



41
42
43
# File 'lib/descope/mixins/validation.rb', line 41

def validate_token_not_empty(token)
  raise AuthException.new('Token cannot be empty', code: 400) unless token.is_a?(String) && !token.empty?
end

#validate_user_id(user_id) ⇒ Object



29
30
31
# File 'lib/descope/mixins/validation.rb', line 29

def validate_user_id(user_id)
  raise Descope::ArgumentException, 'Missing user id' if user_id.nil? || user_id.to_s.empty?
end

#verify_provider(oauth_provider) ⇒ Object



66
67
68
69
70
# File 'lib/descope/mixins/validation.rb', line 66

def verify_provider(oauth_provider)
  return false if oauth_provider.to_s.empty? || oauth_provider.nil?

  true
end