class Keycloak::AccessToken

Attributes

access_token[R]
azp[R]
client_roles[R]
exp[R]
jti[R]
metadata[R]
phone_number[R]
roles[R]
scope[R]
sub[R]
username[R]

Public Class Methods

new(realm, access_token, decoded_token, client_id = nil) click to toggle source
# 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

Public Instance Methods

authorization() click to toggle source
# File lib/keycloak/access_token.rb, line 29
def authorization
  "Bearer #{@access_token}"
end
client_id() click to toggle source
# File lib/keycloak/access_token.rb, line 25
def client_id
  @azp
end
expired?() click to toggle source
# File lib/keycloak/access_token.rb, line 33
def expired?
  exp < DateTime.now
end
has_client_role?(role) click to toggle source
# 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) click to toggle source
# 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
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# 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
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# 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