Class: RubySlime::JWTKit::EncryptedVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_slime/jwt_kit/encrypted_verifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(fetch_secret:, handle_result:, payload_schema:) ⇒ EncryptedVerifier

Returns a new instance of EncryptedVerifier.



6
7
8
9
10
11
12
13
14
# File 'lib/ruby_slime/jwt_kit/encrypted_verifier.rb', line 6

def initialize(
  fetch_secret:,
  handle_result:,
  payload_schema:
)
  @fetch_secret = fetch_secret
  @handle_result = handle_result
  @payload_schema = payload_schema
end

Instance Method Details

#call(token) ⇒ Object

Raises:

  • (::JWT::DecodeError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_slime/jwt_kit/encrypted_verifier.rb', line 16

def call(token)
  raise ::JWT::DecodeError, 'Invalid token format!' unless token.is_a?(::String)
  header = P_GenericCache.instance.header_extract.match(token)
  raise ::JWT::DecodeError, 'Invalid token format!' unless header
  header = header[1].then do |encoded|
    ::Base64.urlsafe_decode64(encoded)
  rescue ::ArgumentError
    raise ::JWT::DecodeError, 'Invalid token format!'
  end.then do |json_string|
    ::JSON.parse(json_string, symbolize_names: true)
  rescue ::TypeError, ::JSON::JSONError
    raise ::JWT::DecodeError, 'Invalid token format!'
  end
  checked_header = check_jwt_section(header, P_GenericCache.instance.header_schema)
  secret = @fetch_secret.call(checked_header)
  begin
    ::JWE.decrypt(token, secret)
  rescue ::ArgumentError
    # (invalid base64: jwe (0.4.0) lib/jwe/base64.rb:14:in `jwe_decode')
    raise ::JWT::DecodeError, 'Invalid token format!'
  end.then do |encoded|
    ::JSON.parse(encoded, symbolize_names: true)
  rescue ::TypeError, ::JSON::JSONError
    raise ::JWT::DecodeError, 'Invalid payload format!'
  end.then do |payload|
    check_jwt_section(payload, @payload_schema)
  end.then do |payload|
    @handle_result.call(payload, header)
  end
end