-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreassemble.go
74 lines (63 loc) · 1.82 KB
/
reassemble.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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package dnstxtjwt
import (
"strings"
)
// reassemble converts the TXT record from the list of encoded lines into
// the expected string of text that we all hope is a legit JWT. The format
// of the lines in the TXT is:
//
// 00:base64_encoded_JWT_chuck_0
// 01:base64_encoded_JWT_chuck_1
// nn:base64_encoded_JWT_chuck_nn
//
// Notes:
// - the index could start at 0 or 1, so accept either.
// - the lines get concatenated in order and all parts are needed
// - support over 100 lines if needed
// - each line can be 255 bytes long including the leading 3 characters
// - it doesn't really matter if we are missing something because the JWT
// won't compute and will be discarded.
func reassemble(lines []string) string {
parts := make(map[int]string, len(lines)+1)
// The value in the TXT record should be 1 (really 1, but make this tolerant
// of 0 based indexing)
parts[0] = ""
for _, line := range lines {
segments := strings.Split(line, ":")
if len(segments) != 2 {
continue // skip empty or otherwise malformed lines.
}
n := getIndexInt(segments[0])
if n < 0 {
continue // skip lines that don't have a valid index
}
txt := strings.TrimSpace(segments[1])
parts[n] = txt
}
// Since we're re-assembling a JWT that is validated later, do the best
// we can here, but don't be too strict.
var buf strings.Builder
for i := 0; i < len(parts); i++ {
val, found := parts[i]
if !found {
return ""
}
buf.WriteString(val)
}
return buf.String()
}
func getIndexInt(s string) int {
var n int
s = strings.TrimSpace(s)
for _, c := range s {
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
n = n*10 + int(c-'0')
default:
return -1
}
}
return n
}