-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
218 lines (190 loc) · 4.78 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package authb
import (
"encoding/json"
"fmt"
"reflect"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
)
type KeysFn func(p nkeys.PrefixByte) (*Key, error)
type Options struct {
SignFn jwt.SignFn
KeysFn KeysFn
}
type IssuingService interface {
Sign(c jwt.Claims, key *Key) (string, error)
NewKey(prefixByte nkeys.PrefixByte) (*Key, error)
}
type AuthImpl struct {
provider AuthProvider
operators []*OperatorData
opts *Options
}
func NewAuth(provider AuthProvider) (*AuthImpl, error) {
return NewAuthWithOptions(provider, nil)
}
func NewAuthWithOptions(provider AuthProvider, opts *Options) (*AuthImpl, error) {
if opts == nil {
opts = &Options{}
}
// initialize default key provider
if opts.KeysFn == nil {
opts.KeysFn = KeyFor
}
auth := &AuthImpl{provider: provider, opts: opts}
auth.provider = provider
operators, err := auth.provider.Load()
if err != nil {
return nil, err
}
auth.operators = operators
auth.initSigningService()
return auth, nil
}
func (a *AuthImpl) initSigningService() {
for _, op := range a.operators {
op.SigningService = a
}
}
func (a *AuthImpl) Sign(c jwt.Claims, key *Key) (string, error) {
kp := key.Pair
if a.opts.SignFn != nil {
var err error
kp, err = nkeys.FromPublicKey(key.Public)
if err != nil {
return "", err
}
}
return c.EncodeWithSigner(kp, a.opts.SignFn)
}
func (a *AuthImpl) NewKey(prefixByte nkeys.PrefixByte) (*Key, error) {
return a.opts.KeysFn(prefixByte)
}
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{SigningService: a.auth}
data.EntityName = name
data.Key, err = data.SigningService.NewKey(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{SigningService: a.auth}
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()
if err != nil {
return err
}
a.initSigningService()
return nil
}
func (b *BaseData) JWT() string {
return b.Token
}
func isNil(T any) bool {
return T == nil || reflect.ValueOf(T).IsNil()
}