diff --git a/app/api/users.py b/app/api/users.py index d54db81..e629284 100644 --- a/app/api/users.py +++ b/app/api/users.py @@ -16,8 +16,8 @@ from pydantic import Field, field_validator from .. import machines, mail, observability from ..core import db -from ..core.auth import (CAPABILITIES, audit, can, has, machine_bound, machine_question, may_move, may_reorder, - password_hash, password_matches, roles_of, signed_in) +from ..core.auth import (CAPABILITIES, audit, can, client_ip, has, login_failed, machine_bound, machine_question, + may_move, may_reorder, password_hash, password_matches, roles_of, signed_in, throttle) from ..core.models import StrictModel from ..runtime import BACK, ENVIRONMENT, STATES, TRANSITIONS @@ -149,12 +149,21 @@ def choose_session_machine(body: MachineAnswer, request: Request): @router.post('/api/operator/me/password') def change_password(body: PasswordChange, request: Request): + """As a customer's (app/api/customer.py): a wrong current password counts + against the sign-in, so a session left open cannot guess it, and the new one + signs out every other session of this account (security area 3, 01/12).""" account = signed_in(request) + throttle('operator:' + account['email'], request) with db.connect() as c: row = c.execute('SELECT password_hash FROM dtf_local.operators WHERE id=%s', (account['id'],)).fetchone() - if not password_matches(body.current, row['password_hash']): - raise HTTPException(403, 'A senha atual não confere.') + if not password_matches(body.current, row['password_hash']): + login_failed('operator:' + account['email']) + audit('operator_password_change_failed', operator=account['email'], ip=client_ip(request)) + raise HTTPException(403, 'A senha atual não confere.') + with db.connect() as c: c.execute('UPDATE dtf_local.operators SET password_hash=%s WHERE id=%s', (password_hash(body.new), account['id'])) + c.execute('DELETE FROM dtf_local.operator_sessions WHERE username=%s AND token_hash<>%s', + (account['email'], account['session'])) audit('operator_password_changed', operator=account['email']) return {'ok': True} diff --git a/tests/users_test.py b/tests/users_test.py index 2198fea..bbb8277 100644 --- a/tests/users_test.py +++ b/tests/users_test.py @@ -2,6 +2,7 @@ Run inside the API container: python -m tests.users_test """ +import hashlib import secrets import time from urllib.request import Request, urlopen @@ -318,8 +319,24 @@ def run(): seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'sete-77'}, expected=422) seller.call('/operator/me/password', {'current': PASSWORD, 'new': 'oito-888'}) seller = signed_in(emails['vendedor'], 'oito-888') + # The new password signs out the account's other sessions, and keeps + # this one (as a customer's, security area 3, 01/12). + other_device = signed_in(emails['vendedor'], 'oito-888') seller.call('/operator/me/password', {'current': 'oito-888', 'new': 'outra-senha-de-teste'}) + seller.call('/operator/me') + other_device.call('/operator/me', expected=401) signed_in(emails['vendedor'], 'outra-senha-de-teste').call('/operator/me') + # A session left open cannot guess the current password: wrong ones count + # against the sign-in and stop at 10, as a failed sign-in does. The count + # starts from zero here: the suites before this one share the address. + with db.connect() as c: + c.execute('DELETE FROM dtf_local.login_attempts') + for _ in range(10): + seller.call('/operator/me/password', {'current': 'errada', 'new': 'qualquer-123'}, expected=403) + seller.call('/operator/me/password', {'current': 'errada', 'new': 'qualquer-123'}, expected=429) + with db.connect() as c: + c.execute('DELETE FROM dtf_local.login_attempts WHERE key=%s', (hashlib.sha256( + ('auth-account|operator:' + emails['vendedor']).encode()).hexdigest(),)) # A second Têxtil machine. Who is on each machine is who chose it after # signing in: Usuários only shows it (item 12).