oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5
6 "amadan.net/oficina/home/handoff"
7)
8
9type handoffFailureCode uint8
10
11const (
12 authorizeAge handoffFailureCode = iota
13 authorizeRequest
14 authorizeEncode
15 authorizeSeal
16 assertionQuery
17 assertionIntentInvalid
18 assertionConsumer
19 assertionSession
20 assertionSubject
21 assertionRandom
22 assertionSign
23 assertionNotNavigation
24)
25
26// Only this fixed vocabulary reaches logs; no request or error is accepted.
27func (a *App) failAssertion(w http.ResponseWriter, r *http.Request, code handoffFailureCode) {
28 stage, reason := "assertion", "unknown"
29 switch code {
30 case authorizeAge:
31 stage, reason = "authorize", "invalid_age"
32 case authorizeRequest:
33 stage, reason = "authorize", "invalid_request"
34 case authorizeEncode:
35 stage, reason = "authorize", "intent_encode"
36 case authorizeSeal:
37 stage, reason = "authorize", "intent_seal"
38 case assertionQuery:
39 reason = "unexpected_query"
40 case assertionIntentInvalid:
41 reason = "invalid_intent"
42 case assertionConsumer:
43 reason = "unknown_consumer"
44 case assertionSession:
45 reason = "missing_session"
46 case assertionSubject:
47 reason = "subject_mismatch"
48 case assertionRandom:
49 reason = "random_id"
50 case assertionSign:
51 reason = "signing"
52 case assertionNotNavigation:
53 reason = "not_navigation"
54 }
55 a.logger.Warn("home handoff failed", "stage", stage, "reason", reason)
56 a.renderHandoffFailure(w, r, http.StatusBadRequest)
57}
58
59func (a *App) failDirectoryHandoff(w http.ResponseWriter, r *http.Request, status int) {
60 if signedOutAllFinish(w) {
61 // The assertion predates "Sign out all devices": say that, not
62 // "something went wrong".
63 w.Header().Set("Cache-Control", "no-store")
64 a.render(w, r, http.StatusForbidden, "directory_signed_out.html", map[string]any{"AllDevices": true, "Action": a.origins.Credential + signoutPath})
65 return
66 }
67 stage := "directory_unknown"
68 switch r.URL.Path {
69 case handoff.StartPath:
70 stage = "directory_start"
71 case freshStartPath:
72 stage = "directory_fresh_start"
73 case handoff.CallbackPath:
74 stage = "directory_callback"
75 case handoff.FinishPath:
76 stage = "directory_finish"
77 }
78 reason := "refused"
79 if status == http.StatusServiceUnavailable {
80 reason = "unavailable"
81 }
82 a.logger.Warn("home handoff failed", "stage", stage, "reason", reason)
83 a.renderHandoffFailure(w, r, status)
84}
85
86func (a *App) renderHandoffFailure(w http.ResponseWriter, r *http.Request, status int) {
87 w.Header().Set("Cache-Control", "no-store")
88 a.render(w, r, status, "handoff_failed.html", map[string]any{"HomeURL": a.origins.Directory})
89}
90