This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
token.go
95 lines (80 loc) · 2.28 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package wego
import (
jsoniter "github.com/json-iterator/go"
"strings"
"time"
"github.com/godcong/wego/util"
)
// Token represents the credentials used to authorize
// the requests to access protected resources on the OAuth 2.0
// provider's backend.
//
// This type is a mirror of oauth2.Token and exists to break
// an otherwise-circular dependency. Other internal packages
// should convert this Token into an oauth2.Token before use.
type Token struct {
// AccessToken is the accessToken that authorizes and authenticates
// the requests.
AccessToken string `json:"access_token"`
// RefreshToken is a accessToken that's used by the application
// (as opposed to the user) to refresh the access accessToken
// if it expires.
RefreshToken string `json:"refresh_token"`
// Expiry is the optional expiration time of the access accessToken.
//
// If zero, TokenSource implementations will reuse the same
// accessToken forever and RefreshToken or equivalent
// mechanisms for that TokenSource will not be used.
ExpiresIn int64 `json:"expires_in"`
// wechat openid
OpenID string `json:"openid"`
// wechat scope
Scope string `json:"scope"`
// Raw optionally contains extra metadata from the server
// when updating a accessToken.
Raw interface{}
}
/*KeyMap get accessToken's key,value with map */
func (t *Token) KeyMap() util.Map {
if t.AccessToken == "" {
return nil
}
return util.Map{
accessTokenKey: t.AccessToken,
}
}
/*SetExpiresIn set expires time */
func (t *Token) SetExpiresIn(ti time.Time) *Token {
t.ExpiresIn = ti.Unix()
return t
}
/*GetExpiresIn get expires time */
func (t *Token) GetExpiresIn() time.Time {
return time.Unix(t.ExpiresIn, 0)
}
/*GetScopes get accessToken scopes for get accessToken*/
func (t *Token) GetScopes() []string {
return strings.Split(t.Scope, ",")
}
/*SetScopes set accessToken scopes for get accessToken*/
func (t *Token) SetScopes(s []string) *Token {
strings.Join(s, ",")
return t
}
/*ToJSON transfer accessToken to json*/
func (t *Token) ToJSON() string {
s, e := jsoniter.MarshalToString(t)
if e != nil {
return ""
}
return s
}
/*ParseToken parse accessToken from string*/
func ParseToken(src string) (*Token, error) {
var t Token
e := jsoniter.UnmarshalFromString(src, &t)
if e != nil {
return nil, e
}
return &t, nil
}