forked from h2oai/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.go
215 lines (195 loc) · 5.55 KB
/
web_server.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2020 H2O.ai, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wave
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"path"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
)
// WebServer represents a web server (d'oh).
type WebServer struct {
site *Site
broker *Broker
fs http.Handler
users map[string][]byte
}
const (
contentTypeJSON = "application/json"
)
func newWebServer(
site *Site,
broker *Broker,
users map[string][]byte,
oidcEnabled bool,
sessions *OIDCSessions,
oauth2Config oauth2.Config,
www string,
) *WebServer {
fs := fallback("/", http.FileServer(http.Dir(www)))
if oidcEnabled {
fs = checkSession(oauth2Config, sessions, fs)
}
return &WebServer{site, broker, fs, users}
}
func (s *WebServer) authenticate(username, password string) bool {
hash, ok := s.users[username]
if !ok {
return false
}
err := bcrypt.CompareHashAndPassword(hash, []byte(password))
return err == nil
}
func (s *WebServer) guard(w http.ResponseWriter, r *http.Request) bool {
username, password, ok := r.BasicAuth()
if !ok || !s.authenticate(username, password) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return false
}
return true
}
func (s *WebServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPatch: // writes
if !s.guard(w, r) {
return
}
s.patch(w, r)
case http.MethodGet: // reads
switch r.Header.Get("Content-Type") {
case contentTypeJSON: // data
s.get(w, r)
default: // template
s.fs.ServeHTTP(w, r)
}
case http.MethodPost: // all other APIs
if !s.guard(w, r) {
return
}
s.post(w, r)
// TODO case http.MethodPut: // file uploads
default:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
}
func (s *WebServer) patch(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body) // XXX add limit
if err != nil {
echo(Log{"t": "read patch request body", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
s.broker.patch(r.URL.Path, data)
}
func (s *WebServer) get(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
page := s.site.at(url)
if page == nil {
echo(Log{"t": "page_not_found", "url": url})
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
data := page.marshal()
if data == nil {
echo(Log{"t": "cache_miss", "url": url})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", contentTypeJSON)
w.Write(data)
}
func (s *WebServer) post(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Content-Type") {
case contentTypeJSON: // data
var req AppRequest
b, err := ioutil.ReadAll(r.Body) // XXX add limit
if err != nil {
echo(Log{"t": "read post request body", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := json.Unmarshal(b, &req); err != nil {
echo(Log{"t": "json_unmarshal", "error": err.Error()})
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if req.RegisterApp != nil {
q := req.RegisterApp
s.broker.addApp(q.Mode, q.Route, q.Address)
} else if req.UnregisterApp != nil {
q := req.UnregisterApp
s.broker.dropApp(q.Route)
}
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}
func checkSession(oauth2Config oauth2.Config, sessions *OIDCSessions, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(path.Ext(r.URL.Path)) > 0 || r.URL.Path == "/_login" {
h.ServeHTTP(w, r)
return
}
u, _ := url.Parse("/_login")
q := u.Query()
q.Set("next", r.URL.Path)
u.RawQuery = q.Encode()
cookie, err := r.Cookie(oidcSessionKey)
if err != nil {
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
sessionID := cookie.Value
session, ok := sessions.get(sessionID)
if !ok {
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
t, err := ensureValidOidcToken(r.Context(), oauth2Config, session.token)
if err != nil {
echo(Log{"t": "access_token_refresh", "error": err.Error()})
http.Redirect(w, r, u.String(), http.StatusFound)
return
}
if session.token != t {
session.token = t
sessions.set(sessionID, session)
}
h.ServeHTTP(w, r)
})
}
func fallback(prefix string, h http.Handler) http.Handler {
// copy of http.StripPrefix
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if the url has an extension, serve the file
if len(path.Ext(r.URL.Path)) > 0 {
h.ServeHTTP(w, r)
return
}
// rewrite
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = prefix
h.ServeHTTP(w, r2)
})
}
func ensureValidOidcToken(c context.Context, cfg oauth2.Config, t *oauth2.Token) (*oauth2.Token, error) {
return cfg.TokenSource(c, t).Token()
}