Class: Keycloak::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak-api-rails/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_resolver) ⇒ Service

Returns a new instance of Service.



4
5
6
7
8
9
10
# File 'lib/keycloak-api-rails/service.rb', line 4

def initialize(key_resolver)
  @key_resolver                          = key_resolver
  @skip_paths                            = Keycloak.config.skip_paths
  @opt_in                                = Keycloak.config.opt_in
  @logger                                = Keycloak.config.logger
  @token_expiration_tolerance_in_seconds = Keycloak.config.token_expiration_tolerance_in_seconds
end

Instance Method Details

#decode_and_verify(token) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/keycloak-api-rails/service.rb', line 12

def decode_and_verify(token)
  unless token.nil? || token&.empty?
    public_key    = @key_resolver.find_public_keys
    decoded_token = JSON::JWT.decode(token, public_key)

    unless expired?(decoded_token)
      decoded_token.verify!(public_key)
      decoded_token
    else
      raise TokenError.expired(token)
    end
  else
    raise TokenError.no_token(token)
  end
rescue JSON::JWT::VerificationFailed => e
  raise TokenError.verification_failed(token, e)
rescue JSON::JWK::Set::KidNotFound => e
  raise TokenError.verification_failed(token, e)
rescue JSON::JWT::InvalidFormat
  raise TokenError.invalid_format(token, e)
end

#need_middleware_authentication?(method, path, headers) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/keycloak-api-rails/service.rb', line 38

def need_middleware_authentication?(method, path, headers)
  !is_preflight?(method, headers) && (!@opt_in && !should_skip?(method, path))
end

#read_token(uri, headers) ⇒ Object



34
35
36
# File 'lib/keycloak-api-rails/service.rb', line 34

def read_token(uri, headers)
  Helper.read_token_from_query_string(uri) || Helper.read_token_from_headers(headers)
end