| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/oficina/home/handoff" |
| 7 | ) |
| 8 | |
| 9 | type handoffFailureCode uint8 |
| 10 | |
| 11 | const ( |
| 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 | ) |
| 24 | |
| 25 | // Only this fixed vocabulary reaches logs; no request or error is accepted. |
| 26 | func (a *App) failAssertion(w http.ResponseWriter, r *http.Request, code handoffFailureCode) { |
| 27 | stage, reason := "assertion", "unknown" |
| 28 | switch code { |
| 29 | case authorizeAge: |
| 30 | stage, reason = "authorize", "invalid_age" |
| 31 | case authorizeRequest: |
| 32 | stage, reason = "authorize", "invalid_request" |
| 33 | case authorizeEncode: |
| 34 | stage, reason = "authorize", "intent_encode" |
| 35 | case authorizeSeal: |
| 36 | stage, reason = "authorize", "intent_seal" |
| 37 | case assertionQuery: |
| 38 | reason = "unexpected_query" |
| 39 | case assertionIntentInvalid: |
| 40 | reason = "invalid_intent" |
| 41 | case assertionConsumer: |
| 42 | reason = "unknown_consumer" |
| 43 | case assertionSession: |
| 44 | reason = "missing_session" |
| 45 | case assertionSubject: |
| 46 | reason = "subject_mismatch" |
| 47 | case assertionRandom: |
| 48 | reason = "random_id" |
| 49 | case assertionSign: |
| 50 | reason = "signing" |
| 51 | } |
| 52 | a.logger.Warn("home handoff failed", "stage", stage, "reason", reason) |
| 53 | a.renderHandoffFailure(w, r, http.StatusBadRequest) |
| 54 | } |
| 55 | |
| 56 | func (a *App) failDirectoryHandoff(w http.ResponseWriter, r *http.Request, status int) { |
| 57 | stage := "directory_unknown" |
| 58 | switch r.URL.Path { |
| 59 | case handoff.StartPath: |
| 60 | stage = "directory_start" |
| 61 | case handoff.CallbackPath: |
| 62 | stage = "directory_callback" |
| 63 | case handoff.FinishPath: |
| 64 | stage = "directory_finish" |
| 65 | } |
| 66 | reason := "refused" |
| 67 | if status == http.StatusServiceUnavailable { |
| 68 | reason = "unavailable" |
| 69 | } |
| 70 | a.logger.Warn("home handoff failed", "stage", stage, "reason", reason) |
| 71 | a.renderHandoffFailure(w, r, status) |
| 72 | } |
| 73 | |
| 74 | func (a *App) renderHandoffFailure(w http.ResponseWriter, r *http.Request, status int) { |
| 75 | w.Header().Set("Cache-Control", "no-store") |
| 76 | a.render(w, r, status, "handoff_failed.html", map[string]any{"HomeURL": a.origins.Directory}) |
| 77 | } |
| 78 | |