-
Notifications
You must be signed in to change notification settings - Fork 0
/
linktoken.go
47 lines (42 loc) · 1.24 KB
/
linktoken.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
package vcago
import (
"time"
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/google/uuid"
)
type (
//LinkToken is used for handling link with token
LinkToken struct {
ID string `json:"id" bson:"_id"`
Code string `json:"code" bson:"code"`
ExpiresAt int64 `json:"expires_at" bson:"expires_at"`
Scope string `json:"scope" bson:"scope"`
UserID string `json:"user_id" bson:"user_id"`
Modified vmod.Modified `json:"modified" bson:"modified"`
}
)
//NewLinkToken initial a Token with a 32bit random string Base64 encoded for Web handling. Set expired time max 1 month.
func NewLinkToken(expired time.Duration, userID string, scope string) (*LinkToken, error) {
code, err := RandomBase64(32)
if err != nil {
return nil, err
}
return &LinkToken{
ID: uuid.New().String(),
Code: code,
ExpiresAt: time.Now().Add(expired).Unix(),
Scope: scope,
UserID: userID,
Modified: vmod.NewModified(),
}, nil
}
//NewCode generate a new code for LinkTokens
func (l *LinkToken) NewCode(expired time.Duration) (*LinkToken, error) {
code, err := RandomBase64(32)
if err != nil {
return nil, err
}
l.Code = code
l.ExpiresAt = time.Now().Add(expired).Unix()
return l, nil
}