Class: Devise::Auth0::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/devise/auth0/token.rb

Overview

Helpers to parse token from a request and to a response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth, resource_class) ⇒ Token

Returns a new instance of Token.



20
21
22
23
# File 'lib/devise/auth0/token.rb', line 20

def initialize(auth, resource_class)
  @auth = auth.presence
  @resource_class = resource_class
end

Class Method Details

.parse(auth, resource_class) ⇒ Object



13
14
15
16
17
# File 'lib/devise/auth0/token.rb', line 13

def parse(auth, resource_class)
  token = new(auth, resource_class)
  token.verify
  token
end

Instance Method Details

#auth0_idObject



33
34
35
36
37
38
# File 'lib/devise/auth0/token.rb', line 33

def auth0_id
  return if verify.nil?
  return "auth0|#{verify[0]["azp"]}" if bot?

  verify[0]["sub"]
end

#bot?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/devise/auth0/token.rb', line 53

def bot?
  return false if verify.nil?

  verify[0]["gty"] == "client-credentials"
end

#permissionsObject



65
66
67
68
69
# File 'lib/devise/auth0/token.rb', line 65

def permissions
  return [] if verify.nil?

  verify[0]["permissions"].presence || []
end

#providerObject



25
26
27
# File 'lib/devise/auth0/token.rb', line 25

def provider
  auth0_id&.split("|")&.first
end

#scopesObject



59
60
61
62
63
# File 'lib/devise/auth0/token.rb', line 59

def scopes
  return [] if verify.nil?

  verify[0]["scope"].to_s.split(" ")
end

#uidObject



29
30
31
# File 'lib/devise/auth0/token.rb', line 29

def uid
  auth0_id&.split("|")&.last
end

#userObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/devise/auth0/token.rb', line 40

def user
  @user ||= if bot?
    {
      "user_id" => uid,
      "email" => "#{uid}@#{config.domain}",
    }
  else
    ::Devise.auth0.cache.fetch("devise-auth0/#{auth0_id}", expires_in: ::Devise.auth0.cache_expires_in) do
      client.user(auth0_id)
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/devise/auth0/token.rb', line 88

def valid?
  !verify.nil?
end

#verifyObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/devise/auth0/token.rb', line 71

def verify
  @payload ||= JWT.decode(
    @auth,
    nil,
    true, # Verify the signature of this token
    algorithms: config.algorithm,
    iss: issuer,
    verify_iss: true,
    aud: config.aud,
    verify_aud: true,
  ) do |header|
    jwks_hash[header["kid"]]
  end
rescue JWT::DecodeError
  nil
end