Module: Devise::Models::Auth0::ClassMethods

Defined in:
lib/devise/models/auth0.rb

Instance Method Summary collapse

Instance Method Details

#auth0_clientObject



204
205
206
# File 'lib/devise/models/auth0.rb', line 204

def auth0_client
  @auth0_client ||= ::Devise::Auth0::Client.new(auth0_config)
end

#auth0_configObject



194
195
196
197
198
199
200
201
202
# File 'lib/devise/models/auth0.rb', line 194

def auth0_config
  return @auth0_config unless @auth0_config.nil?

  @auth0_config ||= ::Devise.auth0.pristine
  @auth0_config.update(::Devise.auth0.values)
  @auth0_config.update(auth0_options) if defined?(@auth0_options)
  @auth0_config.finalize!
  @auth0_config
end

#auth0_where_conditions(provider:, uid:, email: nil) ⇒ Object



188
189
190
191
192
# File 'lib/devise/models/auth0.rb', line 188

def auth0_where_conditions(provider:, uid:, email: nil)
  sql = arel_table[:provider].eq(provider).and(arel_table[:uid].eq(uid))
  sql = sql.or(arel_table[:email].eq(email)) if email && column_names.include?("email")
  sql
end

#from_auth0_omniauth(auth) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/devise/models/auth0.rb', line 167

def from_auth0_omniauth(auth)
  return unless auth0_config.omniauth

  uid = auth.uid.include?("|") ? auth.uid.split("|").last : auth.uid
  user = where(
    auth0_where_conditions(
      provider: auth.provider,
      uid: uid,
      email: auth.info.email,
    ),
  ).first_or_create do |user|
    user.provider = auth.provider
    user.uid = uid
    user.email = auth.info.email if user.respond_to?(:email=)
    user.password = Devise.friendly_token[0, 20] if user.respond_to?(:password=)
    user.after_auth0_omniauth_created(auth)
  end
  user.after_auth0_omniauth(auth)
  user
end

#from_auth0_token(token) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/devise/models/auth0.rb', line 143

def from_auth0_token(token)
  user = where(
    auth0_where_conditions(
      provider: token.provider,
      uid: token.uid,
      email: token.user["email"],
    ),
  ).first_or_create do |user|
    user.provider = token.provider
    user.uid = token.uid
    user.email = token.user["email"] if user.respond_to?(:email=)
    user.password = Devise.friendly_token[0, 20] if user.respond_to?(:password=)
    user.bot = token.bot? if user.respond_to?(:bot=)
    user.after_auth0_token_created(token)
  end
  user.auth0_scopes = token.scopes.dup.concat(token.permissions).uniq
  user.after_auth0_token(token)
  user
end

#parse_auth0_token(token) ⇒ Object



163
164
165
# File 'lib/devise/models/auth0.rb', line 163

def parse_auth0_token(token)
  ::Devise::Auth0::Token.parse(token, self)
end