package calls
// The sign-in door (Home spec 2026-11-19-smart-door.md, step 6; home/connect
// MIGRATING.md): rastrillo's sign-in screen, its ui "signin" partial fed by
// auth.SigninState, drawn inside estilo's door by estilo-signin-screen. Its
// words are rastrillo's approved copy (rastrillo.ui.signin_*), and the
// signed-out page's are estilo's chrome.English, so every app in the suite
// says the same thing. Only the refusal and the removal are Calls'
// (signinRefusal, signinRemoved).
//
// Guests are not the door's business. A guest joins a call by name at the
// invitation (/j/{token}, guest.go), and nothing here stands in front of
// that. A guest session that reaches the door is drawn the door as anybody
// signed out is (signing in is exactly what a guest here is asking to do),
// and it is never refused, remembered or ended here (signin_admission.go).
import (
"context"
"html"
"net/http"
"net/url"
"strings"
"amadan.net/oficina/estilo.git/chrome"
"amadan.net/oficina/estilo.git/sky"
"amadan.net/oficina/home/connect"
rauth "amadan.net/rastrillo/rastrillo/auth"
"amadan.net/rastrillo/rastrillo/lastsignin"
)
// signinForgetPath is where rastrillo auth's Forget is mounted (app.go):
// "Use a different email" on the door, and "Forget me on this device" on
// the signed-out page. It is auth's own default, named here because the
// signed-out page and connect's RoutesConfig.ForgetPath must agree with it.
const signinForgetPath = "/signin/forget"
// oficinaChain is the hidden field the signed-out page's Forget form posts
// (chain=oficina) when Home is configured: after forgetting this browser
// here, Calls sends it on to Oficina ID's own Forget (forget, below). The
// door's "Use a different email" posts to the same Forget without it, and
// stays on the door.
const oficinaChain = "oficina"
// oficinaIDName is the suite's sign-in service, the federated door's name:
// the same in every language, like the door's own "Calls", and rastrillo
// draws it as "Continue with {name}".
const oficinaIDName = "Oficina ID"
// signinRefusal is the refusal (refused, and a session the signed-in
// routes no longer admit), in the screen's app-problem slot with the email
// field. Paul approved the wording for Docs and Sheets (2026-11-21/22):
// the same sentence with the app's name.
const signinRefusal = "That email can’t sign in to Calls. Use a different email, or ask whoever runs this team to add it."
// signinRemoved is the removal: endRemovedSession's landing, and a session
// the signed-in routes end as removed that reaches the door. Calls' own
// sentence, unchanged; only its place on the door moved (the app-problem
// slot, where rastrillo would otherwise draw its generic problem).
const signinRemoved = "Your access has been removed, so you’ve been signed out. Ask whoever runs this team if that’s wrong."
// doorIcon is Calls' mark at the head of every door card.
func doorIcon() string { return assets.Path("static/brand/icon.svg") }
// signinDoor is the sign-in door: standalone GET /signin, and with Home
// configured GET /signin and /signin/local (connect's SigninDoor and
// SigninLocal hand both here, RoutesConfig.LocalSignin), plus auth's
// RenderConfirm (the confirm step) and Refused (the refusal).
//
// rastrillo decides what the door shows from the query and this browser's
// cookies (SigninState: the way in it used last, Sent, problems, the
// chooser), with SigninOneWay drawing one way in per page. Calls adds what
// only it knows: Oficina ID where connect says the door may offer it
// (OficinaHref is "" on /signin/local, on a step-up and standalone), the
// email way first while an invitation waits (joining), the refusal, which
// only someone who has just verified the address, or whose session the
// signed-in routes no longer admit, reaches, and the removal.
func (a *app) signinDoor(w http.ResponseWriter, r *http.Request) {
a.signinDoorStatus(w, r, http.StatusOK)
}
func (a *app) signinDoorStatus(w http.ResponseWriter, r *http.Request, status int) {
st := a.auth.SigninState(r)
if st.Unavailable {
// Under a base path or on a direct host the hub signs people in:
// estilo-signin-screen draws nothing, and every route rastrillo's
// door names answers 404 there. So does the door.
a.notFound(w, r)
return
}
if href := connect.OficinaHref(r); href != "" {
st.Federated = &rauth.FederatedDoor{
Provider: lastsignin.ProviderOficina,
Href: href,
Label: oficinaIDName,
Icon: chrome.OficinaGlyph,
}
}
if joining(r) && st.Way == "" {
// Paul's ruling (2026-11-22, #46): on the way into a call, email
// is the door's default, whatever this browser used last, with
// Oficina ID a row under Other ways. An outsider gets in by the
// pending join an emailed link proves (spec §3.2).
st.Way = rauth.WayEmail
}
switch {
case refusedHere(r):
st.AppProblem = &rauth.AppProblem{Text: signinRefusal, Ask: true}
case removedHere(r):
// rastrillo knows no err=removed (it reads an unknown err as its
// generic problem), so the sentence is Calls', in place of it.
st.Problem = rauth.ProblemNone
st.AppProblem = &rauth.AppProblem{Text: signinRemoved, Ask: true}
}
a.rememberSigninReturn(w, r, &st)
a.auth.PrepareSigninResponse(w, st)
screen := chrome.SignInScreen{Icon: doorIcon(), App: appName, Sky: sky.For(r.Host), State: st}
title, err := signinTitle(screen)
if err != nil {
a.logger.Error("calls: render the sign-in title", "err", err)
a.ErrorPage(w, r, http.StatusInternalServerError, "")
return
}
w.Header().Set("Cache-Control", "no-store")
if status != http.StatusOK {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
}
render(w, "signin", a.page(r, frame{title: title, signinPage: true}, map[string]any{"Screen": screen}))
}
// signinTitle is the door's
: estilo's estilo-signin-screen-title,
// which is rastrillo's signin-title for the same state, so the tab and the
// heading always say the same thing ("Sign in to Calls", "Check your
// email to sign in to Calls"). The template writes escaped text and the
// layout escapes the title again, so it is unescaped here.
func signinTitle(screen chrome.SignInScreen) (string, error) {
var b strings.Builder
if err := pages["signin"].ExecuteTemplate(&b, "estilo-signin-screen-title", screen); err != nil {
return "", err
}
return strings.TrimSpace(html.UnescapeString(b.String())), nil
}
// renderConfirm is auth's RenderConfirm, the page an emailed link opens on
// (GET /auth/verify): the door, whose SigninState answers StepConfirm on
// this request alone, with one Sign in button that POSTs the token back,
// where auth spends it. The GET spends nothing, so a mail-security scanner
// that fetches every link cannot sign anybody in. auth has already set
// no-store, Referrer-Policy strict-origin (the URL is a live credential)
// and the framing refusals; nothing here loosens them.
func (a *app) renderConfirm(w http.ResponseWriter, r *http.Request, _ rauth.ConfirmPageData) {
a.signinDoor(w, r)
}
// refused is auth's Refused hook: Keymail or an emailed link verified the
// address and authorize said no (neither on the allowlist nor holding a
// live pending join). It answers with the door, carrying the refusal, in a
// 403: no session is minted, and auth has already ended the attempt.
//
// Drawn in this response, never from ?err=refused: a query parameter is
// something anybody can type, and the sentence means "you proved this
// address and Calls does not admit it" (Astra on #25, round 3). Only this
// hook can say it, and auth calls it only after the address is proved, so
// saying it is no membership oracle.
func (a *app) refused(w http.ResponseWriter, r *http.Request, _ rauth.Identity) {
a.signinDoorStatus(w, withRefusal(r), http.StatusForbidden)
}
// refusedKey and removedKey mark a request the door draws with the refusal
// or the removal; joiningKey one drawn while an invitation waits.
type (
refusedKey struct{}
removedKey struct{}
joiningKey struct{}
)
// withRefusal marks r as a refusal the door draws: a verified address
// authorize refused (refused), or a session the signed-in routes no longer
// admit (refusedSession).
//
// Only on the plain door: the chooser (?ways=) and a way's own page
// (?way=) are how a refused person reaches another way in, another Oficina
// ID account included, and a refusal there would draw the field again and
// leave "Other ways to sign in" going nowhere (Fable on Docs #139, N1).
func withRefusal(r *http.Request) *http.Request {
if choosing(r) {
return r
}
return r.WithContext(context.WithValue(r.Context(), refusedKey{}, true))
}
// withRemoval is withRefusal for a session the signed-in routes end as
// removed (endRemovedSession).
func withRemoval(r *http.Request) *http.Request {
if choosing(r) {
return r
}
return r.WithContext(context.WithValue(r.Context(), removedKey{}, true))
}
// choosing reports whether r is the chooser or a way's own page.
func choosing(r *http.Request) bool {
q := r.URL.Query()
return q.Has("ways") || q.Has("way")
}
// refusedHere reports whether r is a refusal the door is drawing. Never
// from the query: a query parameter is something anybody can type.
func refusedHere(r *http.Request) bool {
v, _ := r.Context().Value(refusedKey{}).(bool)
return v
}
// removedHere reports whether the door is drawing the removal:
// endRemovedSession's landing (?err=removed), or a session the probe found
// removed. On the plain door only, like the refusal. The query is read, as
// it always was: the sentence names no address and tells nobody anything
// about one.
func removedHere(r *http.Request) bool {
if v, _ := r.Context().Value(removedKey{}).(bool); v {
return true
}
return r.URL.Query().Get("err") == "removed" && !choosing(r)
}
// joining reports whether the door is being drawn for somebody on their
// way into a call through an invitation (the join cookie, joincookie.go):
// email is the door's default way, and Oficina ID one of its other ways
// (signinRoute; Paul's ruling, 2026-11-22).
func joining(r *http.Request) bool {
v, _ := r.Context().Value(joiningKey{}).(bool)
return v
}
// withJoining marks r as joining.
func withJoining(r *http.Request) *http.Request {
return r.WithContext(context.WithValue(r.Context(), joiningKey{}, true))
}
// renderSignedOut is where Sign out lands: /signed-out, in both modes, in
// the door's words: "You’re signed out", Sign in, then the quiet "Forget
// me on this device" (the smart-door spec's "Signed out"), a POST to
// rastrillo auth's Forget. With Home configured the page is connect's
// SignedOut, which carries that path (RoutesConfig.ForgetPath) for PageFor,
// and the form also asks Calls to go on to Oficina ID's own Forget once
// this one has answered (forget). Standalone it is Calls' own path, with
// no chain.
func (a *app) renderSignedOut(w http.ResponseWriter, r *http.Request, status int) {
p := connect.PageFor(r, "signedout", "")
signin, forget, chain := p.Action, p.ForgetAction, []chrome.Hidden{{Name: "chain", Value: oficinaChain}}
if signin == "" {
signin = signinPath
}
if forget == "" {
forget, chain = signinForgetPath, nil
}
notice := chrome.SignInNotice{Icon: doorIcon(), App: appName, Sky: sky.For(r.Host)}.
SignedOut(signin).Forgetting(forget, "", chain...)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
render(w, "signedout", a.page(r, frame{parts: []string{notice.Page()}, signinPage: true}, map[string]any{"Notice": notice}))
}
// signedOut serves GET /signed-out standalone. With Home configured
// connect serves it, through renderHome.
func (a *app) signedOut(w http.ResponseWriter, r *http.Request) {
a.renderSignedOut(w, r, http.StatusOK)
}
// forget is POST /signin/forget, rastrillo auth's Forget: "Use a different
// email" on the door, and "Forget me on this device" on the signed-out
// page. Each host remembers for itself, so with Home configured the
// signed-out page's form (chain=oficina) goes on to Oficina ID's own Forget
// once this one has answered, with Calls' signed-out page as the way back:
// {issuer}/forget?return=/signed-out. id.oficina.works asks "Forget
// me on Oficina ID too?" and returns only to a registered consumer's exact
// /signed-out. Anything but Forget's own redirect (a cross-origin refusal,
// a wrong method) is passed on as it is. A page a sign-in in progress kept
// (signin_return.go) is forgotten too.
func (a *app) forget(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
a.forgetReturn(w)
}
if a.home == nil {
a.auth.Forget(w, r)
return
}
result := &bufferedResponse{header: make(http.Header)}
a.auth.Forget(result, r)
if result.status == http.StatusSeeOther && r.PostFormValue("chain") == oficinaChain {
result.header.Set("Location", oficinaForgetURL(a.cfg.home.Issuer, a.cfg.Origin))
}
result.replay(w)
}
// oficinaForgetURL is Oficina ID's Forget, returning to this origin's
// signed-out page (Home's internal/home/door.go, forgetChainReturn).
func oficinaForgetURL(issuer, origin string) string {
back := strings.TrimSuffix(origin, "/") + "/signed-out"
return strings.TrimSuffix(issuer, "/") + "/forget?" + url.Values{"return": {back}}.Encode()
}
// standaloneSignout is rastrillo's Sign out, landing on /signed-out
// (signedOutAfter), which also forgets a page a sign-in in progress kept:
// it was this person's, not the next one's.
func (a *app) standaloneSignout(signout http.HandlerFunc) http.HandlerFunc {
landing := signedOutAfter(signout)
return func(w http.ResponseWriter, r *http.Request) {
a.forgetReturn(w)
landing(w, r)
}
}
// homeSignout is connect's Sign out (its 307 on to Oficina ID's, which
// the native app's SignOut reads too), which also forgets a page a sign-in
// in progress kept, for the same reason.
func (a *app) homeSignout(w http.ResponseWriter, r *http.Request) {
a.forgetReturn(w)
a.home.routes.Signout(w, r)
}