Class: Keycloak::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak/access_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_tokenObject (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

#azpObject (readonly)

Returns the value of attribute azp.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def azp
  @azp
end

#client_rolesObject (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

#expObject (readonly)

Returns the value of attribute exp.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def exp
  @exp
end

#jtiObject (readonly)

Returns the value of attribute jti.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def jti
  @jti
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def 
  @metadata
end

#phone_numberObject (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

#rolesObject (readonly)

Returns the value of attribute roles.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def roles
  @roles
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def scope
  @scope
end

#subObject (readonly)

Returns the value of attribute sub.



3
4
5
# File 'lib/keycloak/access_token.rb', line 3

def sub
  @sub
end

#usernameObject (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

#authorizationObject



29
30
31
# File 'lib/keycloak/access_token.rb', line 29

def authorization
  "Bearer #{@access_token}"
end

#client_idObject



25
26
27
# File 'lib/keycloak/access_token.rb', line 25

def client_id
  @azp
end

#expired?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/keycloak/access_token.rb', line 33

def expired?
  exp < DateTime.now
end

#has_client_role?(role) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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