-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.py
297 lines (250 loc) · 10.3 KB
/
services.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
import json
import os
from datetime import datetime
from urllib.parse import urlencode, unquote
import httpx
from jose import jwt
from sqlalchemy import select, and_, or_
from sqlalchemy.ext.asyncio import AsyncSession
from src.auth.model import Account
from src.auth.service import get_password_hash
from src.user.model import User
async def make_request(method, url, params=None, data=None, headers=None):
async with httpx.AsyncClient() as client:
if method == 'GET':
response = await client.get(url, params=params)
elif method == 'POST':
response = await client.post(url, data=data, headers=headers)
# Другие методы HTTP: PUT, DELETE и т.д.
else:
raise ValueError(f"Неподдерживаемый метод HTTP: {method}")
response.raise_for_status()
return response.json()
class AsyncBaseUserOAuth:
"""
Base class for user registration via social networks
"""
def __init__(self, code: str, session: AsyncSession):
self.code = code # Cross-site request forgery
self.session = session
self.client_id = None
self.client_secret = None
self.redirect_uri = None
self.ctrate_user = False
self.access_token = None
self.email = None
self.name = None
self.surname = None
self.city = None
self.valid = False
self.error_massage = None
self.uid = None
self.user = None
self.account = None
def clean_data(self):
data = {
'email': self.email,
'first_name': self.surname,
'confirmationed_email': True
}
valid_data = {key: value for key, value in data.items() if value}
return valid_data
async def account_exists(self):
async with self.session.begin():
stmt = select(Account).where(
or_(
Account.email == self.email,
and_(
Account.open_id == self.uid,
Account.type == self.provider
)
)
)
result = await self.session.execute(stmt)
user = result.scalar_one_or_none()
return user is not None
async def get_or_create_user(self):
"""Create user in the database"""
account_exists = await self.account_exists()
if not account_exists:
async with self.session.begin():
user = User(name=self.name, surname=self.surname)
self.session.add(user) # Save the user in the session
await self.session.flush() # Generate the user ID
account = Account(
email=self.email, open_id=self.uid, type=self.provider,
password=get_password_hash('password'), user_id=user.id
)
self.ctrate_user = True
self.account = account
self.session.add(account) # Save the account in the session
await self.session.flush() # Generate the account ID
else:
query = select(Account).where(
or_(
Account.email == self.email,
and_(
Account.open_id == self.uid,
Account.type == self.provider
)
)
)
result = await self.session.execute(query)
account = result.scalar_one_or_none() # Fetch a single result or None
self.account = account
self.valid = True
def update_profile(self):
pass
class AsyncFBUserOAuth(AsyncBaseUserOAuth):
"""
Class for authorization in Facebook
"""
provider = 'Facebook'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = os.environ.get("SOCIAL_AUTH_FACEBOOK_KEY")
self.client_secret = os.environ.get("SOCIAL_AUTH_FACEBOOK_SECRET")
self.redirect_uri = os.environ.get("SOCIAL_AUTH_FACEBOOK_REDIRECT_URL")
async def get_account(self):
params = {
'client_id': self.client_id,
'client_secret': self.client_secret,
'redirect_uri': self.redirect_uri,
'code': self.code}
uri = f'https://graph.facebook.com/v17.0/oauth/access_token'
result = await make_request("GET", uri, params=params)
self.access_token = result.get('access_token')
await self.get_user_id()
async def get_user_id(self):
params = {'access_token': self.access_token}
uri = 'https://graph.facebook.com/me'
result = await make_request("GET", uri, params=params)
self.uid = result.get('id')
self.name = result.get('name')
await self.provider_profile()
async def provider_profile(self):
"""
method gets user information from facebook
"""
params = {
'fields': 'id,email,birthday,first_name,gender,last_name,middle_name,picture,hometown',
'access_token': self.access_token}
uri = f'https://graph.facebook.com/{self.uid}'
response = await make_request("GET", uri, params=params)
data = response
if data is None:
return
self.email = data.get('email')
self.name = data.get('first_name')
self.surname = data.get('last_name')
await self.get_or_create_user()
class AsyncGoogleUserOAuth(AsyncBaseUserOAuth):
"""
Class for authorization in Google
"""
provider = 'Google'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = os.environ.get("SOCIAL_AUTH_GOOGLE_CLIENT_ID")
self.client_secret = os.environ.get("SOCIAL_AUTH_GOOGLE_SECRET")
self.redirect_uri = os.environ.get("SOCIAL_AUTH_GOOGLE_REDIRECT_URL")
async def get_account(self):
url = "https://www.googleapis.com/oauth2/v4/token"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {
'client_id': self.client_id,
'redirect_uri': self.redirect_uri,
'client_secret': self.client_secret,
'grant_type': 'authorization_code',
'code': self.code}
result = await make_request("POST", url, headers=headers, data=urlencode(payload))
self.access_token = result.get('access_token')
await self.get_user_id()
async def get_user_id(self):
params = {'access_token': self.access_token, 'alt': 'json'}
uri = 'https://www.googleapis.com/oauth2/v1/userinfo'
data = await make_request("GET", uri, params=params)
if data is None:
return
self.uid = data.get('id')
self.name = data.get('name')
self.surname = data.get('family_name')
self.email = data.get('email')
await self.get_or_create_user()
class AsyncAppleUserOAuth(AsyncBaseUserOAuth):
"""
Class for authorization in Apple
"""
provider = 'Apple'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = os.environ.get("SOCIAL_AUTH_APPLE_KEY")
self.team_id = os.environ.get("SOCIAL_AUTH_APPLE_TEAM_ID")
self.client_secret = os.environ.get("SOCIAL_AUTH_APPLE_SECRET")
self.redirect_uri = os.environ.get("SOCIAL_AUTH_APPLE_REDIRECT_URL")
async def _create_jwt(self):
claims = {
'iss': self.team_id,
'aud': 'https://appleid.apple.com',
'sub': self.client_id,
'iat': datetime.now().timestamp(),
'exp': datetime.now().timestamp() + 5 * 60,
}
headers = {'kid': 'APPLEID_KEY_ID', 'alg': 'ES256'}
client_secret = jwt.encode(payload=claims, key=self.client_secret, algorithm='ES256', headers=headers).decode(
'utf-8')
@staticmethod
def get_apple_public_key():
return make_request("GET", "https://appleid.apple.com/auth/keys")
async def get_authorization_code_apple(self, data):
uri = "https://appleid.apple.com/auth/token"
payload = {
'client_id': self.client_id,
'client_secret': self._create_jwt,
'grant_type': 'authorization_code',
'code': data['code']}
return await make_request("POST", uri, data=urlencode(payload))
async def get_account(self, data):
self.access_token = self.get_authorization_code_apple(data)
if self.access_token['access_token']:
spublic_key = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(self.get_apple_public_key))
data = jwt.decode(self.access_token['id_token'],
self.public_key,
algorithm="RS256",
verify=True,
audience=self.client_id,
)
self.uid = data['sub']
self.name = data['email']
self.surname = data['name']
await self.get_or_create_user()
return ""
def fb_authorize_client_url():
endpoint = 'https://www.facebook.com/v17.0/dialog/oauth?'
params = {
'client_id': os.environ.get("SOCIAL_AUTH_FACEBOOK_KEY"),
'redirect_uri': os.environ.get("SOCIAL_AUTH_FACEBOOK_REDIRECT_URL"),
'state': "{st=statsssse123addsasdxczxczbc,ds=123456789}",
}
return endpoint + unquote(urlencode(params))
def google_authorize_client_url():
endpoint = 'https://accounts.google.com/o/oauth2/v2/auth?'
params = {
'client_id': os.environ.get("SOCIAL_AUTH_GOOGLE_CLIENT_ID"),
'redirect_uri': os.environ.get("SOCIAL_AUTH_GOOGLE_REDIRECT_URL"),
'scope': 'openid%20profile%20email',
'response_type': 'code',
'access_type': 'offline',
'include_granted_scopes': 'true',
'state': 'state_parameter_passthrough_value'}
return endpoint + unquote(urlencode(params))
def apple_authorize_client_url():
endpoint = 'https://appleid.apple.com/auth/authorize?'
params = {
'client_id': os.environ.get("SOCIAL_AUTH_APPLE_CLIENT_ID"),
'redirect_uri': os.environ.get("SOCIAL_AUTH_APPLE_REDIRECT_URL"),
'scope': 'email+name',
'response_mode': 'form_post',
'include_granted_scopes': 'true',
'state': 'state_parameter_passthrough_value'}
return endpoint + unquote(urlencode(params))