diff --git a/app/api/customer.py b/app/api/customer.py index 29359a3..42f9202 100644 --- a/app/api/customer.py +++ b/app/api/customer.py @@ -394,13 +394,16 @@ def reset_password(body: PasswordReset, request: Request, response: Response): raise HTTPException(400, 'Este link venceu ou já foi usado. Peça outro em “Esqueci minha senha”.') identity = reset['account_id'] account = account_row(c, identity, lock=True) - c.execute('UPDATE dtf_local.accounts SET password_hash=%s WHERE id=%s', (password_hash(body.password), identity)) + # The link came by e-mail, so the address is confirmed too. + c.execute('UPDATE dtf_local.accounts SET password_hash=%s, email_verified=true WHERE id=%s', + (password_hash(body.password), identity)) c.execute('UPDATE dtf_local.password_resets SET used_at=now() WHERE token_hash=%s', (digest,)) # Every other link to this account stops working, and so does every # session signed in with the old password. c.execute('DELETE FROM dtf_local.password_resets WHERE account_id=%s AND used_at IS NULL', (identity,)) if previous: transfer_guest(c, previous, identity) + claim_orders(c, identity, account['profile']) c.execute('DELETE FROM dtf_local.sessions WHERE owner=%s', (identity,)) if previous: c.execute('DELETE FROM dtf_local.sessions WHERE id=%s', (previous['id'],)) diff --git a/app/schema.sql b/app/schema.sql index 952d7c3..7655ffc 100644 --- a/app/schema.sql +++ b/app/schema.sql @@ -304,10 +304,14 @@ CREATE TABLE IF NOT EXISTS dtf_local.email_changes ( sent_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz NOT NULL, sends int NOT NULL DEFAULT 1, attempts int NOT NULL DEFAULT 0, done_at timestamptz ); --- Accounts confirmed by a code before email_verified existed. +-- Accounts confirmed by a code before email_verified existed. The signups +-- are deleted after a day; the code e-mail sent before the account existed +-- stays in the queue. UPDATE dtf_local.accounts a SET email_verified=true WHERE NOT a.email_verified AND ( EXISTS (SELECT 1 FROM dtf_local.account_signups s WHERE s.email=a.email AND s.done_at IS NOT NULL) - OR EXISTS (SELECT 1 FROM dtf_local.email_changes e WHERE e.account_id=a.id AND e.new_email=a.email AND e.done_at IS NOT NULL)); + OR EXISTS (SELECT 1 FROM dtf_local.email_changes e WHERE e.account_id=a.id AND e.new_email=a.email AND e.done_at IS NOT NULL) + OR EXISTS (SELECT 1 FROM dtf_local.mail_queue m WHERE m.kind='code' AND lower(m.to_addr)=a.email + AND m.created_at<=a.created_at)); -- E-mail kinds: a password-recovery link, a code, the notice that an account -- already exists, the notice of a changed e-mail, the team's alerts and a test. -- Defined once, here: the schema runs on every start, and an earlier, narrower diff --git a/tests/mail_test.py b/tests/mail_test.py index 156bc3d..3d57a55 100644 --- a/tests/mail_test.py +++ b/tests/mail_test.py @@ -91,11 +91,15 @@ def recovery(tag): with db.connect() as c: # Only the token's hash is kept. assert not c.execute('SELECT 1 FROM dtf_local.password_resets WHERE token_hash=%s', (token,)).fetchone() + # An account never confirmed: the reset link, sent to the address, confirms it. + c.execute('UPDATE dtf_local.accounts SET email_verified=false WHERE email=%s', (address,)) stranger.call('/account/reset', {'token': 'x' * 43, 'password': NEW_PASSWORD}, expected=400) stranger.call('/account/reset', {'token': token, 'password': 'curta'}, expected=422) assert stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD})['customer']['mail'] == address assert stranger.call('/account/me')['customer']['mail'] == address + with db.connect() as c: + assert c.execute('SELECT email_verified FROM dtf_local.accounts WHERE email=%s', (address,)).fetchone()['email_verified'] # Works once; the old password and the old sessions are gone. stranger.call('/account/reset', {'token': token, 'password': NEW_PASSWORD}, expected=400) elsewhere.call('/account/me', expected=401)