File: client_data.rb

package info (click to toggle)
ruby-webauthn 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 384 kB
  • sloc: ruby: 2,138; sh: 4; makefile: 4
file content (61 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require "json"
require "openssl"
require "webauthn/encoder"
require "webauthn/error"

module WebAuthn
  class ClientDataMissingError < Error; end

  class ClientData
    VALID_TOKEN_BINDING_STATUSES = ["present", "supported", "not-supported"].freeze

    def initialize(client_data_json)
      @client_data_json = client_data_json
    end

    def type
      data["type"]
    end

    def challenge
      WebAuthn.standard_encoder.decode(data["challenge"])
    end

    def origin
      data["origin"]
    end

    def token_binding
      data["tokenBinding"]
    end

    def valid_token_binding_format?
      if token_binding
        token_binding.is_a?(Hash) && VALID_TOKEN_BINDING_STATUSES.include?(token_binding["status"])
      else
        true
      end
    end

    def hash
      OpenSSL::Digest::SHA256.digest(client_data_json)
    end

    private

    attr_reader :client_data_json

    def data
      @data ||=
        begin
          if client_data_json
            JSON.parse(client_data_json)
          else
            raise ClientDataMissingError, "Client Data JSON is missing"
          end
        end
    end
  end
end