forked from pallets-eco/flask-security-3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix session-only authentication. (pallets-eco#813)
* Update test_common.py Added testcase for failing toke-authentication on session-only endpoint * Update conftest.py Added session-only authenticated route to test-fixture * Update decorators.py Added the `_check_session` function to specifically check session data to be used as authentication_method in the `auth_required` * Update decorators.py * Update decorators.py * fixed decorator and added tests * Fix session-only authentication. If an endpoint was decorated with "session" only - a properly submitted token would also be accepted. Fix that by checking as part of the auth_required() decorator and the user is authenticated AND was authenticated using the _user_loader (which is what flask-login calls for session based authenticated). close pallets-eco#791 --------- Co-authored-by: N247S <[email protected]>
- Loading branch information
Showing
6 changed files
with
77 additions
and
11 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
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 |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
import re | ||
import pytest | ||
|
||
from flask import Blueprint | ||
from flask import Blueprint, g | ||
|
||
from flask_security import uia_email_mapper | ||
from flask_security.decorators import auth_required | ||
from flask_principal import identity_loaded | ||
|
||
from tests.test_utils import ( | ||
authenticate, | ||
|
@@ -527,6 +529,17 @@ def test_token_auth_via_header_invalid_token(client): | |
assert response.status_code == 401 | ||
|
||
|
||
def test_token_auth_invalid_for_session_auth(client): | ||
# when user is loaded from token data, session authentication should fail. | ||
response = json_authenticate(client) | ||
token = response.json["response"]["user"]["authentication_token"] | ||
# logout so session doesn't contain valid user details | ||
logout(client) | ||
headers = {"Authentication-Token": token, "Accept": "application/json"} | ||
response = client.get("/session", headers=headers) | ||
assert response.status_code == 401 | ||
|
||
|
||
def test_http_auth(client): | ||
# browsers expect 401 response with WWW-Authenticate header - which will prompt | ||
# them to pop up a login form. | ||
|
@@ -741,6 +754,28 @@ def test_user_deleted_during_session_reverts_to_anonymous_user(app, client): | |
assert b"Hello [email protected]" not in response.data | ||
|
||
|
||
def test_session_loads_identity(app, client): | ||
@app.route("/identity_check") | ||
@auth_required("session") | ||
def id_check(): | ||
if hasattr(g, "identity"): | ||
identity = g.identity | ||
assert hasattr(identity, "loader_called") | ||
assert identity.loader_called | ||
return "Success" | ||
|
||
json_authenticate(client) | ||
|
||
# add identity loader after authentication to only fire it for | ||
# session-authentication next `get` call | ||
@identity_loaded.connect_via(app) | ||
def identity_loaded_check(sender, identity): | ||
identity.loader_called = True | ||
|
||
response = client.get("/identity_check") | ||
assert b"Success" == response.data | ||
|
||
|
||
def test_remember_token(client): | ||
response = authenticate(client, follow_redirects=False) | ||
client.delete_cookie("session") | ||
|
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 |
---|---|---|
|
@@ -1212,15 +1212,15 @@ def test_user_handle(app, client, get_message): | |
.decode("utf-8") | ||
.replace("=", "") | ||
) | ||
upd_signin_data = copy.deepcopy(SIGNIN_DATA_UH) | ||
upd_signin_data["response"]["userHandle"] = b64_user_handle | ||
signin_options, response_url, _ = _signin_start_json(client, "[email protected]") | ||
response = client.post( | ||
response_url, json=dict(credential=json.dumps(upd_signin_data)) | ||
) | ||
# verify actually logged in | ||
response = client.get("/profile", headers={"accept": "application/json"}) | ||
assert response.status_code == 200 | ||
upd_signin_data = copy.deepcopy(SIGNIN_DATA_UH) | ||
upd_signin_data["response"]["userHandle"] = b64_user_handle | ||
signin_options, response_url, _ = _signin_start_json(client, "[email protected]") | ||
response = client.post( | ||
response_url, json=dict(credential=json.dumps(upd_signin_data)) | ||
) | ||
# verify actually logged in | ||
response = client.get("/profile", headers={"accept": "application/json"}) | ||
assert response.status_code == 200 | ||
|
||
|
||
@pytest.mark.settings(webauthn_util_cls=HackWebauthnUtil) | ||
|