Class: WebAuthn::FakeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/webauthn/fake_client.rb

Constant Summary collapse

TYPES =
{ create: "webauthn.create", get: "webauthn.get" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin = fake_origin, token_binding: nil, authenticator: WebAuthn::FakeAuthenticator.new, encoding: WebAuthn.configuration.encoding) ⇒ FakeClient

Returns a new instance of FakeClient.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/webauthn/fake_client.rb', line 15

def initialize(
  origin = fake_origin,
  token_binding: nil,
  authenticator: WebAuthn::FakeAuthenticator.new,
  encoding: WebAuthn.configuration.encoding
)
  @origin = origin
  @token_binding = token_binding
  @authenticator = authenticator
  @encoding = encoding
end

Instance Attribute Details

#originObject (readonly)

Returns the value of attribute origin.



13
14
15
# File 'lib/webauthn/fake_client.rb', line 13

def origin
  @origin
end

#token_bindingObject (readonly)

Returns the value of attribute token_binding.



13
14
15
# File 'lib/webauthn/fake_client.rb', line 13

def token_binding
  @token_binding
end

Instance Method Details

#create(challenge: fake_challenge, rp_id: nil, user_present: true, user_verified: false, attested_credential_data: true) ⇒ Object



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
62
63
64
65
66
# File 'lib/webauthn/fake_client.rb', line 27

def create(
  challenge: fake_challenge,
  rp_id: nil,
  user_present: true,
  user_verified: false,
  attested_credential_data: true
)
  rp_id ||= URI.parse(origin).host

  client_data_json = data_json_for(:create, encoder.decode(challenge))
  client_data_hash = hashed(client_data_json)

  attestation_object = authenticator.make_credential(
    rp_id: rp_id,
    client_data_hash: client_data_hash,
    user_present: user_present,
    user_verified: user_verified,
    attested_credential_data: attested_credential_data
  )

  id =
    if attested_credential_data
      WebAuthn::AuthenticatorData
        .deserialize(CBOR.decode(attestation_object)["authData"])
        .attested_credential_data
        .id
    else
      "id-for-pk-without-attested-credential-data"
    end

  {
    "type" => "public-key",
    "id" => internal_encoder.encode(id),
    "rawId" => encoder.encode(id),
    "response" => {
      "attestationObject" => encoder.encode(attestation_object),
      "clientDataJSON" => encoder.encode(client_data_json)
    }
  }
end

#get(challenge: fake_challenge, rp_id: nil, user_present: true, user_verified: false, sign_count: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/webauthn/fake_client.rb', line 68

def get(challenge: fake_challenge, rp_id: nil, user_present: true, user_verified: false, sign_count: nil)
  rp_id ||= URI.parse(origin).host

  client_data_json = data_json_for(:get, encoder.decode(challenge))
  client_data_hash = hashed(client_data_json)

  assertion = authenticator.get_assertion(
    rp_id: rp_id,
    client_data_hash: client_data_hash,
    user_present: user_present,
    user_verified: user_verified,
    sign_count: sign_count,
  )

  {
    "type" => "public-key",
    "id" => internal_encoder.encode(assertion[:credential_id]),
    "rawId" => encoder.encode(assertion[:credential_id]),
    "response" => {
      "clientDataJSON" => encoder.encode(client_data_json),
      "authenticatorData" => encoder.encode(assertion[:authenticator_data]),
      "signature" => encoder.encode(assertion[:signature]),
      "userHandle" => nil
    }
  }
end