Skip to content

Commit

Permalink
add webhookverify option
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestHC committed Jun 15, 2023
1 parent 16b46d6 commit 7759d7c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions nr/configs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Settings(BaseSettings):
channel_open_api_access_secret: StrictStr = ""
channel_group_id: StrictStr = ""
newreleases_webhook_verify_key: StrictStr = ""
newreleases_webhook_verify: bool = True


settings = Settings()
4 changes: 3 additions & 1 deletion nr/routers/new_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Annotated, List
from zoneinfo import ZoneInfo
from dateutil import parser
from ..configs.config import settings
from ..channel.client import ChannelOpenApiClient
from ..channel.message import Block, Message
from ..schemas.releasehook import ReleaseHook
Expand All @@ -19,7 +20,8 @@ def hook(
x_newreleases_timestamp: Annotated[str, Header()],
release: ReleaseHook
) -> Response:
if not verify_signature(x_newreleases_signature, x_newreleases_timestamp, release):
if settings.newreleases_webhook_verify and \
not verify_signature(x_newreleases_signature, x_newreleases_timestamp, release):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)

release_time = parser.parse(release.time) \
Expand Down
2 changes: 1 addition & 1 deletion nr/schemas/releasehook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Note(BaseModel):


class Account(BaseModel):
type:str
type: str
id: str
name: str

Expand Down
2 changes: 1 addition & 1 deletion nr/utils/new_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def verify_signature(signature: str, timestamp: str, release: ReleaseHook) -> bool:
secret: str = settings.newreleases_webhook_verify_key
message: str = f"{timestamp}.{''.join(release.json(exclude_unset=True, exclude_none=True).split())}"
message: str = f"{timestamp}.{''.join(release.json(exclude_unset=True).split())}"
_hmac: HMAC = hmac.new(
key=secret.encode(),
msg=message.encode(),
Expand Down

0 comments on commit 7759d7c

Please sign in to comment.