-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
67 lines (60 loc) · 1.34 KB
/
utils.go
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
package virtualwebauthn
import (
"crypto/rand"
"github.com/fxamacker/cbor/v2"
)
type clientData struct {
Type string `json:"type"`
Challenge string `json:"challenge"`
Origin string `json:"origin"`
}
func marshalCbor(v any) []byte {
encoder, err := cbor.CTAP2EncOptions().EncMode()
if err != nil {
panic("failed to instantiate cbor encoder")
}
bytes, err := encoder.Marshal(v)
if err != nil {
panic("failed to encode to cbor")
}
return bytes
}
func randomBytes(length int) []byte {
bytes := make([]byte, length)
num, err := rand.Read(bytes)
if err != nil || num != length {
panic("failed to generate random bytes")
}
return bytes
}
func bigEndianBytes[T interface{ int | uint32 }](value T, length int) []byte {
bytes := make([]byte, length)
for i := 0; i < length; i++ {
shift := (length - i - 1) * 8
bytes[i] = byte(value >> shift & 0xFF)
}
return bytes
}
func authenticatorDataFlags(userPresent, userVerified, backupEligible, backupState, attestation, extensions bool) byte {
// https://www.w3.org/TR/webauthn/#flags
flags := byte(0)
if userPresent {
flags |= 1 << 0
}
if userVerified {
flags |= 1 << 2
}
if backupEligible {
flags |= 1 << 3
}
if backupState {
flags |= 1 << 4
}
if attestation {
flags |= 1 << 6
}
if extensions { // extensions not supported yet
flags |= 1 << 7
}
return flags
}