| 1 | package oficinaid |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/home/connect/linkstore" |
| 10 | "amadan.net/rastrillo/rastrillo/sessions" |
| 11 | ) |
| 12 | |
| 13 | func (s *Store) BeginReauth(ctx context.Context, token, purpose string, now time.Time) (string, error) { |
| 14 | if purpose != "link" && purpose != "unlink" { |
| 15 | return "", ErrRefused |
| 16 | } |
| 17 | tx, e := s.begin(ctx, now) |
| 18 | if e != nil { |
| 19 | return "", e |
| 20 | } |
| 21 | defer tx.Rollback() |
| 22 | sess, e := s.session(ctx, tx, token, now) |
| 23 | if e != nil { |
| 24 | return "", e |
| 25 | } |
| 26 | id, e := s.account(ctx, tx, sess.Subject) |
| 27 | if e != nil { |
| 28 | return "", e |
| 29 | } |
| 30 | if _, e = tx.ExecContext(ctx, "DELETE FROM equipo_id_reauth WHERE expires<=?", now.Unix()); e != nil { |
| 31 | return "", e |
| 32 | } |
| 33 | var n int |
| 34 | if e = tx.QueryRowContext(ctx, "SELECT count(*) FROM equipo_id_reauth").Scan(&n); e != nil || n >= 10000 { |
| 35 | return "", ErrRefused |
| 36 | } |
| 37 | secret, hash, e := sessions.NewToken() |
| 38 | if e != nil { |
| 39 | return "", e |
| 40 | } |
| 41 | _, e = tx.ExecContext(ctx, "INSERT INTO equipo_id_reauth(id_hash,account_id,session_hash,started,expires,purpose) VALUES(?,?,?,?,?,?)", hash, id, sessions.HashToken(token), now.Unix(), now.Unix()+300, purpose) |
| 42 | if e != nil { |
| 43 | return "", e |
| 44 | } |
| 45 | if e = tx.Commit(); e != nil { |
| 46 | return "", e |
| 47 | } |
| 48 | return secret, nil |
| 49 | } |
| 50 | func (s *Store) CompleteReauth(ctx context.Context, secret, token string, now time.Time) (string, string, error) { |
| 51 | tx, e := s.begin(ctx, now) |
| 52 | if e != nil { |
| 53 | return "", "", e |
| 54 | } |
| 55 | defer tx.Rollback() |
| 56 | high, e := s.clock(ctx, tx, now) |
| 57 | if e != nil { |
| 58 | return "", "", e |
| 59 | } |
| 60 | var id, started, expires, ready int64 |
| 61 | var old, purpose string |
| 62 | e = tx.QueryRowContext(ctx, "SELECT account_id,session_hash,started,expires,purpose,ready FROM equipo_id_reauth WHERE id_hash=?", sessions.HashToken(secret)).Scan(&id, &old, &started, &expires, &purpose, &ready) |
| 63 | if e != nil || ready != 0 || expires <= high || old == sessions.HashToken(token) { |
| 64 | return "", "", ErrRefused |
| 65 | } |
| 66 | local, e := s.local(ctx, tx, token, now) |
| 67 | if e != nil || local.PersonID != id || local.AuthTime.Unix() < started { |
| 68 | return "", "", ErrRefused |
| 69 | } |
| 70 | // The original session must still exist; revocation invalidates the request. |
| 71 | var originalExpiry, originalCreated string |
| 72 | if e = tx.QueryRowContext(ctx, "SELECT expires_at,created_at FROM sessions WHERE token_hash=?", old).Scan(&originalExpiry, &originalCreated); e != nil { |
| 73 | return "", "", ErrRefused |
| 74 | } |
| 75 | expiry, e := time.Parse(time.RFC3339, originalExpiry) |
| 76 | if e != nil || !expiry.After(now) { |
| 77 | return "", "", ErrRefused |
| 78 | } |
| 79 | created, e := time.Parse(time.RFC3339, originalCreated) |
| 80 | if e != nil { |
| 81 | return "", "", ErrRefused |
| 82 | } |
| 83 | address, e := s.lookup(ctx, tx, id) |
| 84 | if e != nil { |
| 85 | return "", "", e |
| 86 | } |
| 87 | var epoch int64 |
| 88 | if e = tx.QueryRowContext(ctx, "SELECT session_epoch FROM members WHERE address=?", address).Scan(&epoch); e != nil || created.Unix() < epoch { |
| 89 | return "", "", ErrRefused |
| 90 | } |
| 91 | if _, e = tx.ExecContext(ctx, "DELETE FROM sessions WHERE token_hash=?", old); e != nil { |
| 92 | return "", "", e |
| 93 | } |
| 94 | var next string |
| 95 | if purpose == "link" { |
| 96 | next, e = s.Links.StartFreshOn(ctx, tx, local, "/account/home/confirm", now) |
| 97 | if e == nil { |
| 98 | _, e = tx.ExecContext(ctx, "DELETE FROM equipo_id_reauth WHERE id_hash=?", sessions.HashToken(secret)) |
| 99 | } |
| 100 | } else { |
| 101 | next = secret |
| 102 | _, e = tx.ExecContext(ctx, "UPDATE equipo_id_reauth SET session_hash=?,ready=1 WHERE id_hash=?", sessions.HashToken(token), sessions.HashToken(secret)) |
| 103 | } |
| 104 | if e != nil { |
| 105 | return "", "", e |
| 106 | } |
| 107 | if e = tx.Commit(); e != nil { |
| 108 | return "", "", e |
| 109 | } |
| 110 | return next, purpose, nil |
| 111 | } |
| 112 | func (s *Store) Bind(ctx context.Context, init, token, nonce string, now time.Time) error { |
| 113 | tx, e := s.begin(ctx, now) |
| 114 | if e != nil { |
| 115 | return e |
| 116 | } |
| 117 | local, e := s.local(ctx, tx, token, now) |
| 118 | tx.Rollback() |
| 119 | if e != nil { |
| 120 | return e |
| 121 | } |
| 122 | return s.Links.BindNonce(ctx, init, local, nonce, now) |
| 123 | } |
| 124 | func (s *Store) Confirm(ctx context.Context, proof, token string, now time.Time) error { |
| 125 | tx, e := s.begin(ctx, now) |
| 126 | if e != nil { |
| 127 | return e |
| 128 | } |
| 129 | defer tx.Rollback() |
| 130 | local, e := s.local(ctx, tx, token, now) |
| 131 | if e != nil { |
| 132 | return e |
| 133 | } |
| 134 | _, e = s.Links.ConfirmOn(ctx, tx, proof, local, now) |
| 135 | if errors.Is(e, linkstore.ErrRefused) { |
| 136 | if commitErr := tx.Commit(); commitErr != nil { |
| 137 | return commitErr |
| 138 | } |
| 139 | return e |
| 140 | } |
| 141 | if e != nil { |
| 142 | return e |
| 143 | } |
| 144 | if _, e = tx.ExecContext(ctx, "UPDATE equipo_id_accounts SET linked_at=? WHERE id=?", now.Unix(), local.PersonID); e != nil { |
| 145 | return e |
| 146 | } |
| 147 | if e = s.audit(ctx, tx, local.PersonID, "oficina_id.link", now); e != nil { |
| 148 | return e |
| 149 | } |
| 150 | return tx.Commit() |
| 151 | } |
| 152 | func (s *Store) Unlink(ctx context.Context, proof, token string, now time.Time) error { |
| 153 | tx, e := s.begin(ctx, now) |
| 154 | if e != nil { |
| 155 | return e |
| 156 | } |
| 157 | defer tx.Rollback() |
| 158 | local, e := s.local(ctx, tx, token, now) |
| 159 | if e != nil { |
| 160 | return e |
| 161 | } |
| 162 | high, e := s.clock(ctx, tx, now) |
| 163 | if e != nil { |
| 164 | return e |
| 165 | } |
| 166 | var one int |
| 167 | if e = tx.QueryRowContext(ctx, "SELECT 1 FROM equipo_id_reauth WHERE id_hash=? AND account_id=? AND session_hash=? AND purpose='unlink' AND ready=1 AND expires>?", sessions.HashToken(proof), local.PersonID, sessions.HashToken(token), high).Scan(&one); e != nil { |
| 168 | return ErrRefused |
| 169 | } |
| 170 | if _, e = tx.ExecContext(ctx, "DELETE FROM sessions WHERE method IN (?,?) AND subject IN (SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=?)", Method, NativeMethod, s.Config.Issuer, local.PersonID); e != nil { |
| 171 | return e |
| 172 | } |
| 173 | if _, e = tx.ExecContext(ctx, "DELETE FROM home_identity_links WHERE issuer=? AND person_id=?", s.Config.Issuer, local.PersonID); e != nil { |
| 174 | return e |
| 175 | } |
| 176 | if _, e = tx.ExecContext(ctx, "DELETE FROM equipo_id_reauth WHERE account_id=?", local.PersonID); e != nil { |
| 177 | return e |
| 178 | } |
| 179 | for _, table := range []string{"home_link_flows", "oficina_handoff_pending", "oficina_handoff_inbox"} { |
| 180 | if _, e = tx.ExecContext(ctx, "DELETE FROM "+table); e != nil { |
| 181 | return e |
| 182 | } |
| 183 | } |
| 184 | if _, e = tx.ExecContext(ctx, "UPDATE equipo_id_accounts SET linked_at=0 WHERE id=?", local.PersonID); e != nil { |
| 185 | return e |
| 186 | } |
| 187 | if e = s.audit(ctx, tx, local.PersonID, "oficina_id.unlink", now); e != nil { |
| 188 | return e |
| 189 | } |
| 190 | return tx.Commit() |
| 191 | } |
| 192 | |
| 193 | type Linked struct { |
| 194 | Subject string |
| 195 | At time.Time |
| 196 | } |
| 197 | |
| 198 | func (s *Store) Linked(ctx context.Context, token string, now time.Time) (Linked, error) { |
| 199 | sess, e := s.Session(ctx, token, now) |
| 200 | if e != nil { |
| 201 | return Linked{}, e |
| 202 | } |
| 203 | var out Linked |
| 204 | var at int64 |
| 205 | e = s.DB.QueryRowContext(ctx, "SELECT l.subject,a.linked_at FROM home_identity_links l JOIN equipo_id_accounts a ON a.id=l.person_id WHERE l.issuer=? AND a.binding=?", s.Config.Issuer, s.binding(sess.Subject)).Scan(&out.Subject, &at) |
| 206 | if errors.Is(e, sql.ErrNoRows) { |
| 207 | return out, nil |
| 208 | } |
| 209 | out.At = time.Unix(at, 0) |
| 210 | return out, e |
| 211 | } |
| 212 | func (s *Store) Pending(ctx context.Context, proof, token string, now time.Time) error { |
| 213 | tx, e := s.begin(ctx, now) |
| 214 | if e != nil { |
| 215 | return e |
| 216 | } |
| 217 | local, e := s.local(ctx, tx, token, now) |
| 218 | tx.Rollback() |
| 219 | if e != nil { |
| 220 | return e |
| 221 | } |
| 222 | _, e = s.Links.Pending(ctx, proof, local, now) |
| 223 | return e |
| 224 | } |
| 225 | |