-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
48 lines (43 loc) · 1.26 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
package utils
import (
"fmt"
"regexp"
"strings"
)
// ValidateRequiredCredentials validates all required credentials are present
func ValidateRequiredCredentials(m map[string]string, keys []string) error {
var missing []string
for _, key := range keys {
if _, ok := m[key]; !ok {
missing = append(missing, key)
}
}
if len(missing) > 0 {
return fmt.Errorf("missing required credentials: %s", strings.Join(missing, ", "))
}
return nil
}
// FormatHexString adds the Ox prefix to the string if not present
func FormatHexString(toPad string) string {
if strings.HasPrefix(toPad, "0x") {
return toPad
}
return "0x" + toPad
}
// RemoveHexPrefix removes the Ox prefix from the string if present
func RemoveHexPrefix(value string) string {
if !strings.HasPrefix(value, "0x") {
return value
}
return value[2:]
}
// ParseTokensWithRegex retrieves the Address that generated the certificate
// and the public ephemeral key from the identity header
func ParseTokensWithRegex(idHeader string, pattern string) ([]string, error) {
idRegex := *regexp.MustCompile(pattern)
if !idRegex.MatchString(idHeader) {
return nil, fmt.Errorf("malformed 'x-identity' header: %s", idHeader)
}
matches := idRegex.FindAllStringSubmatch(idHeader, -1)
return matches[0][1:], nil
}