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
# File 'lib/keycloak-api-rails/service.rb', line 4

def initialize(key_resolver)
  @key_resolver                          = key_resolver
  @skip_paths                            = Keycloak.config.skip_paths
  @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



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

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_authentication?(method, path, headers) ⇒ Boolean

Returns:

  • (Boolean)


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

def need_authentication?(method, path, headers)
  !should_skip?(method, path) && !is_preflight?(method, headers)
end

#read_token(uri, headers) ⇒ Object



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

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