| 1 | package oficinaid |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/sessions" |
| 5 | "encoding/base64" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func (h *HTTP) Signout(w http.ResponseWriter, r *http.Request) { h.guard(h.signout)(w, r) } |
| 12 | func (h *HTTP) signout(w http.ResponseWriter, r *http.Request) { |
| 13 | token := h.token(r) |
| 14 | tx, e := h.Store.DB.BeginTx(r.Context(), nil) |
| 15 | if e != nil { |
| 16 | h.fail(w, r, e) |
| 17 | return |
| 18 | } |
| 19 | defer tx.Rollback() |
| 20 | var method string |
| 21 | if token != "" { |
| 22 | _ = tx.QueryRowContext(r.Context(), "SELECT method FROM sessions WHERE token_hash=?", sessions.HashToken(token)).Scan(&method) |
| 23 | if _, e = tx.ExecContext(r.Context(), "DELETE FROM sessions WHERE token_hash=?", sessions.HashToken(token)); e != nil { |
| 24 | h.fail(w, r, e) |
| 25 | return |
| 26 | } |
| 27 | } |
| 28 | if e = tx.Commit(); e != nil { |
| 29 | h.fail(w, r, e) |
| 30 | return |
| 31 | } |
| 32 | set(w, h.Auth.SessionCookie(), "", -1) |
| 33 | set(w, logoutCookie, "", -1) |
| 34 | if IsHomeDerived(method) { |
| 35 | sealed, e := h.Store.seal.Seal("signed-out", []byte(strconv.FormatInt(time.Now().Unix()+300, 10))) |
| 36 | if e == nil { |
| 37 | set(w, logoutCookie, base64.RawURLEncoding.EncodeToString(sealed), 300) |
| 38 | } |
| 39 | } |
| 40 | http.Redirect(w, r, "/signed-out", http.StatusSeeOther) |
| 41 | } |
| 42 | func (h *HTTP) SignedOut(w http.ResponseWriter, r *http.Request) { |
| 43 | view := View{State: "signed-out"} |
| 44 | values := []string{} |
| 45 | for _, c := range r.Cookies() { |
| 46 | if c.Name == logoutCookie { |
| 47 | values = append(values, c.Value) |
| 48 | } |
| 49 | } |
| 50 | if len(values) == 1 && len(values[0]) < 256 { |
| 51 | sealed, e := base64.RawURLEncoding.Strict().DecodeString(values[0]) |
| 52 | if e == nil { |
| 53 | plain, e := h.Store.seal.Open("signed-out", sealed) |
| 54 | if e == nil { |
| 55 | until, e := strconv.ParseInt(string(plain), 10, 64) |
| 56 | if e == nil && until > time.Now().Unix() && until <= time.Now().Unix()+300 { |
| 57 | view.HomeSignout = h.Store.Config.Issuer + "/signout" |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | h.Render(w, r, view, 200) |
| 63 | } |
| 64 | |