/
handler_users_firsttimeadd.go
49 lines (43 loc) · 1.16 KB
/
handler_users_firsttimeadd.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
package http
import (
"net/http"
)
func (srv *HttpServer) firstTimeAddUserHandler(
w http.ResponseWriter,
r *http.Request,
) {
if len(srv.users) > 0 {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
r.ParseMultipartForm(
4 * 1024 * 1024,
) // 4MB should suffice even the most ridiculously big certificates :)
file, _, err := r.FormFile("firstTimeCert")
if err != nil {
http.Error(w, "File missing or invalid", http.StatusBadRequest)
return
}
err = srv.addUser(w, r, file)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
srv.ReloadCerts()
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(200)
_, err = w.Write([]byte(`
<html>
<head>
<title>Authorize for CBDC-Test Controller</title>
</head>
<body>
<h1>Success!</h1>
<p>Once you have imported the certificate you authorized in your browser, you can access the test controller <a href="` + srv.GetHttpsEndpoint("https", srv.httpsPort, r) + `">here</a></p>
</body>
</html>`))
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}