Class: Keycloak::AccessToken
- Inherits:
-
Object
- Object
- Keycloak::AccessToken
- Defined in:
- lib/keycloak/access_token.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#azp ⇒ Object
readonly
Returns the value of attribute azp.
-
#client_roles ⇒ Object
readonly
Returns the value of attribute client_roles.
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#jti ⇒ Object
readonly
Returns the value of attribute jti.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#phone_number ⇒ Object
readonly
Returns the value of attribute phone_number.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#sub ⇒ Object
readonly
Returns the value of attribute sub.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authorization ⇒ Object
- #client_id ⇒ Object
- #expired? ⇒ Boolean
- #has_client_role?(role) ⇒ Boolean
- #has_role?(role, include_client_role = true) ⇒ Boolean
-
#initialize(realm, access_token, decoded_token, client_id = nil) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(realm, access_token, decoded_token, client_id = nil) ⇒ AccessToken
Returns a new instance of AccessToken.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/keycloak/access_token.rb', line 6 def initialize(realm, access_token, decoded_token, client_id = nil) @realm = realm @access_token = access_token @metadata = decoded_token[0] @jti = @metadata["jti"] @exp = Time.at(@metadata["exp"]).to_datetime @sub = @metadata["sub"] @azp = @metadata["azp"] if realm_access = @metadata["realm_access"] @roles = realm_access["roles"] || [] end if resource_access = @metadata["resource_access"] @client_roles = (client_id && resource_access.dig(client_id, "roles")) || [] end @scope = @metadata["scope"] @phone_number = @metadata["phone_number"] @username = @metadata["username"] || @metadata["preferred_username"] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/keycloak/access_token.rb', line 45 def method_missing(name, *args, &block) regex = /^has_(.*?)_role\?$/ if name.match?(regex) return has_role?(name.match(regex)[1]) end super end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def access_token @access_token end |
#azp ⇒ Object (readonly)
Returns the value of attribute azp.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def azp @azp end |
#client_roles ⇒ Object (readonly)
Returns the value of attribute client_roles.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def client_roles @client_roles end |
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def exp @exp end |
#jti ⇒ Object (readonly)
Returns the value of attribute jti.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def jti @jti end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def @metadata end |
#phone_number ⇒ Object (readonly)
Returns the value of attribute phone_number.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def phone_number @phone_number end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def roles @roles end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def scope @scope end |
#sub ⇒ Object (readonly)
Returns the value of attribute sub.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def sub @sub end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
3 4 5 |
# File 'lib/keycloak/access_token.rb', line 3 def username @username end |
Instance Method Details
#authorization ⇒ Object
29 30 31 |
# File 'lib/keycloak/access_token.rb', line 29 def "Bearer #{@access_token}" end |
#client_id ⇒ Object
25 26 27 |
# File 'lib/keycloak/access_token.rb', line 25 def client_id @azp end |
#expired? ⇒ Boolean
33 34 35 |
# File 'lib/keycloak/access_token.rb', line 33 def expired? exp < DateTime.now end |
#has_client_role?(role) ⇒ Boolean
41 42 43 |
# File 'lib/keycloak/access_token.rb', line 41 def has_client_role?(role) client_roles.include? role.to_s end |
#has_role?(role, include_client_role = true) ⇒ Boolean
37 38 39 |
# File 'lib/keycloak/access_token.rb', line 37 def has_role?(role, include_client_role = true) roles.include?(role.to_s) || (include_client_role && has_client_role?(role)) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
54 55 56 57 58 59 60 61 |
# File 'lib/keycloak/access_token.rb', line 54 def respond_to_missing?(name, include_private = false) regex = /^has_(.*?)_role\?$/ if name.match?(regex) return true end super end |