-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
167 lines (153 loc) · 4.71 KB
/
cookie.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cookie
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"unicode/utf8"
// "github.com/rdeusser/oauth2-proxy/pkg/structs"
"github.com/rdeusser/oauth2-proxy/pkg/cfg"
"github.com/rdeusser/oauth2-proxy/pkg/domains"
)
const maxCookieSize = 4000
var log = cfg.Cfg.Logger
// SetCookie http
func SetCookie(w http.ResponseWriter, r *http.Request, val string) {
setCookie(w, r, val, cfg.Cfg.Cookie.MaxAge*60) // convert minutes to seconds
}
func setCookie(w http.ResponseWriter, r *http.Request, val string, maxAge int) {
cookieName := cfg.Cfg.Cookie.Name
// foreach domain
domain := domains.Matches(r.Host)
// Allow overriding the cookie domain in the config file
if cfg.Cfg.Cookie.Domain != "" {
domain = cfg.Cfg.Cookie.Domain
log.Debugf("setting the cookie domain to %v", domain)
}
cookie := http.Cookie{
Name: cfg.Cfg.Cookie.Name,
Value: val,
Path: "/",
Domain: domain,
MaxAge: maxAge,
Secure: cfg.Cfg.Cookie.Secure,
HttpOnly: cfg.Cfg.Cookie.HTTPOnly,
}
cookieSize := len(cookie.String())
cookie.Value = ""
emptyCookieSize := len(cookie.String())
// Cookies have a max size of 4096 bytes, but to support most browsers, we should stay below 4000 bytes
// https://tools.ietf.org/html/rfc6265#section-6.1
// http://browsercookielimits.squawky.net/
if cookieSize > maxCookieSize {
// https://www.lifewire.com/cookie-limit-per-domain-3466809
log.Warnf("cookie size: %d. cookie sizes over ~4093 bytes(depending on the browser and platform) have shown to cause issues or simply aren't supported.", cookieSize)
cookieParts := SplitCookie(val, maxCookieSize-emptyCookieSize)
for i, cookiePart := range cookieParts {
// Cookies are named 1of3, 2of3, 3of3
cookieName = fmt.Sprintf("%s_%dof%d", cfg.Cfg.Cookie.Name, i+1, len(cookieParts))
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: cookiePart,
Path: "/",
Domain: domain,
MaxAge: maxAge,
Secure: cfg.Cfg.Cookie.Secure,
HttpOnly: cfg.Cfg.Cookie.HTTPOnly,
})
}
} else {
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: val,
Path: "/",
Domain: domain,
MaxAge: maxAge,
Secure: cfg.Cfg.Cookie.Secure,
HttpOnly: cfg.Cfg.Cookie.HTTPOnly,
})
}
}
// Cookie get the oauth2 jwt cookie
func Cookie(r *http.Request) (string, error) {
var cookieParts []string
var numParts = -1
var err error
cookies := r.Cookies()
// Get the remaining parts
// search for cookie parts in order
// this is the hotpath so we're trying to only walk once
for _, cookie := range cookies {
if cookie.Name == cfg.Cfg.Cookie.Name {
return cookie.Value, nil
}
if strings.HasPrefix(cookie.Name, fmt.Sprintf("%s_", cfg.Cfg.Cookie.Name)) {
log.Debugw("cookie",
"cookieName", cookie.Name,
"cookieValue", cookie.Value,
)
xOFy := strings.Split(cookie.Name, "_")[1]
xyArray := strings.Split(xOFy, "of")
if numParts == -1 { // then its uninitialized
if numParts, err = strconv.Atoi(xyArray[1]); err != nil {
return "", fmt.Errorf("multipart cookie fail: %s", err)
}
log.Debugf("make cookieParts of size %d", numParts)
cookieParts = make([]string, numParts)
}
var i int
if i, err = strconv.Atoi(xyArray[0]); err != nil {
return "", fmt.Errorf("multipart cookie fail: %s", err)
}
cookieParts[i-1] = cookie.Value
}
}
// combinedCookieStr := combinedCookie.String()
combinedCookieStr := strings.Join(cookieParts, "")
if combinedCookieStr == "" {
return "", errors.New("Cookie token empty")
}
log.Debugw("combined cookie",
"cookieValue", combinedCookieStr,
)
return combinedCookieStr, err
}
// ClearCookie get rid of the existing cookie
func ClearCookie(w http.ResponseWriter, r *http.Request) {
cookies := r.Cookies()
domain := domains.Matches(r.Host)
// Allow overriding the cookie domain in the config file
if cfg.Cfg.Cookie.Domain != "" {
domain = cfg.Cfg.Cookie.Domain
log.Debugf("setting the cookie domain to %v", domain)
}
// search for cookie parts
for _, cookie := range cookies {
if strings.HasPrefix(cookie.Name, cfg.Cfg.Cookie.Name) {
log.Debugf("deleting cookie: %s", cookie.Name)
http.SetCookie(w, &http.Cookie{
Name: cookie.Name,
Value: "delete",
Path: "/",
Domain: domain,
MaxAge: -1,
Secure: cfg.Cfg.Cookie.Secure,
HttpOnly: cfg.Cfg.Cookie.HTTPOnly,
})
}
}
}
// SplitCookie separate string into several strings of specified length
func SplitCookie(longString string, maxLen int) []string {
splits := []string{}
var l, r int
for l, r = 0, maxLen; r < len(longString); l, r = r, r+maxLen {
for !utf8.RuneStart(longString[r]) {
r--
}
splits = append(splits, longString[l:r])
}
splits = append(splits, longString[l:])
return splits
}