kenken999 commited on
Commit
64db884
·
1 Parent(s): 2f6a509
Files changed (1) hide show
  1. mysite/asgi.py +25 -8
mysite/asgi.py CHANGED
@@ -624,9 +624,9 @@ demo4 = gr.ChatInterface(
624
  logging.basicConfig(level=logging.INFO)
625
  logger = logging.getLogger(__name__)
626
 
627
- CHANNEL_ID = os.getenv('CHANNEL_ID')
628
- CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
629
- CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
630
  WEBHOOK_URL = os.getenv('WEBHOOK_URL')
631
  import requests
632
  import hmac
@@ -638,6 +638,16 @@ def validate_signature(body: str, signature: str, secret: str) -> bool:
638
  return hmac.compare_digest(expected_signature, signature)
639
 
640
 
 
 
 
 
 
 
 
 
 
 
641
  @app.post("/webhook")
642
  async def webhook(request: Request):
643
  try:
@@ -655,15 +665,24 @@ async def webhook(request: Request):
655
  raise HTTPException(status_code=400, detail="X-Line-Signature header is missing.")
656
 
657
  # 署名を検証
658
- if not validate_signature(body.decode('utf-8'), line_signature, os.getenv('CHANNEL_SECRET')):
659
  raise HTTPException(status_code=400, detail="Invalid signature.")
660
 
 
 
 
 
661
  # 送信するヘッダーを設定
662
- headers = {key: value for key, value in received_headers.items()}
663
- headers['Authorization'] = f'Bearer {CHANNEL_ACCESS_TOKEN}' # 認証トークンを追加
 
 
 
664
 
665
  # ログに転送先URLを記録
666
  logger.info('Forwarding to URL: %s', WEBHOOK_URL)
 
 
667
 
668
  # データを転送
669
  response = requests.post(WEBHOOK_URL, headers=headers, data=body)
@@ -680,8 +699,6 @@ async def webhook(request: Request):
680
  logger.error("Error: %s", str(e))
681
  raise HTTPException(status_code=500, detail=str(e))
682
 
683
-
684
-
685
  def do_something_to_file(file_path):
686
  # ファイルに対して実行する処理をここに記述
687
  with open(file_path, "r") as f:
 
624
  logging.basicConfig(level=logging.INFO)
625
  logger = logging.getLogger(__name__)
626
 
627
+ CHANNEL_ID = os.getenv('ChannelID')
628
+ CHANNEL_SECRET = os.getenv('ChannelSecret')
629
+ CHANNEL_ACCESS_TOKEN = os.getenv('ChannelAccessToken')
630
  WEBHOOK_URL = os.getenv('WEBHOOK_URL')
631
  import requests
632
  import hmac
 
638
  return hmac.compare_digest(expected_signature, signature)
639
 
640
 
641
+
642
+ def validate_signature(body: str, signature: str, secret: str) -> bool:
643
+ if secret is None:
644
+ logger.error("Secret is None")
645
+ return False
646
+
647
+ hash = hmac.new(secret.encode('utf-8'), body.encode('utf-8'), hashlib.sha256).digest()
648
+ expected_signature = base64.b64encode(hash).decode('utf-8')
649
+ return hmac.compare_digest(expected_signature, signature)
650
+
651
  @app.post("/webhook")
652
  async def webhook(request: Request):
653
  try:
 
665
  raise HTTPException(status_code=400, detail="X-Line-Signature header is missing.")
666
 
667
  # 署名を検証
668
+ if not validate_signature(body.decode('utf-8'), line_signature, CHANNEL_SECRET):
669
  raise HTTPException(status_code=400, detail="Invalid signature.")
670
 
671
+ # URLの検証
672
+ if not WEBHOOK_URL or not WEBHOOK_URL.startswith("https://"):
673
+ raise HTTPException(status_code=400, detail="Invalid webhook URL")
674
+
675
  # 送信するヘッダーを設定
676
+ headers = {
677
+ 'Content-Type': 'application/json',
678
+ 'X-Line-Signature': line_signature,
679
+ 'Authorization': f'Bearer {CHANNEL_ACCESS_TOKEN}'
680
+ }
681
 
682
  # ログに転送先URLを記録
683
  logger.info('Forwarding to URL: %s', WEBHOOK_URL)
684
+ logger.info('Forwarding Headers: %s', headers)
685
+ logger.info('Forwarding Body: %s', body.decode('utf-8'))
686
 
687
  # データを転送
688
  response = requests.post(WEBHOOK_URL, headers=headers, data=body)
 
699
  logger.error("Error: %s", str(e))
700
  raise HTTPException(status_code=500, detail=str(e))
701
 
 
 
702
  def do_something_to_file(file_path):
703
  # ファイルに対して実行する処理をここに記述
704
  with open(file_path, "r") as f: