Class: Clavis::OauthIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/clavis/oauth_identity.rb,
lib/clavis/oauth_identity.rb

Overview

Stub class for environments where ActiveRecord is not available

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ OauthIdentity

Returns a new instance of OauthIdentity.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/clavis/oauth_identity.rb', line 107

def initialize(attributes = {})
  @provider = attributes[:provider]
  @uid = attributes[:uid]
  @token = attributes[:token]
  @refresh_token = attributes[:refresh_token]
  @expires_at = attributes[:expires_at]
  self.auth_data = attributes[:auth_data]
  @authenticatable = attributes[:authenticatable]
  @persisted = attributes[:persisted] || false
  @updated_at = attributes[:updated_at] || Time.now
end

Instance Attribute Details

#authenticatableObject

Returns the value of attribute authenticatable.



96
97
98
# File 'lib/clavis/oauth_identity.rb', line 96

def authenticatable
  @authenticatable
end

#expires_atObject

Returns the value of attribute expires_at.



96
97
98
# File 'lib/clavis/oauth_identity.rb', line 96

def expires_at
  @expires_at
end

#providerObject

Returns the value of attribute provider.



96
97
98
# File 'lib/clavis/oauth_identity.rb', line 96

def provider
  @provider
end

#refresh_tokenObject

Override refresh_token getter to decrypt the token



38
39
40
# File 'lib/clavis/oauth_identity.rb', line 38

def refresh_token
  Clavis::Security::TokenStorage.decrypt(self[:refresh_token])
end

#tokenObject

Override token getter to decrypt the token



27
28
29
# File 'lib/clavis/oauth_identity.rb', line 27

def token
  Clavis::Security::TokenStorage.decrypt(self[:token])
end

#uidObject

Returns the value of attribute uid.



96
97
98
# File 'lib/clavis/oauth_identity.rb', line 96

def uid
  @uid
end

#updated_atObject

Returns the value of attribute updated_at.



96
97
98
# File 'lib/clavis/oauth_identity.rb', line 96

def updated_at
  @updated_at
end

Instance Method Details

#auth_dataObject

Custom accessor for auth_data to ensure it's always initialized as a hash



99
100
101
# File 'lib/clavis/oauth_identity.rb', line 99

def auth_data
  @auth_data ||= {}
end

#auth_data=(value) ⇒ Object



103
104
105
# File 'lib/clavis/oauth_identity.rb', line 103

def auth_data=(value)
  @auth_data = value || {}
end

#ensure_fresh_tokenObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/clavis/oauth_identity.rb', line 56

def ensure_fresh_token
  return token unless token_expired?
  return nil unless refresh_token && !refresh_token.empty?

  begin
    provider_instance = Clavis.provider(
      provider.to_sym,
      redirect_uri: Clavis.configuration.providers.dig(provider.to_sym, :redirect_uri)
    )

    new_tokens = provider_instance.refresh_token(refresh_token)

    self.token = new_tokens[:access_token]
    self.refresh_token = new_tokens[:refresh_token] || refresh_token
    self.expires_at = new_tokens[:expires_at] ? Time.at(new_tokens[:expires_at]) : nil

    token
  rescue Clavis::UnsupportedOperation => e
    Clavis::Logging.log_token_refresh(provider, false, e.message)
    token
  rescue Clavis::Error => e
    Clavis::Logging.log_error(e)
    nil
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/clavis/oauth_identity.rb', line 127

def persisted?
  @persisted
end

#save!Object



131
132
133
134
# File 'lib/clavis/oauth_identity.rb', line 131

def save!
  @persisted = true
  self
end

#store_standardized_user_info!Object

Store standardized user info in the auth_data field



83
84
85
86
87
88
89
90
91
# File 'lib/clavis/oauth_identity.rb', line 83

def store_standardized_user_info!
  return unless auth_data.present?

  normalized = Clavis::UserInfoNormalizer.normalize(provider, auth_data)

  # Store the normalized data in auth_data under a standardized key
  self.auth_data = auth_data.merge("standardized" => normalized)
  save! if persisted?
end

#token_expired?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/clavis/oauth_identity.rb', line 48

def token_expired?
  expires_at.present? && expires_at < Time.current
end

#token_valid?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/clavis/oauth_identity.rb', line 52

def token_valid?
  token.present? && !token_expired?
end

#userObject

Alias for authenticatable to support User model assignment



16
17
18
# File 'lib/clavis/oauth_identity.rb', line 16

def user
  authenticatable
end

#user=(user) ⇒ Object

Alias for authenticatable= to support User model assignment



21
22
23
24
# File 'lib/clavis/oauth_identity.rb', line 21

def user=(user)
  self.authenticatable = user
  self.authenticatable_type = user.class.name
end