-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototokens.go
38 lines (33 loc) · 967 Bytes
/
prototokens.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
// Package prototokens contains code for working with [tokenpb.ProtoToken]
package prototokens
import (
"time"
tokenpb "github.com/lusis/prototokens/proto/gen/go/prototokens/v1"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/segmentio/ksuid"
)
// New returns a new prototoken valid for the provided duration
func New(duration time.Duration, opts ...TokenOpt) (*tokenpb.ProtoToken, error) {
if duration == 0 {
// no you can't shoot yourself in the foot sorry
// provide a really long duration if you want a long-lived token
panic("duration MUST be provided")
}
nvb := time.Now().UTC()
nva := nvb.Add(duration)
tok := &tokenpb.ProtoToken{
Timestamps: &tokenpb.Timestamps{
NotValidBefore: timestamppb.New(nvb),
NotValidAfter: timestamppb.New(nva),
},
}
for _, opt := range opts {
if err := opt(tok); err != nil {
return nil, err
}
}
if tok.GetId() == "" {
tok.Id = ksuid.New().String()
}
return tok, nil
}