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.



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

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.



28
29
30
# File 'lib/keycloak/realm.rb', line 28

def auth_server_url
  @auth_server_url
end

#realmObject

Returns the value of attribute realm.



28
29
30
# File 'lib/keycloak/realm.rb', line 28

def realm
  @realm
end

Class Method Details

.register(&block) ⇒ Object



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

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



49
50
51
# File 'lib/keycloak/realm.rb', line 49

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

#nameObject



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

def name
  realm
end

#parse_access_token(access_token, client_id:) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/keycloak/realm.rb', line 41

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