-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
138 lines (119 loc) · 3.25 KB
/
helper.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package neko
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
// ClientIP returns more real IP address.
func (c *Context) ClientIP() string {
clientIP := c.Req.Header.Get("X-Real-IP")
if len(clientIP) == 0 {
clientIP = c.Req.Header.Get("X-Forwarded-For")
}
if len(clientIP) == 0 {
clientIP = c.Req.RemoteAddr
}
return clientIP
}
// SetCookie sets given cookie value to response header.
// ctx.SetCookie(name, value [, MaxAge, Path, Domain, Secure, HttpOnly])
func (c *Context) SetCookie(name, value string, others ...interface{}) {
cookie := &http.Cookie{}
cookie.Name = name
cookie.Value = value
if len(others) > 0 {
switch v := others[0].(type) {
case int:
cookie.MaxAge = v
case int64:
cookie.MaxAge = int(v)
case int32:
cookie.MaxAge = int(v)
}
}
// default "/"
if len(others) > 1 {
if v, ok := others[1].(string); ok && len(v) > 0 {
cookie.Path = v
}
} else {
cookie.Path = "/"
}
// default empty
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
cookie.Domain = v
}
}
// default empty
if len(others) > 3 {
switch v := others[3].(type) {
case bool:
cookie.Secure = v
}
}
// default false.
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
cookie.HttpOnly = true
}
}
http.SetCookie(c.Writer, cookie)
}
// GetCookie returns given cookie value from request header.
func (c *Context) GetCookie(name string) string {
cookie, err := c.Req.Cookie(name)
if err != nil {
return ""
}
return cookie.Value
}
var cookieSecret string
// SetCookieSecret sets global default secure cookie secret.
func (m *Engine) SetCookieSecret(secret string) {
cookieSecret = secret
}
// SetSecureCookie sets given cookie value to response header with default secret string.
func (ctx *Context) SetSecureCookie(name, value string, others ...interface{}) {
ctx.SetBasicSecureCookie(cookieSecret, name, value, others...)
}
// GetSecureCookie returns given cookie value from request header with default secret string.
func (ctx *Context) GetSecureCookie(name string) (string, bool) {
return ctx.GetBasicSecureCookie(cookieSecret, name)
}
// SetBasicSecureCookie sets given cookie value to response header with secret string.
func (ctx *Context) SetBasicSecureCookie(Secret, name, value string, others ...interface{}) {
vs := base64.URLEncoding.EncodeToString([]byte(value))
timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
hm := hmac.New(sha1.New, []byte(Secret))
fmt.Fprintf(hm, "%s%s", vs, timestamp)
sig := fmt.Sprintf("%02x", hm.Sum(nil))
cookie := strings.Join([]string{vs, timestamp, sig}, "|")
ctx.SetCookie(name, cookie, others...)
}
// GetBasicSecureCookie returns given cookie value from request header with secret string.
func (ctx *Context) GetBasicSecureCookie(Secret, name string) (string, bool) {
val := ctx.GetCookie(name)
if val == "" {
return "", false
}
parts := strings.SplitN(val, "|", 3)
if len(parts) != 3 {
return "", false
}
vs := parts[0]
timestamp := parts[1]
sig := parts[2]
hm := hmac.New(sha1.New, []byte(Secret))
fmt.Fprintf(hm, "%s%s", vs, timestamp)
if fmt.Sprintf("%02x", hm.Sum(nil)) != sig {
return "", false
}
res, _ := base64.URLEncoding.DecodeString(vs)
return string(res), true
}