-
Notifications
You must be signed in to change notification settings - Fork 3
Add subscribe API for COSCUP-Volunteer and API Token management page #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
orertrr
wants to merge
9
commits into
COSCUP:main
Choose a base branch
from
orertrr:subscribe-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21d5c0b
feat: add api for COSCUP-Volunteer
orertrr 2ed095d
feat: add token management page prototype
orertrr 21f51e4
feat: add API for accessing API tokens
orertrr 3d1280e
feat: add token management page
orertrr d137804
feat: Implement the process of verifying api token
orertrr 592e0c5
feat: add api for registering COSCUP-Volunteer user as subscriber
orertrr c2040b4
fix: return false when the token being verified has been removed
orertrr 1da6deb
style: fix pylint and mypy errors
orertrr f5fe522
style: fix import and blueprint order
orertrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| ''' APIToken DB ''' | ||
| from models.base import DBBase | ||
|
|
||
| class APITokenDB(DBBase): | ||
| ''' Token Collection | ||
|
|
||
| Schema: | ||
| { | ||
| serial_no: string, | ||
| token: string, | ||
| label: string | ||
| } | ||
| ''' | ||
| def __init__(self) -> None: | ||
| super().__init__('api_token') | ||
|
|
||
| def index(self) -> None: | ||
| ''' index ''' | ||
| self.create_index([('serial_no', 1)]) |
This file contains hidden or 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,8 +1,10 @@ | ||
| ''' index ''' | ||
| from models.api_token import APITokenDB | ||
| from models.subscriberdb import (SubscriberDB, SubscriberLoginTokenDB, | ||
| SubscriberReadDB) | ||
|
|
||
| if __name__ == '__main__': | ||
| APITokenDB().index() | ||
| SubscriberDB().index() | ||
| SubscriberLoginTokenDB().index() | ||
| SubscriberReadDB().index() |
This file contains hidden or 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,62 @@ | ||
| ''' API Token Module ''' | ||
| from typing import Any | ||
| from uuid import uuid4 | ||
| from dataclasses import dataclass, asdict, field | ||
|
|
||
| from passlib.context import CryptContext | ||
|
|
||
| from models.api_token import APITokenDB | ||
|
|
||
| @dataclass | ||
| class APITokenSchema: | ||
| ''' Schema of api_token collection ''' | ||
| token: str = field(default_factory=lambda: uuid4().hex) | ||
| serial_no: str = field(default_factory=lambda: f'{uuid4().node:08x}') | ||
| label: str = '' | ||
|
|
||
| class APIToken: | ||
| ''' Class for managing API tokens ''' | ||
| @staticmethod | ||
| def create(label: str) -> APITokenSchema: | ||
| ''' Create token ''' | ||
| new_token = APITokenSchema(label=label) | ||
|
|
||
| hash_context = CryptContext(schemes=['bcrypt'], deprecated='auto') | ||
| hashed_token = asdict(new_token) | ||
| hashed_token['token'] = hash_context.hash(hashed_token['token']) | ||
|
|
||
| APITokenDB().insert_one(hashed_token) | ||
|
|
||
| new_token.token = f'{new_token.serial_no}|{new_token.token}' | ||
|
|
||
| return new_token | ||
|
|
||
| @staticmethod | ||
| def get_list() -> list[dict[str, Any]]: | ||
| ''' Get list of token ''' | ||
| return list(APITokenDB().find({}, { 'label': 1, 'serial_no': 1, '_id': 0 })) | ||
|
|
||
| @staticmethod | ||
| def delete(tokens: list[str]) -> None: | ||
| ''' Delete the given token serial_no ''' | ||
| APITokenDB().delete_many({ | ||
| 'serial_no': { '$in': tokens } | ||
| }) | ||
|
|
||
| @staticmethod | ||
| def verify(token: str) -> bool: | ||
| ''' Check if the token exists and valid ''' | ||
| schema, token = token.split(' ') | ||
| if schema.lower() != 'bearer': | ||
| return False | ||
|
|
||
| serial_no, token = token.split('|') | ||
| hash_context = CryptContext(schemes=['bcrypt'], deprecated='auto') | ||
| hashed_token = APITokenDB().find_one({ | ||
| 'serial_no': serial_no | ||
| }) | ||
|
|
||
| if hashed_token is None: | ||
| return False | ||
|
|
||
| return hash_context.verify(token, hashed_token['token']) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@orertrr 這段移到
view/volunteer.py的每一個 endpoint 去判斷,這裡想要單純的扮演 route 的節點。然後在 Line 78 加入and not request.path.startswith('/volunteer')There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這部分如果包成 function 然後掛一個
VIEW_VOLUNTEER.before_request的 decorator 上去可以嗎?理論上這樣寫可以有同樣的效果,且之後
/volunteer底下有要再加 Endpoint 時也不用再寫同樣的邏輯