oficinaid
Code
Branches
CI
Releases
Discussions
Prompts
Insights
Settings
equipo / internal / equipo / oficinaid / reauth.go
Download this file
1 package oficinaid 2 3 import ( 4 "net/http" 5 "time" 6 7 "amadan.net/rastrillo/rastrillo/sessions" 8 ) 9 10 // LocalReauth keeps the initiating session available to the link transaction. 11 // The normal email handler still earns and mints the replacement session. 12 // CompleteReauth rechecks and revokes the original together with its proof. 13 func (h *HTTP) LocalReauth(next http.HandlerFunc) http.HandlerFunc { 14 return func(w http.ResponseWriter, r *http.Request) { 15 proof, token := cookie(r, reauthCookie), h.token(r) 16 if proof != "" && token != "" && (r.URL.Path == "/auth/callback" || r.Method == http.MethodPost) { 17 var one int 18 e := h.Store.DB.QueryRowContext(r.Context(), "SELECT 1 FROM equipo_id_reauth WHERE id_hash=? AND session_hash=? AND ready=0 AND expires>?", sessions.HashToken(proof), sessions.HashToken(token), time.Now().Unix()).Scan(&one) 19 if _, sessionErr := h.Store.Session(r.Context(), token, time.Now()); e == nil && sessionErr == nil { 20 nextRequest := r.Clone(r.Context()) 21 nextRequest.Header = r.Header.Clone() 22 nextRequest.Header.Del("Cookie") 23 for _, c := range r.Cookies() { 24 if c.Name != h.Auth.SessionCookie() { 25 nextRequest.AddCookie(c) 26 } 27 } 28 r = nextRequest 29 } 30 } 31 next(w, r) 32 } 33 } 34