Class: Devise::Auth0::Token
- Inherits:
-
Object
- Object
- Devise::Auth0::Token
- 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
- #auth0_id ⇒ Object
- #bot? ⇒ Boolean
-
#initialize(auth, resource_class) ⇒ Token
constructor
A new instance of Token.
- #permissions ⇒ Object
- #provider ⇒ Object
- #scopes ⇒ Object
- #uid ⇒ Object
- #user ⇒ Object
- #valid? ⇒ Boolean
- #verify ⇒ Object
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_id ⇒ Object
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
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 |
#permissions ⇒ Object
65 66 67 68 69 |
# File 'lib/devise/auth0/token.rb', line 65 def return [] if verify.nil? verify[0]["permissions"].presence || [] end |
#provider ⇒ Object
25 26 27 |
# File 'lib/devise/auth0/token.rb', line 25 def provider auth0_id&.split("|")&.first end |
#scopes ⇒ Object
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 |
#uid ⇒ Object
29 30 31 |
# File 'lib/devise/auth0/token.rb', line 29 def uid auth0_id&.split("|")&.last end |
#user ⇒ Object
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
88 89 90 |
# File 'lib/devise/auth0/token.rb', line 88 def valid? !verify.nil? end |
#verify ⇒ Object
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 |