-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathjaas.go
58 lines (51 loc) · 1.39 KB
/
jaas.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
package config
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
)
var (
regexUsername = regexp.MustCompile(`(?m:username[[:blank:]]*=[[:blank:]]*"(.*?)")`)
regexPassword = regexp.MustCompile(`(?m:password[[:blank:]]*=[[:blank:]]*"(.*?)")`)
)
type JaasCredentials struct {
Username string
Password string
}
func NewJaasCredentialFromFile(filename string) (*JaasCredentials, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return NewJaasCredentials(string(bytes))
}
func NewJaasCredentials(s string) (*JaasCredentials, error) {
username, err := getJaasAttr(regexUsername.FindAllStringSubmatch(s, -1))
if err != nil {
return nil, fmt.Errorf("cannot retrieve jaas username: %s", err.Error())
}
password, err := getJaasAttr(regexPassword.FindAllStringSubmatch(s, -1))
if err != nil {
return nil, fmt.Errorf("cannot retrieve jaas password: %s", err.Error())
}
return &JaasCredentials{Username: username, Password: password}, nil
}
func getJaasAttr(submatch [][]string) (string, error) {
if len(submatch) == 0 {
return "", errors.New("no entry was found")
}
if len(submatch) != 1 {
return "", errors.New("multiple entries were found")
}
group := submatch[0]
if len(group) != 2 {
return "", errors.New("multiple entries were found")
}
attr := group[1]
if attr == "" {
return "", errors.New("attribute is empty")
}
return strings.TrimSpace(attr), nil
}