-
Notifications
You must be signed in to change notification settings - Fork 0
/
protector.go
48 lines (40 loc) · 872 Bytes
/
protector.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
package open
import (
"crypto/rand"
"encoding/base64"
"net/http"
)
type Protector interface {
}
func RedirectToLastLocation(w http.ResponseWriter, r *http.Request) {
location, err := r.Cookie("location")
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
http.Redirect(w, r, location.Value, http.StatusFound)
}
func generateStateOauthCookie(w http.ResponseWriter) string {
b := make([]byte, 16)
rand.Read(b)
state := base64.URLEncoding.EncodeToString(b)
cookie := http.Cookie{
Name: "oauthstate",
Value: state,
MaxAge: 0,
Path: "/",
HttpOnly: true,
}
http.SetCookie(w, &cookie)
return state
}
func setLastLocationCookie(w http.ResponseWriter, url string) {
http.SetCookie(w, &http.Cookie{
Name: "location",
Value: url,
MaxAge: 0,
Secure: false,
HttpOnly: true,
Path: "/",
})
}