-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.go
127 lines (108 loc) · 2.62 KB
/
misc.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
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
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
124
125
126
127
package webauthn
import (
"bytes"
"crypto"
"crypto/subtle"
"encoding/base64"
"encoding/binary"
"io"
"net/url"
"strings"
"github.com/fxamacker/cbor/v2"
)
func bytesAreEqual(x, y []byte) bool {
return subtle.ConstantTimeCompare(x, y) == 1
}
func concat(bss ...[]byte) []byte {
sz := 0
for _, bs := range bss {
sz += len(bs)
}
result := make([]byte, 0, sz)
for _, bs := range bss {
result = append(result, bs...)
}
return result
}
// extractCBOR splits a byte slice into two parts. The first contains CBOR data. The second the remaining bytes.
func extractCBOR(data []byte) (cborData, remaining []byte, err error) {
var m interface{}
decoder := cbor.NewDecoder(bytes.NewReader(data))
err = decoder.Decode(&m)
if err != nil {
return nil, nil, err
}
return data[:decoder.NumBytesRead()], data[decoder.NumBytesRead():], nil
}
func hashIsEqual(hash crypto.Hash, data []byte, expectedHashSum []byte) bool {
if !hash.Available() {
return false
}
h := hash.New()
h.Write(data)
return subtle.ConstantTimeCompare(h.Sum(nil), expectedHashSum) == 1
}
func originMatches(clientOrigin, relyingPartyOrigin string) bool {
clientOriginURL, err := url.Parse(clientOrigin)
if err != nil {
return false
}
relyingPartyOriginURL, err := url.Parse(relyingPartyOrigin)
if err != nil {
return false
}
clientHost := clientOriginURL.Hostname()
relyingPartyHost := relyingPartyOriginURL.Hostname()
for clientHost != "" {
if clientHost == relyingPartyHost {
return true
}
if idx := strings.Index(clientHost, "."); idx >= 0 {
clientHost = clientHost[idx+1:]
} else {
return false
}
}
return false
}
func stringsAreEqual(x, y string) bool {
return bytesAreEqual([]byte(x), []byte(y))
}
func write(w io.Writer, bs ...byte) error {
_, err := w.Write(bs)
return err
}
func writeUint16(w io.Writer, v uint16) error {
var buf [2]byte
binary.BigEndian.PutUint16(buf[:], v)
_, err := w.Write(buf[:])
return err
}
func writeUint32(w io.Writer, v uint32) error {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], v)
_, err := w.Write(buf[:])
return err
}
func fromBase64URL(encoded string) ([]byte, error) {
if encoded == "" {
return nil, nil
}
return base64.RawURLEncoding.DecodeString(encoded)
}
func toBase64URL(raw []byte) string {
return base64.RawURLEncoding.EncodeToString(raw)
}
func fromNullableBase64URL(encoded *string) ([]byte, error) {
if encoded == nil || *encoded == "" {
return nil, nil
}
return base64.RawURLEncoding.DecodeString(*encoded)
}
func toNullableBase64URL(raw []byte) *string {
if len(raw) == 0 {
return nil
}
encoded := base64.RawURLEncoding.EncodeToString(raw)
return &encoded
}