-
Notifications
You must be signed in to change notification settings - Fork 53
/
public_key_credential.rb
94 lines (74 loc) · 2.27 KB
/
public_key_credential.rb
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
62
63
64
65
66
67
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
94
# frozen_string_literal: true
require "webauthn/encoder"
module WebAuthn
class PublicKeyCredential
class InvalidChallengeError < Error; end
attr_reader :type, :id, :raw_id, :client_extension_outputs, :authenticator_attachment, :response
def self.from_client(credential, relying_party: WebAuthn.configuration.relying_party)
new(
type: credential["type"],
id: credential["id"],
raw_id: relying_party.encoder.decode(credential["rawId"]),
client_extension_outputs: credential["clientExtensionResults"],
authenticator_attachment: credential["authenticatorAttachment"],
response: response_class.from_client(credential["response"], relying_party: relying_party),
relying_party: relying_party
)
end
def initialize(
type:,
id:,
raw_id:,
response:,
authenticator_attachment: nil,
client_extension_outputs: {},
relying_party: WebAuthn.configuration.relying_party
)
@type = type
@id = id
@raw_id = raw_id
@client_extension_outputs = client_extension_outputs
@authenticator_attachment = authenticator_attachment
@response = response
@relying_party = relying_party
end
def verify(challenge, *_args)
unless valid_class?(challenge)
msg = "challenge must be a String. input challenge class: #{challenge.class}"
raise(InvalidChallengeError, msg)
end
valid_type? || raise("invalid type")
valid_id? || raise("invalid id")
true
end
def sign_count
authenticator_data&.sign_count
end
def authenticator_extension_outputs
authenticator_data.extension_data if authenticator_data&.extension_data_included?
end
def backup_eligible?
authenticator_data&.credential_backup_eligible?
end
def backed_up?
authenticator_data&.credential_backed_up?
end
private
attr_reader :relying_party
def valid_type?
type == TYPE_PUBLIC_KEY
end
def valid_id?
raw_id && id && raw_id == WebAuthn.standard_encoder.decode(id)
end
def valid_class?(challenge)
challenge.is_a?(String)
end
def authenticator_data
response&.authenticator_data
end
def encoder
relying_party.encoder
end
end
end