-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_token.go
50 lines (45 loc) · 1.56 KB
/
access_token.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
package vcago
import (
"time"
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/golang-jwt/jwt"
)
//AccessToken represents the default access_token contains the basic user informations.
type AccessToken struct {
ID string `json:"id,omitempty" bson:"_id"`
Email string `json:"email" bson:"email" validate:"required,email"`
FirstName string `json:"first_name" validate:"required"`
LastName string `json:"last_name" validate:"required"`
FullName string `json:"full_name"`
DisplayName string `json:"display_name"`
Roles vmod.RoleListCookie `json:"system_roles"`
Country string `json:"country"`
PrivacyPolicy bool `json:"privacy_policy"`
Confirmed bool `json:"confirmed"`
LastUpdate string `json:"last_update"`
jwt.StandardClaims
}
//NewAccessToken creates an new access_token from vmod.User model.
func NewAccessToken(user *vmod.User) *AccessToken {
return &AccessToken{
user.ID,
user.Email,
user.FirstName,
user.LastName,
user.FullName,
user.DisplayName,
*user.Roles.Cookie(),
user.Country,
user.PrivacyPolicy,
user.Confirmd,
user.LastUpdate,
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * 15).Unix(),
},
}
}
//SignedString is used for Sign an accesstoken based on the secret param.
func (i *AccessToken) SignedString(secret string) (string, error) {
temp := jwt.NewWithClaims(jwt.SigningMethodHS256, i)
return temp.SignedString([]byte(secret))
}