-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
166 lines (146 loc) · 3.65 KB
/
auth.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package authb
import (
"encoding/json"
"fmt"
"reflect"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
)
type AuthImpl struct {
provider AuthProvider
operators []*OperatorData
}
func NewAuth(provider AuthProvider) (*AuthImpl, error) {
auth := &AuthImpl{provider: provider}
auth.provider = provider
operators, err := auth.provider.Load()
if err != nil {
return nil, err
}
auth.operators = operators
return auth, nil
}
type OperatorsImpl struct {
auth *AuthImpl
}
func (a *AuthImpl) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Operators []*OperatorData `json:"operators"`
}{
Operators: a.operators,
})
}
func (a *AuthImpl) UnmarshalJSON(data []byte) error {
var v struct {
Operators []*OperatorData `json:"operators"`
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
a.operators = v.Operators
return nil
}
func (a *AuthImpl) Operators() Operators {
return &OperatorsImpl{auth: a}
}
func (a *OperatorsImpl) List() []Operator {
v := make([]Operator, len(a.auth.operators))
for i, o := range a.auth.operators {
v[i] = o
}
return v
}
func (a *OperatorsImpl) Get(name string) (Operator, error) {
for _, o := range a.auth.operators {
if o.EntityName == name || o.Subject() == name {
return o, nil
}
}
return nil, ErrNotFound
}
func (a *OperatorsImpl) Add(name string) (Operator, error) {
var err error
data := &OperatorData{}
data.EntityName = name
data.Key, err = KeyFor(nkeys.PrefixByteOperator)
if err != nil {
return nil, err
}
data.Claim = jwt.NewOperatorClaims(data.Key.Public)
data.Claim.Name = name
a.auth.operators = append(a.auth.operators, data)
if err := data.update(); err != nil {
return nil, err
}
return data, nil
}
func (a *OperatorsImpl) Delete(name string) error {
idx := -1
for i, op := range a.auth.operators {
if op.EntityName == name || op.Subject() == name {
idx = i
break
}
}
if idx != -1 {
a.auth.operators[idx] = a.auth.operators[len(a.auth.operators)-1]
a.auth.operators = a.auth.operators[:len(a.auth.operators)-1]
}
return nil
}
func (a *OperatorsImpl) Import(token []byte, keys []string) (Operator, error) {
claim, err := jwt.DecodeOperatorClaims(string(token))
if err != nil {
return nil, err
}
// we require all the keys? - new NGS will not allow you to
// edit configs via CLI, so this is not a problem?
m := make(map[string]*Key, len(keys))
for i, k := range keys {
key, err := KeyFrom(k, nkeys.PrefixByteOperator, nkeys.PrefixByteSeed)
if err != nil {
return nil, fmt.Errorf("invalid seed at %d: %w", i, err)
}
if key.Public != claim.Subject && !claim.SigningKeys.Contains(key.Public) {
return nil, fmt.Errorf("invalid seed %s: is not referenced by the operator", k)
}
m[key.Public] = key
}
if len(keys) != len(claim.SigningKeys)+1 {
return nil, fmt.Errorf("not all keys are provided: %d", len(keys))
}
var ok bool
data := &OperatorData{}
data.Claim = claim
data.EntityName = claim.Name
data.Key, ok = m[claim.Subject]
if !ok {
return nil, fmt.Errorf("%s was not provided", claim.Subject)
}
for _, k := range claim.SigningKeys {
key, ok := m[k]
if !ok {
return nil, fmt.Errorf("%s was not provided", k)
}
data.OperatorSigningKeys = append(data.OperatorSigningKeys, key)
}
a.auth.operators = append(a.auth.operators, data)
if err := data.update(); err != nil {
return nil, err
}
return data, nil
}
func (a *AuthImpl) Commit() error {
return a.provider.Store(a.operators)
}
func (a *AuthImpl) Reload() error {
var err error
a.operators, err = a.provider.Load()
return err
}
func (b *BaseData) JWT() string {
return b.Token
}
func isNil(T any) bool {
return T == nil || reflect.ValueOf(T).IsNil()
}