-
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.
Merge pull request #19 from t3tra-dev/feat/oauth
add OAuthManager
- Loading branch information
Showing
9 changed files
with
516 additions
and
10 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .auth_manager import AuthManager | ||
from .manager import AuthManager | ||
from .mixins import UserMixin | ||
|
||
__all__ = ["AuthManager", "UserMixin"] |
File renamed without changes.
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,4 @@ | ||
from .manager import OAuthManager | ||
from .mixins import UserMixin | ||
|
||
__all__ = ['OAuthManager', 'UserMixin'] |
Large diffs are not rendered by default.
Oops, something went wrong.
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,28 @@ | ||
""" | ||
This module defines models and mixins for the OAuth 2.0 implementation. | ||
""" | ||
|
||
from typing import Protocol | ||
|
||
__all__ = ["UserMixin"] | ||
|
||
|
||
class UserMixin(Protocol): | ||
""" | ||
Provides default implementations for methods that a user class should have. | ||
""" | ||
|
||
def get_id(self) -> str: | ||
""" | ||
Return the unique identifier of the user as a string. | ||
""" | ||
raise NotImplementedError( | ||
"Method 'get_id' must be implemented to return a unique identifier for the user" | ||
) | ||
|
||
@property | ||
def is_authenticated(self) -> bool: | ||
""" | ||
Return True if the user is authenticated. | ||
""" | ||
return True |
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,70 @@ | ||
# app.py | ||
|
||
from typing import Any, Dict, Optional | ||
from haru import Haru, Request, Response | ||
from haru.oauth import OAuthManager, UserMixin | ||
|
||
app = Haru(__name__) | ||
|
||
|
||
oauth_manager = OAuthManager(secret_key='your-secret-key') | ||
oauth_manager.init_app(app) | ||
|
||
|
||
class User(UserMixin): | ||
def __init__(self, user_id: str, username: str): | ||
self.id = user_id | ||
self.username = username | ||
|
||
def get_id(self) -> str: | ||
return self.id | ||
|
||
|
||
@oauth_manager.user_loader | ||
def load_user(user_id: str) -> Optional[User]: | ||
if user_id == '1': | ||
return User(user_id='1', username='john') | ||
return None | ||
|
||
|
||
@oauth_manager.client_loader | ||
def load_client(client_id: str) -> Optional[Dict[str, Any]]: | ||
if client_id == 'client_123': | ||
return { | ||
'client_id': 'client_123', | ||
'client_secret': 'secret_456', | ||
'redirect_uris': ['http://localhost:8000/callback'], | ||
'intents': ['read', 'write'], | ||
} | ||
return None | ||
|
||
|
||
@app.route('/login', methods=['GET', 'POST']) | ||
def login(request: Request): | ||
if request.method == 'GET': | ||
return Response('Login Form', content_type='text/html') | ||
elif request.method == 'POST': | ||
username = request.form.get('username') | ||
password = request.form.get('password') | ||
if username == 'john' and password == 'secret': | ||
user = User(user_id='1', username='john') | ||
oauth_manager.login(request, user) | ||
return Response('Logged in') | ||
else: | ||
return Response('Invalid credentials', status_code=401) | ||
|
||
|
||
@app.route('/logout') | ||
def logout(request: Request): | ||
oauth_manager.logout(request) | ||
return Response('Logged out') | ||
|
||
|
||
@app.route('/protected') | ||
@oauth_manager.login_require(intents=['read']) | ||
def protected_resource(request: Request): | ||
user = request.current_user | ||
return Response(f'Hello, {user.username}!') | ||
|
||
|
||
app.run() |
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,15 @@ | ||
rm -rf build | ||
rm -rf dist | ||
python -m venv .build-venv | ||
source .build-venv/bin/activate | ||
pip install -U setuptools | ||
python setup.py sdist bdist_wheel | ||
rm -rf .build-venv | ||
|
||
python3 -m venv .test-venv | ||
source .test-venv/bin/activate | ||
pip install -e . | ||
clear | ||
cd tests | ||
python oauth.py | ||
rm -rf .test-venv |