Module: Clavis::Models::Concerns::OauthAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/clavis/models/concerns/oauth_authenticatable.rb

Overview

This module adds OAuth authentication capabilities to a model It's typically included in the User model

Instance Method Summary collapse

Instance Method Details

#oauth_avatar_urlString?

Get the profile picture URL from the OAuth identity

Returns:

  • (String, nil)

    The profile picture URL from the OAuth identity or nil



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 81

def oauth_avatar_url
  return nil unless oauth_identity&.auth_data

  # First check standardized data
  avatar = extract_standardized_value(oauth_identity.auth_data, "avatar_url")

  # Fall back to various possible fields in raw auth data
  avatar ||= oauth_identity.auth_data["image"] || oauth_identity.auth_data[:image]
  avatar ||= oauth_identity.auth_data["picture"] || oauth_identity.auth_data[:picture]
  avatar ||= oauth_identity.auth_data["avatar_url"] || oauth_identity.auth_data[:avatar_url]

  avatar
end

#oauth_emailString?

Get the email from the OAuth identity

Returns:

  • (String, nil)

    The email from the OAuth identity or nil



53
54
55
56
57
58
59
60
61
62
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 53

def oauth_email
  return nil unless oauth_identity&.auth_data

  # First check standardized data
  email = extract_standardized_value(oauth_identity.auth_data, "email")

  # Fall back to raw auth data
  email ||= oauth_identity.auth_data["email"] || oauth_identity.auth_data[:email]
  email
end

#oauth_identityClavis::OauthIdentity?

Get the primary OAuth identity for this user

Returns:



24
25
26
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 24

def oauth_identity
  oauth_identities.first
end

#oauth_identity_for(provider) ⇒ Clavis::OauthIdentity?

Get the identity for a specific provider

Parameters:

  • provider (String, Symbol)

    The provider name

Returns:



46
47
48
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 46

def oauth_identity_for(provider)
  oauth_identities.find_by(provider: provider)
end

#oauth_nameString?

Get the name from the OAuth identity

Returns:

  • (String, nil)

    The name from the OAuth identity or nil



67
68
69
70
71
72
73
74
75
76
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 67

def oauth_name
  return nil unless oauth_identity&.auth_data

  # First check standardized data
  name = extract_standardized_value(oauth_identity.auth_data, "name")

  # Fall back to raw auth data
  name ||= oauth_identity.auth_data["name"] || oauth_identity.auth_data[:name]
  name
end

#oauth_refresh_tokenString?

Get the refresh token for the primary identity

Returns:

  • (String, nil)

    The refresh token or nil



105
106
107
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 105

def oauth_refresh_token
  oauth_identity&.refresh_token
end

#oauth_tokenString?

Get the access token for the primary identity

Returns:

  • (String, nil)

    The access token or nil



98
99
100
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 98

def oauth_token
  oauth_identity&.token
end

#oauth_token_expired?Boolean

Check if the token for the primary identity has expired

Returns:

  • (Boolean)

    True if the token has expired, false otherwise or if expires_at is nil



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 112

def oauth_token_expired?
  return false unless oauth_identity&.expires_at

  # Convert to Time object if it's an Integer
  expires_at = if oauth_identity.expires_at.is_a?(Integer)
                 Time.at(oauth_identity.expires_at)
               else
                 oauth_identity.expires_at
               end
  expires_at < Time.now
end

#oauth_user?Boolean

Determines if this user was created via OAuth

Returns:

  • (Boolean)

    True if the user has any OAuth identities or oauth_user flag is true



31
32
33
34
35
36
37
38
39
40
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 31

def oauth_user?
  return true if respond_to?(:oauth_user) && oauth_user

  # Handle both Array and ActiveRecord collection
  if oauth_identities.respond_to?(:exists?)
    oauth_identities.exists?
  else
    oauth_identities.any?
  end
end

#refresh_oauth_tokenString?

Refresh the access token for the primary identity

Returns:

  • (String, nil)

    The new access token or nil if refresh failed or not supported



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/clavis/models/concerns/oauth_authenticatable.rb', line 127

def refresh_oauth_token
  return nil unless oauth_identity&.refresh_token.present?

  provider = oauth_identity.provider
  provider_instance = Clavis.provider(provider)
  return nil unless provider_instance.respond_to?(:refresh_token)

  # Refresh the token
  new_tokens = provider_instance.refresh_token(oauth_identity.refresh_token)

  # Update the identity with the new tokens
  oauth_identity.token = new_tokens[:access_token] || new_tokens[:token]
  oauth_identity.refresh_token = new_tokens[:refresh_token] if new_tokens[:refresh_token].present?
  oauth_identity.expires_at = new_tokens[:expires_at] if new_tokens[:expires_at].present?
  oauth_identity.save!

  # Return the new token
  oauth_identity.token
end