-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a json rest like api for login (post) logout (delete) and who-am-i (get). Login is authenticated by whatever authentication classes are active and a http cookie session is managed by the endpoint. fixes #5932
- Loading branch information
Showing
8 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added a login api endpoint to result in an authorization cookie from any other sort of feasible authentication. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import http | ||
import pytest | ||
|
||
pytestmark = [pytest.mark.parallel] | ||
|
||
|
||
@pytest.fixture | ||
def session_user(pulpcore_bindings, gen_user, anonymous_user): | ||
old_cookie = pulpcore_bindings.client.cookie | ||
user = gen_user() | ||
with user: | ||
response = pulpcore_bindings.LoginApi.login_with_http_info() | ||
if isinstance(response, tuple): | ||
# old bindings | ||
_, _, headers = response | ||
else: | ||
# new bindings | ||
headers = response.headers | ||
cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) | ||
# Use anonymous_user to remove the basic auth header from the api client. | ||
with anonymous_user: | ||
pulpcore_bindings.client.cookie = "; ".join( | ||
(f"{k}={v.value}" for k, v in cookie_jar.items()) | ||
) | ||
yield user | ||
pulpcore_bindings.client.cookie = old_cookie | ||
|
||
|
||
def test_login_read_denies_anonymous(pulpcore_bindings, anonymous_user): | ||
with anonymous_user: | ||
with pytest.raises(pulpcore_bindings.module.ApiException) as exc: | ||
pulpcore_bindings.LoginApi.login_read() | ||
assert exc.value.status == 401 | ||
|
||
|
||
def test_login_read_returns_username(pulpcore_bindings, gen_user): | ||
user = gen_user() | ||
with user: | ||
result = pulpcore_bindings.LoginApi.login_read() | ||
assert result.username == user.username | ||
|
||
|
||
def test_login_denies_anonymous(pulpcore_bindings, anonymous_user): | ||
with anonymous_user: | ||
with pytest.raises(pulpcore_bindings.module.ApiException) as exc: | ||
pulpcore_bindings.LoginApi.login() | ||
assert exc.value.status == 401 | ||
|
||
|
||
def test_login_sets_session_cookie(pulpcore_bindings, gen_user): | ||
user = gen_user() | ||
with user: | ||
response = pulpcore_bindings.LoginApi.login_with_http_info() | ||
if isinstance(response, tuple): | ||
# old bindings | ||
result, status, headers = response | ||
else: | ||
# new bindings | ||
result = response.data | ||
status = response.status | ||
headers = response.headers | ||
assert status == 201 | ||
assert result.username == user.username | ||
cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) | ||
assert cookie_jar["sessionid"].value != "" | ||
assert cookie_jar["csrftoken"].value != "" | ||
|
||
|
||
def test_session_cookie_is_authorization(pulpcore_bindings, anonymous_user, session_user): | ||
result = pulpcore_bindings.LoginApi.login_read() | ||
assert result.username == session_user.username | ||
|
||
|
||
# For whatever reason, this tests fails with '{"detail":"CSRF Failed: CSRF token missing."}' | ||
# But we sent the csrf token along... | ||
# The test right after this tries to close the gap and uses basic auth to logout. | ||
# Please remove it when this one is fixed. | ||
@pytest.mark.xfail | ||
def test_logout_removes_sessionid(pulpcore_bindings, session_user): | ||
response = pulpcore_bindings.LoginApi.logout_with_http_info() | ||
if isinstance(response, tuple): | ||
# old bindings | ||
_, status, headers = response | ||
else: | ||
# new bindings | ||
status = response.status | ||
headers = response.headers | ||
assert status == 204 | ||
cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) | ||
assert cookie_jar["sessionid"].value == "" | ||
|
||
|
||
def test_basicauth_logout_removes_sessionid(pulpcore_bindings, session_user): | ||
with session_user: | ||
response = pulpcore_bindings.LoginApi.logout_with_http_info() | ||
if isinstance(response, tuple): | ||
# old bindings | ||
_, status, headers = response | ||
else: | ||
# new bindings | ||
status = response.status | ||
headers = response.headers | ||
assert status == 204 | ||
cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) | ||
assert cookie_jar["sessionid"].value == "" | ||
|
||
|
||
def test_logout_denies_anonymous(pulpcore_bindings, anonymous_user): | ||
with anonymous_user: | ||
with pytest.raises(pulpcore_bindings.module.ApiException) as exc: | ||
pulpcore_bindings.LoginApi.logout() | ||
assert exc.value.status == 401 |