-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
21d5c0b
2ed095d
21f51e4
3d1280e
d137804
592e0c5
c2040b4
1da6deb
f5fe522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
| import google_auth_oauthlib.flow | ||
| from apiclient import discovery | ||
| from flask import (Flask, g, got_request_exception, redirect, render_template, | ||
| request, session, url_for) | ||
| request, session, url_for, make_response) | ||
| from flask.wrappers import Response | ||
| from werkzeug.wrappers import Response as ResponseBase | ||
|
|
||
|
|
@@ -21,6 +21,9 @@ | |
| from view.subscribe import VIEW_SUBSCRIBE | ||
| from view.subscriber import VIEW_SUBSCRIBER | ||
| from view.trello import VIEW_TRELLO | ||
| from view.volunteer import VIEW_VOLUNTEER | ||
| from view.token import VIEW_TOKEN | ||
| from module.api_token import APIToken | ||
|
|
||
| logging.basicConfig( | ||
| filename='./log/log.log', | ||
|
|
@@ -38,6 +41,8 @@ | |
| app.register_blueprint(VIEW_SUBSCRIBE) | ||
| app.register_blueprint(VIEW_SUBSCRIBER) | ||
| app.register_blueprint(VIEW_TRELLO) | ||
| app.register_blueprint(VIEW_VOLUNTEER) | ||
| app.register_blueprint(VIEW_TOKEN) | ||
|
|
||
| if app.debug: | ||
| app.config['TEMPLATES_AUTO_RELOAD'] = True | ||
|
|
@@ -60,6 +65,16 @@ def need_login() -> ResponseBase | None: | |
| request.headers.get('USER-AGENT'), | ||
| session, ) | ||
|
|
||
| if request.path.startswith('/volunteer'): | ||
| token = request.headers.get('Authorization') | ||
| if token is None: | ||
| return make_response('Unauthorized', 401) | ||
|
|
||
| if APIToken.verify(token) is True: | ||
| return None | ||
|
|
||
| return make_response('Unauthorized', 401) | ||
|
|
||
|
Comment on lines
+68
to
+77
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @orertrr 這段移到
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這部分如果包成 function 然後掛一個 理論上這樣寫可以有同樣的效果,且之後 |
||
| if request.path not in NO_NEED_LOGIN_PATH \ | ||
| and not request.path.startswith('/subscriber') \ | ||
| and not request.path.startswith('/subscribe') \ | ||
|
|
||
| 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)]) |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這個檔案上方 import 好像沒有排序,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修正 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| ''' index ''' | ||
| from models.subscriberdb import (SubscriberDB, SubscriberLoginTokenDB, | ||
| SubscriberReadDB) | ||
| from models.api_token import APITokenDB | ||
|
|
||
| if __name__ == '__main__': | ||
| SubscriberDB().index() | ||
| SubscriberLoginTokenDB().index() | ||
| SubscriberReadDB().index() | ||
|
|
||
| APITokenDB().index() |
| 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.
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.
Line 39 - Line 45 這裡排序一下。
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.
已修正