-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication_manager.py
100 lines (72 loc) · 3.01 KB
/
authentication_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from retrying import retry
from gql import gql
from logger import logger
from queries import login_mutation, logout_mutation, get_current_session_query
import jwt
from config import ENDPOINT
import os
import requests
from dotenv import load_dotenv
load_dotenv()
class AuthenticationManager:
def __init__(self, client):
self.client = client
self.token = None # Initialize the token attribute
@retry(wait_exponential_multiplier=3000, wait_exponential_max=10000, stop_max_attempt_number=3, retry_on_exception=lambda e: True)
def execute(self, query, variables=None, attempt=1):
gql_query = gql(query)
try:
result = self.client.execute(gql_query, variable_values=variables)
if attempt > 1:
logger.info(f"Query succeeded on attempt {attempt}.")
return result
except Exception as e:
logger.error(f"Error executing query on attempt {attempt}: {str(e)}")
if attempt < 3: # If it's not the last attempt
return self.execute(query, variables, attempt + 1)
raise
def refresh_session(self):
# Fetching the SN_AUTH_COOKIE from environment variables
sn_auth_cookie = os.environ.get('SN_AUTH_COOKIE')
if not sn_auth_cookie:
raise ValueError("SN_AUTH_COOKIE not found in environment variables!")
# Stacker News URL for refreshing the session
sn_url = "https://stacker.news/api/auth/session"
# Preparing the request headers
headers = {
"Cookie": sn_auth_cookie
}
# Making the GET request to refresh the session
response = requests.get(sn_url, headers=headers)
# Handling the response
if response.status_code != 200:
raise Exception(f"Error refreshing SN session: {response.text}")
return response.text # or return True/None based on your preference
def get_current_session(self):
return self.execute(get_current_session_query)
def login(self, k1, sig):
variables = {
"k1": k1,
"sig": sig
}
response = self.execute(login_mutation, variables)
if response.get("lnurlauth", {}).get("ok"):
self.token = response["lnurlauth"]["jwt"]
self._update_transport_with_token()
else:
raise Exception(response["lnurlauth"]["error"])
return response
def _update_transport_with_token(self):
headers = {"Authorization": f"Bearer {self.token}"}
transport = AIOHTTPTransport(url=ENDPOINT, headers=headers)
self.client = Client(transport=transport, fetch_schema_from_transport=False)
def validate_token(self):
if not self.token:
return False
try:
decoded_token = jwt.decode(self.token, options={"verify_signature": False})
return True
except jwt.ExpiredSignatureError:
return False
def logout(self):
return self.execute(logout_mutation)