-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
175 lines (151 loc) · 4.42 KB
/
hook.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
168
169
170
171
172
173
174
175
// +build go1.12
package main
import (
"context"
"fmt"
"net/http"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/metadata"
)
const (
sessionCookieName = "sessionid"
stateCookieName = "oidcstate"
HttpCookiePath = "/"
)
// cookieMapper is mapping an outgoing session to a session cookie
// This function is used on the login call
// Information processed here are generated by our own service
/*
func cookieMapper(ctx context.Context, w http.ResponseWriter, resp proto.Message) error {
_, ok := resp.(*Empty) // logout
if ok {
return nil
}
session, ok := resp.(*SessionData) // login we map.
if !ok {
return errors.New("Cannot cast to Session")
}
cookie := &http.Cookie{
Name: sessionCookieName,
Value: session.Id,
Path: HttpCookiePath,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
// XXX Create cookie the wrong way
//cookie := fmt.Sprintf(HttpCookieFormat, HttpCookieToken, session.Id)
// we must use Cookie creation API from net/http and set in the
// header
http.SetCookie(w, cookie)
// empty the content it's in the header.
//session.Id = ""
return nil
}
*/
// ON ERROR it does NOT go through here, so we cannot interfere
func cookieOrRedirectMapper(ctx context.Context, w http.ResponseWriter, resp proto.Message) error {
fmt.Printf("EXEC COOKIE BLA\n")
switch v := resp.(type) {
case *Empty:
/*
case *RedirectData:
w.Header().Add("Location", v.Url)
case *RedirectSessionData:
*/
case *SessionBackend:
var cookie *http.Cookie
sessioncookie := v.GetCookieSession()
url := v.GetUrl()
fmt.Printf("BACKEND sessionc: %s\n", sessioncookie)
// session id cookie
if len(sessioncookie) > 0 {
cookie = &http.Cookie{
Name: sessionCookieName,
Value: sessioncookie,
Path: HttpCookiePath,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
w.Header().Add("Location", url)
}
http.SetCookie(w, cookie)
return ErrRedirect
case *SessionIdp:
var cookie *http.Cookie
statecookie := v.GetCookieState()
cookiepath := v.GetCookiePath()
url := v.GetUrl()
fmt.Printf("OIDC SESSION statec: %s pathc: %s url: %s\n",
statecookie,
cookiepath,
url)
// session id cookie
if len(statecookie) > 0 && len(url) > 0 && len(cookiepath) > 0 { // this is the first redirect
cookie = &http.Cookie{
Name: stateCookieName,
Value: statecookie,
//Path: HttpCookiePath,
Path: cookiepath,
Secure: true,
HttpOnly: true,
// XXX super odd, but with chrome on strict mode
// the first request does NOT come with the
// cookie previously set.
//SameSite: http.SameSiteStrictMode,
SameSite: http.SameSiteLaxMode,
// XXX TODO: if you do that it becomes a permanent cookie and we WANT to remain just a session
//MaxAge: int((5 * time.Minute).Seconds()),
//Expires: time.Now().Add(5 * time.Minute),
// TODO
// need Expires
// need Domain
}
w.Header().Add("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
w.Header().Add("Location", url)
}
http.SetCookie(w, cookie)
return ErrRedirect
}
// XXX Create cookie the wrong way
//cookie := fmt.Sprintf(HttpCookieFormat, HttpCookieToken, session.Id)
// we must use Cookie creation API from net/http and set in the
// header
// empty the content it's in the header.
//session.Id = ""
return nil
}
func headerRemover(headerName string) (mdName string, ok bool) {
//header := strings.ToLower(headerName)
//headerLen := len(headerName)
// This fonction just prevent those headers out
// XXX we need to check if any header received on the gateway can tamper
// with grpc behavior
/*
if headerLen == 26 && strings.Compare(header, "grpc-metadata-content-type") == 0 {
return "", false
}
*/
/*
if headerLen == len(rpc.GrpcMdContentType) && strings.EqualFold(headerName, rpc.GrpcMdContentType) {
return "", false
}
*/
return headerName, true
}
func headerToMetadata(ctx context.Context, r *http.Request) metadata.MD {
mdmap := make(map[string]string)
// XXX needs input validation happening here..
// these data should be SHORT.
cookieSession, err := r.Cookie(sessionCookieName)
if err == nil {
mdmap[sessionCookieName] = cookieSession.Value
}
cookieState, err := r.Cookie(stateCookieName)
// 8k is more than enough..
if err == nil && len(cookieState.Value) < 8192 {
mdmap[stateCookieName] = cookieState.Value
}
return metadata.New(mdmap)
}