-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from GabrielInTheWorld/of-can-access-mediafiles
Python-library: Can authenticate only with cookie now
- Loading branch information
Showing
6 changed files
with
97 additions
and
9 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
43 changes: 43 additions & 0 deletions
43
auth/libraries/pip-auth/authlib/test/test_authenticate_only_cookie.py
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,43 @@ | ||
import jwt | ||
|
||
from .base import BaseTestEnvironment | ||
from ..config import Environment | ||
from ..constants import USER_ID_PROPERTY | ||
from datetime import datetime | ||
|
||
class TestAuthenticateOnlyCookie(BaseTestEnvironment): | ||
environment = Environment() | ||
|
||
def test_authenticate_with_cookie(self): | ||
_, cookie = self.fake_request.login() | ||
user_id = self.auth_handler.authenticate_only_refresh_id(cookie) | ||
self.assertEqual(1, user_id) | ||
|
||
def test_authenticate_without_cookie(self): | ||
user_id = self.auth_handler.authenticate_only_refresh_id(None) | ||
self.assertEqual(0, user_id) | ||
|
||
def test_authenticate_without_cookie_but_token(self): | ||
access_token, _ = self.fake_request.login() | ||
with self.assertRaises(jwt.exceptions.InvalidSignatureError): | ||
self.auth_handler.authenticate_only_refresh_id(access_token) | ||
|
||
def test_authenticate_without_cookie_but_request(self): | ||
self.fake_request.login() | ||
user_id = self.auth_handler.authenticate_only_refresh_id(None) | ||
self.assertEqual(0, user_id) | ||
|
||
def test_authenticate_with_expired_cookie(self): | ||
_, cookie = self.fake_request.login() | ||
cookie = cookie[7:] | ||
|
||
session_id = jwt.decode(cookie, self.environment.get_cookie_key(), algorithms=["HS256"])["sessionId"] | ||
expired_cookie_payload = { | ||
"sessionId": session_id, | ||
USER_ID_PROPERTY: 1, | ||
"exp": datetime.utcfromtimestamp(0) | ||
} | ||
raw_cookie = jwt.encode(expired_cookie_payload, self.environment.get_cookie_key(), algorithm="HS256") | ||
expired_cookie = "bearer " + raw_cookie.decode("utf-8") | ||
with self.assertRaises(jwt.exceptions.ExpiredSignatureError): | ||
self.auth_handler.authenticate_only_refresh_id(expired_cookie) |
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