Class: Keycloak::Realm

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

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

ParseAccessTokenError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_server_url, realm) ⇒ Realm

Returns a new instance of Realm.



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

def initialize(auth_server_url, realm)
  @auth_server_url = auth_server_url
  @realm = realm
end

Instance Attribute Details

#auth_server_urlObject

Returns the value of attribute auth_server_url.



31
32
33
# File 'lib/keycloak/realm.rb', line 31

def auth_server_url
  @auth_server_url
end

#realmObject

Returns the value of attribute realm.



31
32
33
# File 'lib/keycloak/realm.rb', line 31

def realm
  @realm
end

Class Method Details

.register(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/keycloak/realm.rb', line 13

def register(&block)
  return unless block_given?

  cfg = Configuration.new
  block.call(cfg)
  if file = cfg.installation_file
    file_cfg = JSON.parse(File.read(file))
    realm_key = file_cfg['realm'].underscore.to_sym
    @realms[realm_key] = Realm.new(file_cfg['auth-server-url'], file_cfg['realm'])
  else
    realm_key = cfg.realm.underscore.to_sym
    @realms[realm_key] = Realm.new(cfg.auth_server_url, cfg.realm)
  end

  define_singleton_method(realm_key) { @realms[realm_key] }
end

Instance Method Details

#clientObject



52
53
54
# File 'lib/keycloak/realm.rb', line 52

def client
  @client ||= Client.new(auth_server_url, realm)
end

#nameObject



38
39
40
# File 'lib/keycloak/realm.rb', line 38

def name
  realm
end

#parse_access_token(access_token, client_id:) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/keycloak/realm.rb', line 44

def parse_access_token(access_token, client_id:)
  alg = JWT.decode(access_token, nil, false)[1]["alg"]
  decoded_token = JWT.decode access_token, public_keys[alg], true, algorithm: alg
  azp = decoded_token[0]["azp"]
  raise ParseAccessTokenError, "Unexpected client, expect #{client_id}, got #{azp}" if client_id && azp != client_id
  AccessToken.new self, access_token, decoded_token, client_id
end