-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth2.py
405 lines (338 loc) · 13.8 KB
/
oauth2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""
OAuth2 Client Module.
"""
import os
import logging
import base64
import math
import textwrap
import json
import datetime
from authlib.integrations.requests_client import OAuth2Session
from authlib.integrations.base_client import OAuthError
import sentry_sdk
from utils import get_configs
OAUTH2_CONFIGURATIONS = {
"gmail": {
"urls": {
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"userinfo_uri": "https://www.googleapis.com/oauth2/v3/userinfo",
"send_message_uri": "https://www.googleapis.com/gmail/v1/users/{}/messages/send",
"revoke_uri": "https://oauth2.googleapis.com/revoke",
},
"default_params": {
"scope": [
"openid",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
],
"access_type": "offline",
"prompt": "consent",
},
},
"twitter": {
"urls": {
"auth_uri": "https://twitter.com/i/oauth2/authorize",
"token_uri": "https://api.twitter.com/2/oauth2/token",
"userinfo_uri": "https://api.twitter.com/2/users/me",
"send_message_uri": "https://api.twitter.com/2/tweets",
"revoke_uri": "https://api.twitter.com/2/oauth2/revoke",
},
"default_params": {
"scope": ["tweet.write", "users.read", "tweet.read", "offline.access"]
},
},
}
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def load_credentials(platform_name):
"""
Load client credentials from a JSON file specified in environment variables.
Args:
platform_name (str): The name of the platform (e.g., 'gmail').
Returns:
dict: A dictionary containing client_id, client_secret, and redirect_uri.
"""
creds_file = get_configs(f"{platform_name.upper()}_CREDENTIALS")
if not creds_file:
raise ValueError(
f"Missing environment variable for {platform_name.upper()}_CREDENTIALS"
)
with open(creds_file, "r", encoding="utf-8") as file:
creds = json.load(file)
def find_nested_credentials(data):
for key, value in data.items():
if isinstance(value, dict):
nested_creds = find_nested_credentials(value)
if nested_creds:
return nested_creds
elif key in ["client_id", "client_secret", "redirect_uri", "redirect_uris"]:
return data
return None
creds_data = find_nested_credentials(creds)
if not creds_data:
raise ValueError(
f"Credentials not found in the JSON file for platform: {platform_name}"
)
required_fields = {
"client_id": creds_data.get("client_id"),
"client_secret": creds_data.get("client_secret"),
"redirect_uris": creds_data.get("redirect_uris", []),
}
redirect_uri = required_fields["redirect_uris"][0]
return {
"client_id": required_fields["client_id"],
"client_secret": required_fields["client_secret"],
"redirect_uri": redirect_uri,
}
class OAuth2Client:
"""
OAuth2 client implementation using Authlib.
"""
def __init__(self, platform_name, token=None, update_token=None):
"""
Initialize the OAuth2Client.
Args:
platform_name (str): The name of the platform (e.g., 'gmail', 'twitter').
token (dict, optional): The OAuth 2.0 token containing 'access_token',
'refresh_token', 'expires_at', and other relevant token information.
Default is None.
update_token (callable, optional): A callable function that updates the
OAuth 2.0 token. Used when refreshing tokens. Default is None.
"""
oauth2_config = OAUTH2_CONFIGURATIONS.get(platform_name.lower())
if not oauth2_config:
raise ValueError(f"Configuration for platform '{platform_name}' not found.")
if token and not self._is_token_format_correct(token):
logger.info("Token format is incorrect. Converting token format...")
token = self._convert_token_format(token)
self.platform = platform_name
self.creds = load_credentials(self.platform)
self.urls = oauth2_config["urls"]
self.default_params = oauth2_config["default_params"]
self.session = OAuth2Session(
client_id=self.creds["client_id"],
client_secret=self.creds["client_secret"],
redirect_uri=self.creds["redirect_uri"],
token_endpoint=self.urls["token_uri"],
token=token,
update_token=update_token,
)
def _is_token_format_correct(self, token):
"""
Check if the token is already in the correct format.
Args:
token (dict): The token credentials.
Returns:
bool: True if the token is in the correct format, False otherwise.
"""
required_keys = {"access_token", "token_type", "expires_at", "refresh_token"}
return required_keys.issubset(token.keys())
def _convert_token_format(self, old_format_token):
"""
Convert token credentials from one format to another.
Args:
old_format_token (dict): The original token credentials.
Returns:
dict: The converted token credentials in the new format.
"""
access_token = old_format_token.get("token")
refresh_token = old_format_token.get("refresh_token")
scope = " ".join(old_format_token.get("scopes", []))
expiry_time = old_format_token.get("expiry")
if expiry_time:
expiry_datetime = datetime.datetime.fromisoformat(
expiry_time.replace("Z", "+00:00")
)
expires_at = int(expiry_datetime.timestamp())
else:
expires_at = None
new_format_token = {
"access_token": access_token,
"expires_in": 3599,
"scope": scope,
"token_type": "Bearer",
"id_token": "",
"expires_at": expires_at,
"refresh_token": refresh_token,
}
return new_format_token
@staticmethod
def generate_code_verifier(length=128):
"""
Generate a code verifier for PKCE.
Args:
length (int, optional): Length of the code verifier. Default is 128.
Returns:
str: The generated code verifier.
"""
code_verifier = base64.urlsafe_b64encode(os.urandom(length)).decode("utf-8")
return "".join(c for c in code_verifier if c.isalnum())
def get_authorization_url(self, autogenerate_code_verifier=False, **kwargs):
"""
Get the authorization URL.
Args:
autogenerate_code_verifier (bool, optional): Whether to auto-generate
a code verifier for PKCE. Default is False.
**kwargs: Additional parameters to include in the authorization URL.
Returns:
tuple: A tuple containing:
- authorization_url (str): The generated authorization URL.
- state (str): The state parameter for CSRF protection.
- code_verifier (str or None): The generated code verifier for PKCE if
applicable, otherwise None.
- client_id (str): The client ID for the OAuth2 application.
- scope (str): The scope of the authorization request, as a
comma-separated string.
- redirect_uri (str): The redirect URI for the OAuth2 application.
"""
code_verifier = kwargs.get("code_verifier")
if autogenerate_code_verifier and not code_verifier:
code_verifier = self.generate_code_verifier(48)
kwargs["code_verifier"] = code_verifier
self.session.code_challenge_method = "S256"
if code_verifier:
kwargs["code_verifier"] = code_verifier
self.session.code_challenge_method = "S256"
params = {**self.default_params, **kwargs}
authorization_url, state = self.session.create_authorization_url(
self.urls["auth_uri"], **params
)
logger.info("Authorization URL generated: %s", authorization_url)
return (
authorization_url,
state,
code_verifier,
self.creds["client_id"],
",".join(self.default_params["scope"]),
self.session.redirect_uri,
)
def fetch_token(self, code, **kwargs):
"""
Fetch the access token using the authorization code.
Args:
code (str): The authorization code.
**kwargs: Additional parameters for fetching the token.
Returns:
tuple: A tuple containing:
- The token response.
- scope (list): The scope of the authorization request.
"""
logger.debug("Fetching access token...")
token_response = self.session.fetch_token(
self.urls["token_uri"], code=code, **kwargs
)
logger.info("Access token fetched successfully.")
return token_response, self.default_params["scope"]
def fetch_userinfo(self):
"""
Fetch user information using the access token.
Returns:
dict: User information response.
"""
logger.debug("Fetching user information...")
userinfo = self.session.get(self.urls["userinfo_uri"]).json()
logger.info("User information fetched successfully.")
return userinfo
def revoke_token(self):
"""
Revoke the given OAuth2 token.
Returns:
dict: The response from the revocation endpoint.
"""
try:
refreshed_tokens = self.session.refresh_token(self.urls.get("token_uri"))
self.session.token = refreshed_tokens
response = self.session.revoke_token(
self.urls.get("revoke_uri"), token_type_hint="refresh_token"
)
if not response.ok:
response_data = response.text
logger.error(
"Failed to revoke tokens for %s: %s", self.platform, response_data
)
return response_data
response_data = response.json()
logger.info("Token revoked successfully.")
return response_data
except OAuthError as e:
logger.error("Error revoking OAuth2 token: %s", e)
return e
def send_message(self, message, user_id=None):
"""
Send a message.
Args:
message (dict): The message payload to be sent. The payload should be a
properly formatted dictionary according to the platform's specifications.
user_id (str, optional): The ID of the user to send the message on behalf of.
Returns:
dict: The response from the platform.
"""
url = (
self.urls["send_message_uri"].format(user_id)
if user_id
else self.urls["send_message_uri"]
)
if self.platform == "twitter":
return self._send_twitter_message(message, url)
return self._send_generic_message(message, url)
def _send_twitter_message(self, message, url):
def chunk_tweet(tweet, max_length=280):
tweet_length = len(tweet)
if tweet_length <= max_length:
return [tweet]
tweet_threads_required = math.ceil(tweet_length / max_length)
tweet_per_thread = math.ceil(tweet_length / tweet_threads_required)
return textwrap.wrap(tweet, tweet_per_thread, break_long_words=False)
def create_tweet_payload(text, in_reply_to_tweet_id=None):
payload = {"text": text}
if in_reply_to_tweet_id is not None:
payload["reply"] = {"in_reply_to_tweet_id": str(in_reply_to_tweet_id)}
return payload
tweets = chunk_tweet(message)
tweet_id = None
for chunk in tweets:
payload = create_tweet_payload(chunk, tweet_id)
response = self.session.post(url, json=payload)
if not response.ok:
response_data = response.text
logger.error(
"Failed to send message for %s: %s",
self.platform,
response_data,
)
return response_data
tweet_id = response.json()["data"]["id"]
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
publish_alert = (
f"Successfully sent message for '{self.platform}' at {timestamp}"
)
sentry_sdk.capture_message(
publish_alert,
level="info",
)
logger.info(publish_alert)
return f"Successfully sent message to '{self.platform}' on your behalf at {timestamp}."
def _send_generic_message(self, message, url):
response = self.session.post(url, json=message)
if not response.ok:
response_data = response.text
logger.error(
"Failed to send message for %s: %s", self.platform, response_data
)
return response_data
response_data = response.json()
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
publish_alert = (
f"Successfully sent message for '{self.platform}' at {timestamp}"
)
sentry_sdk.capture_message(
publish_alert,
level="info",
)
logger.info(publish_alert)
return f"Successfully sent message to '{self.platform}' on your behalf at {timestamp}."