-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
envkey.go
70 lines (63 loc) · 2.16 KB
/
envkey.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
package sessionguard
import (
"encoding/json"
"strconv"
"strings"
"time"
"github.com/admpub/nging/v5/application/library/common"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
)
func EnvKey(ctx echo.Context) string {
return com.Md5(ctx.RealIP()+`|`+ctx.Request().UserAgent()) + `T` + strconv.FormatInt(time.Now().Unix(), 10)
}
func VerifyEnvKey(ctx echo.Context, envKey string, dontCheckTime ...bool) error {
pos := strings.LastIndex(envKey, `T`)
end := len(envKey) - 1
if pos < 1 || pos >= end {
return ctx.NewError(code.InvalidParameter, `凭证无效`).SetZone(`envKey`)
}
if len(dontCheckTime) == 0 || !dontCheckTime[0] {
ts, err := strconv.ParseInt(envKey[pos+1:], 10, 64)
if err != nil {
return ctx.NewError(code.DataFormatIncorrect, `凭证解析失败`).SetZone(`envKey`)
}
if time.Now().Unix()-ts > 300 {
return ctx.NewError(code.DataHasExpired, `凭证已经过期,请刷新页面后重新操作`).SetZone(`envKey`)
}
}
if com.Md5(ctx.RealIP()+`|`+ctx.Request().UserAgent()) != envKey[0:pos] {
return ctx.NewError(code.DataStatusIncorrect, `凭证来源不符合要求`).SetZone(`envKey`)
}
return nil
}
type PE struct {
Password string `json:"p"`
EnvKey string `json:"e"`
Timestamp int64 `json:"t"`
}
func (p *PE) Verify(ctx echo.Context) error {
sessionGuardCfg := common.ExtendConfig().Children(`sessionGuard`)
encryptedPasswordExpires := sessionGuardCfg.Int64(`encryptedPasswordExpires`)
if encryptedPasswordExpires <= 0 {
encryptedPasswordExpires = 300
}
//fmt.Println(`encryptedPasswordExpires~~~~~~~~~~~~~~~~~~~~~~~~~>`, encryptedPasswordExpires)
if time.Now().Unix()-p.Timestamp > encryptedPasswordExpires {
return ctx.NewError(code.DataHasExpired, `凭证已经失效`).SetZone(`envKey`)
}
return VerifyEnvKey(ctx, p.EnvKey, true)
}
func Unpack(ctx echo.Context, encrypted string) (password string, err error) {
if !strings.HasPrefix(encrypted, `{`) {
return encrypted, nil
}
pe := &PE{}
jerr := json.Unmarshal(com.Str2bytes(encrypted), pe)
if jerr != nil {
err = ctx.NewError(code.DataFormatIncorrect, `密码拆包失败`)
return
}
return pe.Password, pe.Verify(ctx)
}