From 39f9ba31ede7dbd83212019daed5eedff27cefe8 Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Wed, 6 Dec 2023 09:53:06 +0000
Subject: [PATCH] Raise LoginNeeded if not logged in

---
 add_links/wikidata_oauth.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/add_links/wikidata_oauth.py b/add_links/wikidata_oauth.py
index d91b66e..9b4de72 100644
--- a/add_links/wikidata_oauth.py
+++ b/add_links/wikidata_oauth.py
@@ -1,7 +1,10 @@
+"""Wikipedia OAuth."""
+
 import typing
 import urllib
 from typing import cast
 
+import requests
 from flask import current_app, session
 from requests_oauthlib import OAuth1Session
 
@@ -9,6 +12,12 @@ wiki_hostname = "en.wikipedia.org"
 api_url = f"https://{wiki_hostname}/w/api.php"
 
 
+class LoginNeeded(Exception):
+    """Not logged in."""
+
+    pass
+
+
 def get_edit_proxy() -> dict[str, str]:
     """Retrieve proxy information from config."""
     edit_proxy = current_app.config.get("EDIT_PROXY")
@@ -18,7 +27,7 @@ def get_edit_proxy() -> dict[str, str]:
         return {}
 
 
-def api_post_request(params: dict[str, str | int]):
+def api_post_request(params: dict[str, str | int]) -> requests.Response:
     """HTTP Post using Oauth."""
     app = current_app
     # url = "https://www.wikidata.org/w/api.php"
@@ -34,12 +43,14 @@ def api_post_request(params: dict[str, str | int]):
     return oauth.post(api_url, data=params, timeout=4, proxies=proxies)
 
 
-def raw_request(params: typing.Mapping[str, str | int]):
+def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response:
     """Low-level API request."""
     app = current_app
     # url = "https://www.wikidata.org/w/api.php?" + urlencode(params)
     client_key = app.config["CLIENT_KEY"]
     client_secret = app.config["CLIENT_SECRET"]
+    if "owner_key" not in session or "owner_secret" not in session:
+        raise LoginNeeded
     oauth = OAuth1Session(
         client_key,
         client_secret=client_secret,