-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_token.go
53 lines (41 loc) · 1.45 KB
/
default_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
51
52
53
package oauth2
import (
"time"
"github.com/dgrijalva/jwt-go"
"gopkg.in/mgo.v2/bson"
)
// DefaultToken describes a mongodb Token.
type DefaultToken struct {
ID bson.ObjectId `bson:"_id"`
User bson.ObjectId `bson:"user_id,omitempty"`
Client bson.ObjectId `bson:"client_id,omitempty"`
Created time.Time `bson:"created_time,omitempty"`
Expired time.Time `bson:"expired_time,omitempty"`
}
// ClientID returns client_id.
func (t *DefaultToken) ClientID() string { return t.Client.Hex() }
// UserID returns user_id.
func (t *DefaultToken) UserID() string { return t.User.Hex() }
// Token returns token.
func (t *DefaultToken) Token() string {
token := jwt.New(jwt.SigningMethodRS256)
// Set some claims
createdTime, _ := t.Created.MarshalText()
expiredTime, _ := t.Expired.MarshalText()
token.Claims = jwt.MapClaims{
"_id": t.ID.Hex(),
"user_id": t.User.Hex(),
"client_id": t.Client.Hex(),
"created_time": string(createdTime),
"expired_time": string(expiredTime),
}
// Generate token
tokenString, _ := token.SignedString(privateKey)
return tokenString
}
// IsExpired validate if this token is expired or not.
func (t *DefaultToken) IsExpired() bool { return time.Now().UTC().Unix() >= t.Expired.Unix() }
// CreatedTime returns created_time.
func (t *DefaultToken) CreatedTime() time.Time { return t.Created }
// ExpiredTime returns expired_time.
func (t *DefaultToken) ExpiredTime() time.Time { return t.Expired }