-
Notifications
You must be signed in to change notification settings - Fork 7
/
state.go
81 lines (68 loc) · 2.15 KB
/
state.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
package goauth
import (
"fmt"
"time"
)
import (
"github.com/sanxia/glib"
)
/* ================================================================================
* Oauth State
* qq group: 582452342
* email : 2091938785@qq.com
* author : 美丽的地球啊 - mliu
* ================================================================================ */
type (
OauthState struct {
encryptKey string //密钥
minutes int //有效分钟数
}
)
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* 初始化OauthState
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
func NewOauthState(encryptKey string, minutes int) *OauthState {
return &OauthState{
encryptKey: encryptKey,
minutes: minutes,
}
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* 获取状态值
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
func (s *OauthState) GetState() string {
state := glib.ToBase64(glib.Guid())
expired := glib.ToBase64(fmt.Sprintf("%d", glib.DatetimeAddMinute(time.Now(), s.minutes).Unix()))
sign := glib.HmacSha256(fmt.Sprintf("%s.%s", state, expired), s.encryptKey)
return glib.ToBase64(fmt.Sprintf("%s.%d.%s", state, expired, sign), true)
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* 判断状态值是否有效
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
func (s *OauthState) IsValid(rawState string) bool {
if len(rawState) == 0 {
return false
}
state, err := glib.FromBase64(rawState, true)
if err != nil {
return false
}
states := glib.StringToStringSlice(state, ".")
if len(states) != 3 {
return false
}
//签名是否有效
if sign := glib.HmacSha256(fmt.Sprintf("%s.%s", states[0], states[1]), s.encryptKey); sign != states[3] {
return false
}
//是否过期
expired, err := glib.FromBase64(states[1])
if err != nil {
return false
}
expiredDate := glib.UnixTimestampToDate(glib.StringToInt64(expired))
if isExpired := time.Now().UTC().After(expiredDate); isExpired {
return false
}
return true
}