-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
98 lines (83 loc) · 2.63 KB
/
handlers.go
File metadata and controls
98 lines (83 loc) · 2.63 KB
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
package main
import (
"encoding/base64"
"errors"
"log/slog"
"net/http"
"strings"
)
func routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /healthz", handleHealth)
mux.HandleFunc("GET /login", handleLogin)
mux.HandleFunc("POST /auth", handleAuth)
return mux
}
func handleHealth(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok\n"))
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
respondError(w, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
if svc := r.URL.Query().Get("svc"); svc != service.ID {
respondError(w, http.StatusBadRequest, "Bad Request\nUnknown service")
return
}
http.Redirect(w, r, buildAuthorizationURL(service.ID), http.StatusFound)
}
func handleAuth(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondError(w, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
if err := r.ParseForm(); err != nil {
respondError(w, http.StatusBadRequest, "Bad Request\nMalformed form")
return
}
if extErr := r.PostFormValue("error"); extErr != "" {
desc := r.PostFormValue("error_description")
slog.Warn("oidc provider returned error",
slog.String("error", extErr),
slog.String("error_description", desc),
)
respondError(w, http.StatusBadRequest, "Bad Request\nExternal error")
return
}
idToken := r.PostFormValue("id_token")
if idToken == "" {
respondError(w, http.StatusBadRequest, "Bad Request\nID token expected but not found")
return
}
claims, err := verifyIDToken(r.Context(), idToken)
if err != nil {
if tokenErr, ok := errors.AsType[*tokenValidationError](err); ok {
respondError(w, http.StatusBadRequest, "Bad Request\n"+tokenErr.PublicMessage)
return
}
slog.Error("token verification failed", slog.Any("error", err))
respondError(w, http.StatusBadRequest, "Bad Request\nInvalid JWT")
return
}
userID, err := userIDFromEmail(claims.Email)
if err != nil {
respondError(w, http.StatusUnauthorized, "Unauthorized\n"+err.Error())
return
}
wire, ticketID, err := issueTicket(service, userID, "OIDC")
if err != nil {
slog.Error("ticket issuance failed", slog.Any("error", err))
respondError(w, http.StatusInternalServerError, "Internal Server Error\nCannot issue ticket")
return
}
slog.Info("ticket issued",
slog.String("service_id", service.ID),
slog.String("user_id", userID),
slog.String("email", strings.ToLower(claims.Email)),
slog.String("ticket_id", ticketID),
)
writeContinuePage(w, service, base64.StdEncoding.EncodeToString(wire))
}