-
Notifications
You must be signed in to change notification settings - Fork 3
/
service_exports.go
86 lines (76 loc) · 1.79 KB
/
service_exports.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
package authb
import (
"errors"
"github.com/nats-io/jwt/v2"
)
type serviceExports struct {
*AccountData
}
func (s *serviceExports) Get(subject string) (ServiceExport, error) {
se := s.getServiceExport(subject)
if se == nil {
return nil, ErrNotFound
}
return se, nil
}
func (s *serviceExports) GetByName(name string) (ServiceExport, error) {
for _, e := range s.Claim.Exports {
if e.IsService() && e.Name == name {
se := &ServiceExportImpl{}
se.data = s.AccountData
se.export = e
return se, nil
}
}
return nil, ErrNotFound
}
func (s *serviceExports) List() []ServiceExport {
return s.getServiceExports()
}
func (s *serviceExports) AddWithConfig(e ServiceExport) error {
if e == nil {
return errors.New("invalid service export")
}
be, ok := e.(*ServiceExportImpl)
if !ok {
return errors.New("invalid service export")
}
if err := s.addExport(be.export); err != nil {
return err
}
return s.update()
}
func (s *serviceExports) Add(name string, subject string) (ServiceExport, error) {
err := s.newExport(name, subject, jwt.Service)
if err != nil {
return nil, err
}
// the pointer in the claim is changed by update, so we need to find it again
x := s.getServiceExport(subject)
if x == nil {
return nil, errors.New("could not find service export")
}
if err := x.update(); err != nil {
return nil, err
}
return x, nil
}
func (s *serviceExports) Set(exports ...ServiceExport) error {
var buf []*jwt.Export
// save existing streamExports
for _, e := range s.Claim.Exports {
if e.IsStream() {
buf = append(buf, e)
}
}
s.Claim.Exports = buf
for _, e := range exports {
if err := s.AddWithConfig(e); err != nil {
return err
}
}
return s.update()
}
func (s *serviceExports) Delete(subject string) (bool, error) {
return s.deleteExport(subject, true)
}