-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenopts.go
70 lines (63 loc) · 1.66 KB
/
tokenopts.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
package prototokens
import (
"fmt"
tokenpb "github.com/lusis/prototokens/proto/gen/go/prototokens/v1"
)
// TokenOpt is an option for creating a token
type TokenOpt func(*tokenpb.ProtoToken) error
// WithID provides a custom id for the new token
func WithID(id string) TokenOpt {
return func(pt *tokenpb.ProtoToken) error {
if id == "" {
return fmt.Errorf("id cannot be empty")
}
// this shouldn't happen since these are only used by New
// but might as well prevent a footgun
if pt.GetId() != "" {
return fmt.Errorf("%w: id", ErrOverwrite)
}
pt.Id = id
return nil
}
}
// WithUsages sets the valid usages for a token
func WithUsages(usages ...tokenpb.TokenUsages) TokenOpt {
return func(pt *tokenpb.ProtoToken) error {
if len(usages) == 0 {
return fmt.Errorf("at least one usage must be provided")
}
if pt.GetUsages() != nil {
return fmt.Errorf("%w: usages", ErrOverwrite)
}
pt.Usages = usages
return nil
}
}
// WithSID provides a custom sid for the new token
func WithSID(sid string) TokenOpt {
return func(pt *tokenpb.ProtoToken) error {
if sid == "" {
return fmt.Errorf("%w: sid", ErrOverwrite)
}
// this shouldn't happen since these are only used by New
// but might as well prevent a footgun
if pt.GetSid() != "" {
return fmt.Errorf("%w: sid", ErrOverwrite)
}
pt.Sid = sid
return nil
}
}
// WithVendor populates the vendor field for the new token
func WithVendor(data []byte) TokenOpt {
return func(pt *tokenpb.ProtoToken) error {
if data == nil {
return fmt.Errorf("data cannot be empty")
}
if pt.GetVendor() != nil {
return fmt.Errorf("%w: vendor", ErrOverwrite)
}
pt.Vendor = data
return nil
}
}