| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/oficina/estilo.git/brand" |
| 7 | "amadan.net/oficina/estilo.git/chrome" |
| 8 | "amadan.net/oficina/estilo.git/sky" |
| 9 | "amadan.net/rastrillo/rastrillo/auth" |
| 10 | ) |
| 11 | |
| 12 | // The sign-in door: estilo's chrome in every state, as in every sibling |
| 13 | // (README, "Sign-in door"). These handlers are thin because rastrillo's |
| 14 | // auth owns the flow. |
| 15 | |
| 16 | // brandIcon is Mapa's own mark, from estilo's brand set. |
| 17 | func brandIcon() []byte { return brand.SVG(themeSlug) } |
| 18 | |
| 19 | func (app *app) signin(writer http.ResponseWriter, request *http.Request) { |
| 20 | query := request.URL.Query() |
| 21 | head := doorHead(request) |
| 22 | if query.Get("sent") == "1" { |
| 23 | // The same page for a member and a stranger (beginGuard). |
| 24 | sent := head.Sent(signinPath) |
| 25 | render(writer, request, "signin", signinPage{Sent: &sent, |
| 26 | Theme: themeAttr(request)}) |
| 27 | return |
| 28 | } |
| 29 | form := chrome.SignIn{Icon: head.Icon, App: head.App, Sky: head.Sky, |
| 30 | Error: signinError(query.Get("err")), Action: "/auth/begin"} |
| 31 | // A failed keymail round trip comes back with force=1 so the next |
| 32 | // submission skips keymail and emails a link. |
| 33 | if query.Get("force") == "1" { |
| 34 | form.Hidden = []chrome.Hidden{{Name: "force", Value: "1"}} |
| 35 | } |
| 36 | render(writer, request, "signin", signinPage{Form: &form, |
| 37 | Theme: themeAttr(request)}) |
| 38 | } |
| 39 | |
| 40 | // signinPage is templates/signin.html's data: exactly one of its first |
| 41 | // four fields is set. |
| 42 | type signinPage struct { |
| 43 | Form *chrome.SignIn |
| 44 | Sent *chrome.SignInNotice |
| 45 | Confirm *signinConfirm |
| 46 | Continue *signinContinue |
| 47 | Theme string |
| 48 | } |
| 49 | |
| 50 | // Title is the tab title, the door's own for each state. |
| 51 | func (page signinPage) Title() string { |
| 52 | switch { |
| 53 | case page.Sent != nil: |
| 54 | return page.Sent.Title() |
| 55 | case page.Form != nil: |
| 56 | return page.Form.Title() |
| 57 | } |
| 58 | return chrome.SignIn{App: "Mapa"}.Title() |
| 59 | } |
| 60 | |
| 61 | // signinConfirm is the emailed link's confirm step: one Sign in button |
| 62 | // that POSTs the token back, so a mail scanner that fetches every link |
| 63 | // signs nobody in. |
| 64 | type signinConfirm struct { |
| 65 | Door chrome.SignInNotice |
| 66 | Heading, Submit string |
| 67 | Action, Token string |
| 68 | } |
| 69 | |
| 70 | // confirmSignin is auth.Config.RenderConfirm. rastrillo has set no-store, |
| 71 | // the referrer policy and the framing refusals; nothing here loosens them. |
| 72 | func confirmSignin(writer http.ResponseWriter, request *http.Request, |
| 73 | data auth.ConfirmPageData) { |
| 74 | door := chrome.SignIn{} |
| 75 | render(writer, request, "signin", signinPage{Theme: themeAttr(request), |
| 76 | Confirm: &signinConfirm{Door: doorHead(request), |
| 77 | Heading: door.Heading(), Submit: door.Submit(), |
| 78 | Action: data.Action, Token: data.Token}}) |
| 79 | } |
| 80 | |
| 81 | // signinContinue is the keymail continuation (auth_navigation.go). |
| 82 | type signinContinue struct { |
| 83 | Door chrome.SignInNotice |
| 84 | ContinueURL string |
| 85 | } |
| 86 | |
| 87 | // doorHead is what every state of the door shares: the mark and name, on |
| 88 | // the suite's sky for this instance. |
| 89 | func doorHead(request *http.Request) chrome.SignInNotice { |
| 90 | return chrome.SignInNotice{Icon: assets.Path("static/brand/icon.svg"), |
| 91 | App: "Mapa", Sky: sky.For(request.Host)} |
| 92 | } |
| 93 | |
| 94 | func (app *app) authRoute(handler func(*auth.Auth, |
| 95 | http.ResponseWriter, *http.Request)) http.HandlerFunc { |
| 96 | return func(writer http.ResponseWriter, request *http.Request) { |
| 97 | if app.auth == nil { |
| 98 | app.logger.Error("no sign-in configured") |
| 99 | http.Error(writer, "sign-in is not configured", |
| 100 | http.StatusInternalServerError) |
| 101 | return |
| 102 | } |
| 103 | handler(app.auth, writer, request) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // signinError turns rastrillo's ?err= codes into plain language. None |
| 108 | // may tell an admitted address from a stranger's. |
| 109 | func signinError(code string) string { |
| 110 | switch code { |
| 111 | case "": |
| 112 | return "" |
| 113 | case "rate": |
| 114 | return "Too many sign-in attempts for that address. Wait a " + |
| 115 | "few minutes, then try again." |
| 116 | case "address": |
| 117 | return "That doesn't look like an email address. Check it " + |
| 118 | "and try again." |
| 119 | case "expired": |
| 120 | return "That sign-in link has expired or has already been " + |
| 121 | "used. Ask for a new one below." |
| 122 | case "keymail": |
| 123 | return "Keymail couldn't finish signing you in. You can have " + |
| 124 | "a link emailed to you instead." |
| 125 | } |
| 126 | return "Something went wrong on our side, not yours. Trying again " + |
| 127 | "won't help until it is fixed." |
| 128 | } |
| 129 | |