Module: BetterAuth::SSO::Linking::Types

Defined in:
lib/better_auth/sso/linking/types.rb

Constant Summary collapse

REQUIRED_PROFILE_KEYS =
{
  provider_type: "providerType",
  provider_id: "providerId",
  account_id: "accountId",
  email: "email",
  email_verified: "emailVerified"
}.freeze

Class Method Summary collapse

Class Method Details

.normalized_profile(profile) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/better_auth/sso/linking/types.rb', line 17

def normalized_profile(profile)
  raw_attributes = raw_value(profile, :raw_attributes, "rawAttributes", "raw_attributes")
  source = BetterAuth::Plugins.normalize_hash(profile || {})
  normalized = {
    provider_type: source[:provider_type].to_s,
    provider_id: source[:provider_id].to_s,
    account_id: source[:account_id].to_s,
    email: source[:email].to_s.downcase,
    email_verified: !!source[:email_verified]
  }
  normalized[:name] = source[:name] if source.key?(:name)
  normalized[:image] = source[:image] if source.key?(:image)
  normalized[:raw_attributes] = raw_attributes unless raw_attributes.nil?

  missing = REQUIRED_PROFILE_KEYS.filter_map do |key, upstream_name|
    value = normalized[key]
    upstream_name if value.nil? || (value.respond_to?(:empty?) && value.empty?)
  end
  raise ArgumentError, "Missing normalized SSO profile fields: #{missing.join(", ")}" unless missing.empty?
  raise ArgumentError, "Invalid normalized SSO profile providerType: #{normalized[:provider_type]}" unless BetterAuth::SSO::Types.provider_type?(normalized[:provider_type])

  normalized.freeze
end

.raw_value(profile, *keys) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/better_auth/sso/linking/types.rb', line 41

def raw_value(profile, *keys)
  return nil unless profile.respond_to?(:key?) && profile.respond_to?(:[])

  keys.each do |key|
    return profile[key] if profile.key?(key)
  end
  nil
end