81 lines
3.2 KiB
Python
81 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
import base64
|
|
import uuid
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
|
|
from client import Client, Failure, arguments, check, run # noqa: E402
|
|
|
|
|
|
def register(c, username, password):
|
|
c.call('POST', '/api/v1/users', 201, body={
|
|
'username': username,
|
|
'display_name': 'Auth test',
|
|
'password': password,
|
|
})
|
|
|
|
|
|
def login(c, username, password):
|
|
cred = base64.b64encode(f'{username}:{password}'.encode()).decode()
|
|
r = c.call('POST', '/api/v1/auth/sessions', 201,
|
|
headers={'Authorization': 'Basic ' + cred}, schema='Session')
|
|
return r['json']
|
|
|
|
|
|
def main():
|
|
args = arguments('Logout и неверные учётные данные')
|
|
c = Client(args.base_url, args.ca_file)
|
|
|
|
# --- logout отзывает текущую сессию ---
|
|
username = 'lo_' + uuid.uuid4().hex[:10]
|
|
password = 'T3st-' + uuid.uuid4().hex
|
|
register(c, username, password)
|
|
session = login(c, username, password)
|
|
actor = {'id': session['user']['id'], 'token': session['token']}
|
|
|
|
# токен работает
|
|
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
|
|
|
# отзываем
|
|
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor)
|
|
|
|
# тот же токен больше не работает
|
|
c.call('GET', '/api/v1/users/me', 401, actor=actor)
|
|
c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=actor)
|
|
|
|
# --- неверный пароль и неизвестный username дают одинаковый 401 ---
|
|
wrong = base64.b64encode(f'{username}:wrong-password-123'.encode()).decode()
|
|
r_wrong = c.call('POST', '/api/v1/auth/sessions', 401,
|
|
headers={'Authorization': 'Basic ' + wrong})
|
|
check('Basic' in r_wrong['headers'].get('WWW-Authenticate', ''),
|
|
'нет WWW-Authenticate: Basic на неверный пароль')
|
|
|
|
unknown = base64.b64encode(
|
|
f'nobody_{uuid.uuid4().hex[:10]}:any-password-123'.encode()).decode()
|
|
r_unknown = c.call('POST', '/api/v1/auth/sessions', 401,
|
|
headers={'Authorization': 'Basic ' + unknown})
|
|
|
|
check(r_wrong['json']['error']['code'] == 'unauthorized',
|
|
'неверный пароль: ожидался код unauthorized')
|
|
check(r_unknown['json']['error']['code'] == 'unauthorized',
|
|
'неизвестный username: ожидался код unauthorized')
|
|
check(r_wrong['json']['error']['message'] == r_unknown['json']['error']['message'],
|
|
'сообщения отличаются: перебор username возможен')
|
|
|
|
# --- X-User-Id не даёт доступ в lab2 ---
|
|
c.call('GET', '/api/v1/users/me', 401,
|
|
headers={'X-User-Id': session['user']['id']})
|
|
|
|
# --- Basic на бизнес-endpoint не проходит ---
|
|
good = base64.b64encode(f'{username}:{password}'.encode()).decode()
|
|
c.call('GET', '/api/v1/users/me', 401,
|
|
headers={'Authorization': 'Basic ' + good})
|
|
|
|
print('PASS: logout, wrong password, X-User-Id и Basic на бизнес-endpoint')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main) |