Update auth flow
authorAdriaan Joubert <45142747+adriaanjoubert@users.noreply.github.com>
Sun, 23 Jun 2024 01:18:36 +0000 (02:18 +0100)
committerAdriaan Joubert <45142747+adriaanjoubert@users.noreply.github.com>
Sun, 23 Jun 2024 01:18:47 +0000 (02:18 +0100)
getmyancestors/classes/session.py

index 5fb2700ad7ae8d1e8c2c1b673057314567b9b95e..88b8c64b5dab7101e910642d2570ebe9701939fc 100644 (file)
@@ -1,6 +1,7 @@
 # global imports
 import sys
 import time
+from urllib.parse import urlparse, parse_qs
 
 import requests
 from fake_useragent import UserAgent
@@ -78,6 +79,37 @@ class Session(requests.Session):
                 self.write_log("Downloading: " + url)
                 res = self.get(url, headers=self.headers)
                 res.raise_for_status()
+
+                url = f"https://ident.familysearch.org/cis-web/oauth2/v3/authorization?response_type=code&scope=openid profile email qualifies_for_affiliate_account country&client_id=a02j000000KTRjpAAH&redirect_uri=https://misbach.github.io/fs-auth/index_raw.html&username={self.username}"
+                self.write_log("Downloading: " + url)
+                response = self.get(url, allow_redirects=False, headers=self.headers)
+                location = response.headers["location"]
+                code = parse_qs(urlparse(location).query).get("code")
+                url = "https://ident.familysearch.org/cis-web/oauth2/v3/token"
+                self.write_log("Downloading: " + url)
+                res = self.post(
+                    url,
+                    data={
+                        "grant_type": "authorization_code",
+                        "client_id": "a02j000000KTRjpAAH",
+                        "code": code,
+                        "redirect_uri": "https://misbach.github.io/fs-auth/index_raw.html",
+                    },
+                    headers=self.headers,
+                )
+
+                try:
+                    data = res.json()
+                except ValueError:
+                    self.write_log("Invalid auth request")
+                    continue
+
+                if "access_token" not in data:
+                    self.write_log(res.text)
+                    continue
+                access_token = data["access_token"]
+                self.headers.update({"Authorization": f"Bearer {access_token}"})
+
             except requests.exceptions.ReadTimeout:
                 self.write_log("Read timed out")
                 continue