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

#encodingObject (readonly)

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#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, backup_eligibility: false, backup_state: false, attested_credential_data: true, credential_algorithm: nil, extensions: nil) ⇒ 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
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/webauthn/fake_client.rb', line 27

def create(
  challenge: fake_challenge,
  rp_id: nil,
  user_present: true,
  user_verified: false,
  backup_eligibility: false,
  backup_state: false,
  attested_credential_data: true,
  credential_algorithm: nil,
  extensions: nil
)
  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,
    backup_eligibility: backup_eligibility,
    backup_state: backup_state,
    attested_credential_data: attested_credential_data,
    algorithm: credential_algorithm,
    extensions: extensions
  )

  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),
    "authenticatorAttachment" => 'platform',
    "clientExtensionResults" => extensions,
    "response" => {
      "attestationObject" => encoder.encode(attestation_object),
      "clientDataJSON" => encoder.encode(client_data_json),
      "transports" => ["internal"],
    }
  }
end

#get(challenge: fake_challenge, rp_id: nil, user_present: true, user_verified: false, backup_eligibility: false, backup_state: true, sign_count: nil, extensions: nil, user_handle: nil, allow_credentials: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/webauthn/fake_client.rb', line 79

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

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

  if allow_credentials
    allow_credentials = allow_credentials.map { |credential| encoder.decode(credential) }
  end

  assertion = authenticator.get_assertion(
    rp_id: rp_id,
    client_data_hash: client_data_hash,
    user_present: user_present,
    user_verified: user_verified,
    backup_eligibility: backup_eligibility,
    backup_state: backup_state,
    sign_count: sign_count,
    extensions: extensions,
    allow_credentials: allow_credentials
  )

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