Skip to content

Commit

Permalink
Merge pull request #53 from drsanwujiang/dev
Browse files Browse the repository at this point in the history
Update to 4.1.2
  • Loading branch information
drsanwujiang authored Jul 2, 2024
2 parents 34f4e02 + 91f3588 commit 9419e9c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
19 changes: 14 additions & 5 deletions app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
import hmac

from fastapi import Request, Header
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.security import check_password_hash
import jwt

from .log import logger
from .config import status, settings
from .exceptions import TokenInvalidError, SignatureInvalidError


def generate_password(password: str) -> str:
return generate_password_hash(password)


def verify_password(password: str) -> bool:
if not settings.security.admin.password_hash:
return True
Expand All @@ -29,22 +26,31 @@ def generate_jwt_token() -> str:


def verify_jwt_token(authorization: str = Header()) -> None:
logger.debug("HTTP request received, verify JWT token")

scheme, _, token = authorization.partition(" ")

if not authorization or scheme != "Bearer" or not token:
logger.debug("JWT token verification failed, token not exists")
raise TokenInvalidError

try:
jwt.decode(token, settings.security.jwt.secret, settings.security.jwt.algorithm)
except jwt.InvalidTokenError:
logger.debug("JWT token verification failed, token invalid")
raise TokenInvalidError

logger.debug("JWT token verification passed")


async def verify_signature(
request: Request,
signature: str = Header(alias="X-Signature", min_length=45, max_length=45)
) -> None:
logger.debug("HTTP request received, verify signature")

if status.debug:
logger.debug("Signature verification passed, debug mode")
return

signature = signature[5:]
Expand All @@ -55,4 +61,7 @@ async def verify_signature(
).hex()

if signature != digest:
logger.debug("Signature verification failed, signature invalid")
raise SignatureInvalidError

logger.debug("Signature verification passed")
4 changes: 4 additions & 0 deletions app/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ def start(cls) -> None:
with open(os.path.join(cls.config_dir, f"napcat_{settings.napcat.account}.json"), "w") as f:
json.dump(cls.napcat_config, f)

cls.onebot_config["http"]["host"] = str(settings.napcat.api.host)
cls.onebot_config["http"]["port"] = settings.napcat.api.port
cls.onebot_config["http"]["secret"] = settings.security.webhook.secret

with open(os.path.join(cls.config_dir, "onebot11.json"), "w") as f:
json.dump(cls.onebot_config, f)

Expand Down
10 changes: 5 additions & 5 deletions app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ def update_security(cls, settings: dict) -> None:
settings: New security settings.
"""

old_settings = cls._settings.security.model_dump()
security_settings = cls._settings.security.model_dump()

if "webhook" in settings:
old_settings["webhook"] = deep_update(old_settings["webhook"], settings["webhook"])
security_settings["webhook"] = deep_update(security_settings["webhook"], settings["webhook"])
if "jwt" in settings:
old_settings["jwt"] = deep_update(old_settings["jwt"], settings["jwt"])
security_settings["jwt"] = deep_update(security_settings["jwt"], settings["jwt"])
if "admin" in settings:
old_settings["admin"]["password_hash"] = generate_password_hash(settings["admin"]["password"])
security_settings["admin"]["password_hash"] = generate_password_hash(settings["admin"]["password"])

cls._settings.security = cls._Settings.Security.model_validate(old_settings)
cls._settings.security = cls._Settings.Security.model_validate(security_settings)

@classmethod
def update_application(cls, settings: dict) -> None:
Expand Down
7 changes: 1 addition & 6 deletions app/router/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ async def message_report(content: dict) -> EmptyResponse:
handle_message(PrivateMessage.model_validate(content))
case MessageType.GROUP:
handle_message(GroupMessage.model_validate(content))
case _:
raise MessageInvalidError
case ReportType.META_EVENT:
pass
case ReportType.NOTICE:
Expand Down Expand Up @@ -71,13 +69,10 @@ async def message_report(content: dict) -> EmptyResponse:
handle_event(FriendAddRequest.model_validate(content))
case RequestType.GROUP:
handle_event(GroupAddRequest.model_validate(content))
case _:
raise MessageInvalidError
case _:
raise MessageInvalidError

logger.info("Report completed")
except ValueError:
logger.warning("Report finished, message invalid")
raise MessageInvalidError
except RuntimeError:
logger.info("Report filtered")
Expand Down
2 changes: 1 addition & 1 deletion app/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "4.1.1"
VERSION = "4.1.2"
8 changes: 8 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def __call__(self) -> None:

pass

def save_plugin_settings(self) -> None:
"""Save plugin settings.
Plugin settings must be saved explicitly to avoid inappropriate modification.
"""

plugin_settings.set(plugin=self.name, settings=self.plugin_settings)


class OrderPlugin(DiceRobotPlugin):
"""DiceRobot order plugin.
Expand Down
5 changes: 4 additions & 1 deletion plugin/dicerobot/order/daily_60s.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime

from plugin import OrderPlugin
from app.config import plugin_settings
from app.schedule import scheduler
from app.exceptions import OrderInvalidError, OrderError
from app.enum import ChatType
Expand All @@ -12,7 +13,7 @@ class DailySixtySeconds(OrderPlugin):
name = "dicerobot.daily_60s"
display_name = "每天60秒读懂世界"
description = "每天60秒读懂世界,15条简报+1条微语,让你瞬间了解世界正在发生的大事"
version = "1.1.0"
version = "1.1.1"

default_plugin_settings = {
"api": "https://api.2xb.cn/zaob",
Expand Down Expand Up @@ -55,9 +56,11 @@ def __call__(self) -> None:

if self.chat_id not in self.plugin_settings["subscribers"]:
self.plugin_settings["subscribers"].append(self.chat_id)
self.save_plugin_settings()
self.reply_to_sender(self.replies["subscribe"])
else:
self.plugin_settings["subscribers"].remove(self.chat_id)
self.save_plugin_settings()
self.reply_to_sender(self.replies["unsubscribe"])

def check_order_content(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dicerobot"
version = "4.1.1"
version = "4.1.2"
description = "A TRPG assistant bot"
authors = ["Drsanwujiang <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 9419e9c

Please sign in to comment.