Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
A prog2-höz tartozó friss repo anyagok itt elérhetőek:
https://git.iit.bme.hu/
Commit
d8264cd8
authored
Mar 10, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
linter
parent
e2461572
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
241 additions
and
78 deletions
+241
-78
.gitignore
+2
-0
balancer/util.py
+11
-11
core/auth.py
+9
-11
core/bearer.py
+16
-13
core/models.py
+6
-9
main.py
+12
-22
poetry.lock
+168
-1
pyproject.toml
+3
-1
sredis/models.py
+2
-1
sredis/sredis.py
+12
-9
No files found.
.gitignore
View file @
d8264cd8
# Python bytecode:
# Python bytecode:
*.py[co]
*.py[co]
.tokens
.tokens
.env
.ruff_cache/
# Packaging files:
# Packaging files:
*.egg*
*.egg*
...
...
balancer/util.py
View file @
d8264cd8
...
@@ -4,22 +4,23 @@ from fastapi import HTTPException
...
@@ -4,22 +4,23 @@ from fastapi import HTTPException
import
requests
import
requests
import
json
import
json
def
proxy_datacenters
(
serverpath
:
str
,
username
,
method
=
"GET"
,
balancer_fun
=
rr_get
):
def
proxy_datacenters
(
serverpath
:
str
,
username
,
method
=
"GET"
,
balancer_fun
=
rr_get
):
server
=
balancer_fun
()
server
=
balancer_fun
()
token
=
get_datacenter_token
(
username
,
server
)
token
=
get_datacenter_token
(
username
,
server
)
url
=
f
"{server}/{serverpath}"
url
=
f
"{server}/{serverpath}"
t_resp
=
requests
.
request
(
t_resp
=
requests
.
request
(
method
=
method
,
method
=
method
,
url
=
url
,
url
=
url
,
allow_redirects
=
False
,
verify
=
False
,
allow_redirects
=
False
,
headers
=
{
verify
=
False
,
'Authorization'
:
token
headers
=
{
"Authorization"
:
token
},
}
)
)
if
t_resp
.
status_code
/
100
!=
2
:
if
t_resp
.
status_code
/
100
!=
2
:
raise
HTTPException
(
status_code
=
t_resp
.
status_code
,
detail
=
"Remote server error"
)
raise
HTTPException
(
status_code
=
t_resp
.
status_code
,
detail
=
"Remote server error"
)
response
=
ORJSONResponse
(
response
=
ORJSONResponse
(
json
.
loads
(
t_resp
.
content
),
json
.
loads
(
t_resp
.
content
),
status_code
=
t_resp
.
status_code
status_code
=
t_resp
.
status_code
)
)
return
response
return
response
\ No newline at end of file
core/auth.py
View file @
d8264cd8
...
@@ -3,6 +3,7 @@ from typing import Dict
...
@@ -3,6 +3,7 @@ from typing import Dict
import
jwt
import
jwt
from
passlib.hash
import
pbkdf2_sha256
from
passlib.hash
import
pbkdf2_sha256
from
decouple
import
config
from
decouple
import
config
from
fastapi
import
HTTPException
JWT_SECRET
=
config
(
"secret"
)
JWT_SECRET
=
config
(
"secret"
)
...
@@ -10,25 +11,23 @@ JWT_ALGORITHM = config("algorithm")
...
@@ -10,25 +11,23 @@ JWT_ALGORITHM = config("algorithm")
def
token_response
(
token
:
str
):
def
token_response
(
token
:
str
):
return
{
return
{
"access_token"
:
token
}
"access_token"
:
token
}
def
signJWT
(
user_id
:
str
)
->
Dict
[
str
,
str
]:
def
signJWT
(
user_id
:
str
)
->
Dict
[
str
,
str
]:
payload
=
{
payload
=
{
"user_id"
:
user_id
,
"expires"
:
time
.
time
()
+
60000
}
"user_id"
:
user_id
,
"expires"
:
time
.
time
()
+
60000
}
token
=
jwt
.
encode
(
payload
,
JWT_SECRET
,
algorithm
=
JWT_ALGORITHM
)
token
=
jwt
.
encode
(
payload
,
JWT_SECRET
,
algorithm
=
JWT_ALGORITHM
)
return
token_response
(
token
)
return
token_response
(
token
)
def
decodeJWT
(
token
:
str
)
->
dict
:
def
decodeJWT
(
token
:
str
)
->
dict
:
try
:
try
:
decoded_token
=
jwt
.
decode
(
token
,
JWT_SECRET
,
algorithms
=
[
JWT_ALGORITHM
])
decoded_token
=
jwt
.
decode
(
token
,
JWT_SECRET
,
algorithms
=
[
JWT_ALGORITHM
])
return
decoded_token
if
decoded_token
[
"expires"
]
>=
time
.
time
()
else
None
return
decoded_token
if
decoded_token
[
"expires"
]
>=
time
.
time
()
else
None
except
:
except
jwt
.
ExpiredSignatureError
:
return
{}
raise
HTTPException
(
status_code
=
501
,
detail
=
"JWT token decode error"
)
def
hash_pass
(
password
:
str
)
->
str
:
def
hash_pass
(
password
:
str
)
->
str
:
return
pbkdf2_sha256
.
hash
(
password
)
return
pbkdf2_sha256
.
hash
(
password
)
\ No newline at end of file
core/bearer.py
View file @
d8264cd8
...
@@ -6,18 +6,23 @@ from sredis.models import PUser
...
@@ -6,18 +6,23 @@ from sredis.models import PUser
class
JWTBearer
(
HTTPBearer
):
class
JWTBearer
(
HTTPBearer
):
def
__init__
(
self
,
auto_error
:
bool
=
True
):
def
__init__
(
self
,
auto_error
:
bool
=
True
):
super
(
JWTBearer
,
self
)
.
__init__
(
auto_error
=
auto_error
)
super
(
JWTBearer
,
self
)
.
__init__
(
auto_error
=
auto_error
)
async
def
__call__
(
self
,
request
:
Request
):
async
def
__call__
(
self
,
request
:
Request
):
credentials
:
HTTPAuthorizationCredentials
=
await
super
(
JWTBearer
,
self
)
.
__call__
(
request
)
credentials
:
HTTPAuthorizationCredentials
=
await
super
(
JWTBearer
,
self
)
.
__call__
(
request
)
if
credentials
:
if
credentials
:
if
not
credentials
.
scheme
==
"Bearer"
:
if
not
credentials
.
scheme
==
"Bearer"
:
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid authentication scheme."
)
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid authentication scheme."
)
cred
=
self
.
verify_jwt
(
credentials
.
credentials
)
cred
=
self
.
verify_jwt
(
credentials
.
credentials
)
if
not
cred
:
if
not
cred
:
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid token or expired token."
)
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid token or expired token."
)
return
credentials
.
credentials
return
credentials
.
credentials
else
:
else
:
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid authorization code."
)
raise
HTTPException
(
status_code
=
403
,
detail
=
"Invalid authorization code."
)
...
@@ -25,21 +30,20 @@ class JWTBearer(HTTPBearer):
...
@@ -25,21 +30,20 @@ class JWTBearer(HTTPBearer):
def
verify_jwt
(
self
,
jwtoken
:
str
)
->
bool
:
def
verify_jwt
(
self
,
jwtoken
:
str
)
->
bool
:
isTokenValid
:
bool
=
False
isTokenValid
:
bool
=
False
try
:
payload
=
decodeJWT
(
jwtoken
)
payload
=
decodeJWT
(
jwtoken
)
except
:
payload
=
None
if
payload
:
if
payload
:
return
payload
return
payload
return
isTokenValid
return
isTokenValid
async
def
get_current_user
(
token
:
str
=
Depends
(
JWTBearer
()))
->
str
:
async
def
get_current_user
(
token
:
str
=
Depends
(
JWTBearer
()))
->
str
:
payload
=
decodeJWT
(
token
)
payload
=
decodeJWT
(
token
)
return
payload
[
'user_id'
]
return
payload
[
"user_id"
]
async
def
admin_user
(
token
:
str
=
Depends
(
JWTBearer
()))
->
str
:
async
def
admin_user
(
token
:
str
=
Depends
(
JWTBearer
()))
->
str
:
payload
=
decodeJWT
(
token
)
payload
=
decodeJWT
(
token
)
user
=
PUser
.
find
(
PUser
.
username
==
payload
[
'user_id'
])
.
all
()[
0
]
user
=
PUser
.
find
(
PUser
.
username
==
payload
[
"user_id"
])
.
all
()[
0
]
if
not
user
.
admin
:
if
not
user
.
admin
:
raise
HTTPException
(
status_code
=
401
,
detail
=
"you can not have access"
)
raise
HTTPException
(
status_code
=
401
,
detail
=
"you can not have access"
)
return
payload
[
'user_id'
]
return
payload
[
"user_id"
]
\ No newline at end of file
core/models.py
View file @
d8264cd8
from
typing
import
Union
from
pydantic
import
BaseModel
,
EmailStr
from
pydantic
import
BaseModel
,
EmailStr
class
User
(
BaseModel
):
class
User
(
BaseModel
):
username
:
str
username
:
str
email
:
EmailStr
email
:
EmailStr
password
:
str
password
:
str
class
DataCenter
(
BaseModel
):
class
DataCenter
(
BaseModel
):
name
:
str
name
:
str
class
Token
(
BaseModel
):
class
Token
(
BaseModel
):
datacenter
:
str
datacenter
:
str
token
:
str
token
:
str
class
UserLoginSchema
(
BaseModel
):
class
UserLoginSchema
(
BaseModel
):
username
:
str
username
:
str
password
:
str
password
:
str
class
Config
:
class
Config
:
schema_extra
=
{
schema_extra
=
{
"example"
:
{
"username"
:
"user"
,
"password"
:
"weakpassword"
}}
"example"
:
{
"username"
:
"user"
,
"password"
:
"weakpassword"
}
}
\ No newline at end of file
main.py
View file @
d8264cd8
from
fastapi
import
FastAPI
,
Response
,
Body
,
Depends
from
fastapi
import
FastAPI
,
Response
,
Body
,
Depends
from
typing
import
List
from
typing
import
List
from
balancer.util
import
proxy_datacenters
from
balancer.util
import
proxy_datacenters
from
sredis.models
import
*
from
sredis.sredis
import
check_user
,
create_puser
,
add_datacenter
,
set_token
from
sredis.sredis
import
check_user
,
create_puser
,
add_datacenter
,
set_token
import
logging
import
logging
import
requests
import
requests
...
@@ -10,11 +9,11 @@ from core.auth import signJWT
...
@@ -10,11 +9,11 @@ from core.auth import signJWT
from
core.bearer
import
get_current_user
,
admin_user
from
core.bearer
import
get_current_user
,
admin_user
from
redis_om
import
Migrator
from
redis_om
import
Migrator
logging
.
config
.
fileConfig
(
'logging.conf'
,
disable_existing_loggers
=
False
)
logging
.
config
.
fileConfig
(
"logging.conf"
,
disable_existing_loggers
=
False
)
# get root logger
# get root logger
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
requests
.
packages
.
urllib3
.
disable_warnings
()
requests
.
packages
.
urllib3
.
disable_warnings
()
Migrator
()
.
run
()
Migrator
()
.
run
()
app
=
FastAPI
()
app
=
FastAPI
()
...
@@ -28,41 +27,32 @@ async def create_user(user: User = Body(...)):
...
@@ -28,41 +27,32 @@ async def create_user(user: User = Body(...)):
create_puser
(
user
)
create_puser
(
user
)
return
signJWT
(
user
.
username
)
return
signJWT
(
user
.
username
)
@app.post
(
"/user/login"
,
tags
=
[
"user"
])
@app.post
(
"/user/login"
,
tags
=
[
"user"
])
async
def
user_login
(
user
:
UserLoginSchema
=
Body
(
...
)):
async
def
user_login
(
user
:
UserLoginSchema
=
Body
(
...
)):
if
check_user
(
user
):
if
check_user
(
user
):
return
signJWT
(
user
.
username
)
return
signJWT
(
user
.
username
)
return
{
return
{
"error"
:
"Wrong login details!"
}
"error"
:
"Wrong login details!"
}
@app.get
(
"/lb/{server_path:path}"
)
@app.get
(
"/lb/{server_path:path}"
)
def
proxy
(
def
proxy_get
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)):
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)
):
return
proxy_datacenters
(
server_path
,
username
)
return
proxy_datacenters
(
server_path
,
username
)
@app.post
(
"/lb/{server_path:path}"
)
@app.post
(
"/lb/{server_path:path}"
)
def
proxy
(
def
proxy_post
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)):
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)
):
return
proxy_datacenters
(
server_path
,
username
,
method
=
"POST"
)
return
proxy_datacenters
(
server_path
,
username
,
method
=
"POST"
)
@app.post
(
"/add_datacenter/"
)
@app.post
(
"/add_datacenter/"
)
def
create_datacenter
(
def
create_datacenter
(
dc
:
DataCenter
=
None
,
username
=
Depends
(
admin_user
)):
dc
:
DataCenter
=
None
,
username
=
Depends
(
admin_user
)
):
add_datacenter
(
dc
.
name
)
add_datacenter
(
dc
.
name
)
return
Response
(
status_code
=
201
)
return
Response
(
status_code
=
201
)
@app.post
(
"/set_tokens/"
)
@app.post
(
"/set_tokens/"
)
def
set_tokens
(
def
set_tokens
(
tokens
:
List
[
Token
]
=
None
,
username
=
Depends
(
get_current_user
)):
tokens
:
List
[
Token
]
=
None
,
username
=
Depends
(
get_current_user
)
):
for
token
in
tokens
:
for
token
in
tokens
:
set_token
(
username
,
str
(
token
.
datacenter
),
str
(
token
.
token
))
set_token
(
username
,
str
(
token
.
datacenter
),
str
(
token
.
token
))
return
tokens
return
tokens
poetry.lock
View file @
d8264cd8
...
@@ -38,6 +38,57 @@ files = [
...
@@ -38,6 +38,57 @@ files = [
typing-extensions = {version = ">=3.6.5", markers = "python_version < \"3.8\""}
typing-extensions = {version = ">=3.6.5", markers = "python_version < \"3.8\""}
[[package]]
[[package]]
name = "black"
version = "23.1.0"
description = "The uncompromising code formatter."
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "black-23.1.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:b6a92a41ee34b883b359998f0c8e6eb8e99803aa8bf3123bf2b2e6fec505a221"},
{file = "black-23.1.0-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:57c18c5165c1dbe291d5306e53fb3988122890e57bd9b3dcb75f967f13411a26"},
{file = "black-23.1.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:9880d7d419bb7e709b37e28deb5e68a49227713b623c72b2b931028ea65f619b"},
{file = "black-23.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e6663f91b6feca5d06f2ccd49a10f254f9298cc1f7f49c46e498a0771b507104"},
{file = "black-23.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:9afd3f493666a0cd8f8df9a0200c6359ac53940cbde049dcb1a7eb6ee2dd7074"},
{file = "black-23.1.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:bfffba28dc52a58f04492181392ee380e95262af14ee01d4bc7bb1b1c6ca8d27"},
{file = "black-23.1.0-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:c1c476bc7b7d021321e7d93dc2cbd78ce103b84d5a4cf97ed535fbc0d6660648"},
{file = "black-23.1.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:382998821f58e5c8238d3166c492139573325287820963d2f7de4d518bd76958"},
{file = "black-23.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2bf649fda611c8550ca9d7592b69f0637218c2369b7744694c5e4902873b2f3a"},
{file = "black-23.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:121ca7f10b4a01fd99951234abdbd97728e1240be89fde18480ffac16503d481"},
{file = "black-23.1.0-cp37-cp37m-macosx_10_16_x86_64.whl", hash = "sha256:a8471939da5e824b891b25751955be52ee7f8a30a916d570a5ba8e0f2eb2ecad"},
{file = "black-23.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8178318cb74f98bc571eef19068f6ab5613b3e59d4f47771582f04e175570ed8"},
{file = "black-23.1.0-cp37-cp37m-win_amd64.whl", hash = "sha256:a436e7881d33acaf2536c46a454bb964a50eff59b21b51c6ccf5a40601fbef24"},
{file = "black-23.1.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:a59db0a2094d2259c554676403fa2fac3473ccf1354c1c63eccf7ae65aac8ab6"},
{file = "black-23.1.0-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:0052dba51dec07ed029ed61b18183942043e00008ec65d5028814afaab9a22fd"},
{file = "black-23.1.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:49f7b39e30f326a34b5c9a4213213a6b221d7ae9d58ec70df1c4a307cf2a1580"},
{file = "black-23.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:162e37d49e93bd6eb6f1afc3e17a3d23a823042530c37c3c42eeeaf026f38468"},
{file = "black-23.1.0-cp38-cp38-win_amd64.whl", hash = "sha256:8b70eb40a78dfac24842458476135f9b99ab952dd3f2dab738c1881a9b38b753"},
{file = "black-23.1.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:a29650759a6a0944e7cca036674655c2f0f63806ddecc45ed40b7b8aa314b651"},
{file = "black-23.1.0-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:bb460c8561c8c1bec7824ecbc3ce085eb50005883a6203dcfb0122e95797ee06"},
{file = "black-23.1.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:c91dfc2c2a4e50df0026f88d2215e166616e0c80e86004d0003ece0488db2739"},
{file = "black-23.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2a951cc83ab535d248c89f300eccbd625e80ab880fbcfb5ac8afb5f01a258ac9"},
{file = "black-23.1.0-cp39-cp39-win_amd64.whl", hash = "sha256:0680d4380db3719ebcfb2613f34e86c8e6d15ffeabcf8ec59355c5e7b85bb555"},
{file = "black-23.1.0-py3-none-any.whl", hash = "sha256:7a0f701d314cfa0896b9001df70a530eb2472babb76086344e688829efd97d32"},
{file = "black-23.1.0.tar.gz", hash = "sha256:b0bd97bea8903f5a2ba7219257a44e3f1f9d00073d6cc1add68f0beec69692ac"},
]
[package.dependencies]
click = ">=8.0.0"
mypy-extensions = ">=0.4.3"
packaging = ">=22.0"
pathspec = ">=0.9.0"
platformdirs = ">=2"
tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
typed-ast = {version = ">=1.4.2", markers = "python_version < \"3.8\" and implementation_name == \"cpython\""}
typing-extensions = {version = ">=3.10.0.0", markers = "python_version < \"3.10\""}
[package.extras]
colorama = ["colorama (>=0.4.3)"]
d = ["aiohttp (>=3.7.4)"]
jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
uvloop = ["uvloop (>=0.15.2)"]
[[package]]
name = "certifi"
name = "certifi"
version = "2022.12.7"
version = "2022.12.7"
description = "Python package for providing Mozilla's CA Bundle."
description = "Python package for providing Mozilla's CA Bundle."
...
@@ -504,6 +555,18 @@ files = [
...
@@ -504,6 +555,18 @@ files = [
]
]
[[package]]
[[package]]
name = "mypy-extensions"
version = "1.0.0"
description = "Type system extensions for programs checked with the mypy type checker."
category = "main"
optional = false
python-versions = ">=3.5"
files = [
{file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
{file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
]
[[package]]
name = "orjson"
name = "orjson"
version = "3.8.7"
version = "3.8.7"
description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy"
description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy"
...
@@ -558,6 +621,18 @@ files = [
...
@@ -558,6 +621,18 @@ files = [
]
]
[[package]]
[[package]]
name = "packaging"
version = "23.0"
description = "Core utilities for Python packages"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "packaging-23.0-py3-none-any.whl", hash = "sha256:714ac14496c3e68c99c29b00845f7a2b85f3bb6f1078fd9f72fd20f0570002b2"},
{file = "packaging-23.0.tar.gz", hash = "sha256:b6ad297f8907de0fa2fe1ccbd26fdaf387f5f47c7275fedf8cce89f99446cf97"},
]
[[package]]
name = "passlib"
name = "passlib"
version = "1.7.4"
version = "1.7.4"
description = "comprehensive password hashing framework supporting over 30 schemes"
description = "comprehensive password hashing framework supporting over 30 schemes"
...
@@ -588,6 +663,37 @@ files = [
...
@@ -588,6 +663,37 @@ files = [
]
]
[[package]]
[[package]]
name = "pathspec"
version = "0.11.0"
description = "Utility library for gitignore style pattern matching of file paths."
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "pathspec-0.11.0-py3-none-any.whl", hash = "sha256:3a66eb970cbac598f9e5ccb5b2cf58930cd8e3ed86d393d541eaf2d8b1705229"},
{file = "pathspec-0.11.0.tar.gz", hash = "sha256:64d338d4e0914e91c1792321e6907b5a593f1ab1851de7fc269557a21b30ebbc"},
]
[[package]]
name = "platformdirs"
version = "3.1.0"
description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "platformdirs-3.1.0-py3-none-any.whl", hash = "sha256:13b08a53ed71021350c9e300d4ea8668438fb0046ab3937ac9a29913a1a1350a"},
{file = "platformdirs-3.1.0.tar.gz", hash = "sha256:accc3665857288317f32c7bebb5a8e482ba717b474f3fc1d18ca7f9214be0cef"},
]
[package.dependencies]
typing-extensions = {version = ">=4.4", markers = "python_version < \"3.8\""}
[package.extras]
docs = ["furo (>=2022.12.7)", "proselint (>=0.13)", "sphinx (>=6.1.3)", "sphinx-autodoc-typehints (>=1.22,!=1.23.4)"]
test = ["appdirs (==1.4.4)", "covdefaults (>=2.2.2)", "pytest (>=7.2.1)", "pytest-cov (>=4)", "pytest-mock (>=3.10)"]
[[package]]
name = "poethepoet"
name = "poethepoet"
version = "0.18.1"
version = "0.18.1"
description = "A task runner that works well with poetry."
description = "A task runner that works well with poetry."
...
@@ -796,6 +902,33 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)"]
...
@@ -796,6 +902,33 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)"]
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
[[package]]
[[package]]
name = "ruff"
version = "0.0.254"
description = "An extremely fast Python linter, written in Rust."
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "ruff-0.0.254-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:dd58c500d039fb381af8d861ef456c3e94fd6855c3d267d6c6718c9a9fe07be0"},
{file = "ruff-0.0.254-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:688379050ae05394a6f9f9c8471587fd5dcf22149bd4304a4ede233cc4ef89a1"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ac1429be6d8bd3db0bf5becac3a38bd56f8421447790c50599cd90fd53417ec4"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:059a380c08e849b6f312479b18cc63bba2808cff749ad71555f61dd930e3c9a2"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:b3f15d5d033fd3dcb85d982d6828ddab94134686fac2c02c13a8822aa03e1321"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:8deba44fd563361c488dedec90dc330763ee0c01ba54e17df54ef5820079e7e0"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ef20bf798ffe634090ad3dc2e8aa6a055f08c448810a2f800ab716cc18b80107"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:0deb1d7226ea9da9b18881736d2d96accfa7f328c67b7410478cc064ad1fa6aa"},
{file = "ruff-0.0.254-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:27d39d697fdd7df1f2a32c1063756ee269ad8d5345c471ee3ca450636d56e8c6"},
{file = "ruff-0.0.254-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:2fc21d060a3197ac463596a97d9b5db2d429395938b270ded61dd60f0e57eb21"},
{file = "ruff-0.0.254-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:f70dc93bc9db15cccf2ed2a831938919e3e630993eeea6aba5c84bc274237885"},
{file = "ruff-0.0.254-py3-none-musllinux_1_2_i686.whl", hash = "sha256:09c764bc2bd80c974f7ce1f73a46092c286085355a5711126af351b9ae4bea0c"},
{file = "ruff-0.0.254-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:d4385cdd30153b7aa1d8f75dfd1ae30d49c918ead7de07e69b7eadf0d5538a1f"},
{file = "ruff-0.0.254-py3-none-win32.whl", hash = "sha256:c38291bda4c7b40b659e8952167f386e86ec29053ad2f733968ff1d78b4c7e15"},
{file = "ruff-0.0.254-py3-none-win_amd64.whl", hash = "sha256:e15742df0f9a3615fbdc1ee9a243467e97e75bf88f86d363eee1ed42cedab1ec"},
{file = "ruff-0.0.254-py3-none-win_arm64.whl", hash = "sha256:b435afc4d65591399eaf4b2af86e441a71563a2091c386cadf33eaa11064dc09"},
{file = "ruff-0.0.254.tar.gz", hash = "sha256:0eb66c9520151d3bd950ea43b3a088618a8e4e10a5014a72687881e6f3606312"},
]
[[package]]
name = "sniffio"
name = "sniffio"
version = "1.3.0"
version = "1.3.0"
description = "Sniff out which async library your code is running under"
description = "Sniff out which async library your code is running under"
...
@@ -839,6 +972,40 @@ files = [
...
@@ -839,6 +972,40 @@ files = [
]
]
[[package]]
[[package]]
name = "typed-ast"
version = "1.5.4"
description = "a fork of Python 2 and 3 ast modules with type comment support"
category = "main"
optional = false
python-versions = ">=3.6"
files = [
{file = "typed_ast-1.5.4-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:669dd0c4167f6f2cd9f57041e03c3c2ebf9063d0757dc89f79ba1daa2bfca9d4"},
{file = "typed_ast-1.5.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:211260621ab1cd7324e0798d6be953d00b74e0428382991adfddb352252f1d62"},
{file = "typed_ast-1.5.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:267e3f78697a6c00c689c03db4876dd1efdfea2f251a5ad6555e82a26847b4ac"},
{file = "typed_ast-1.5.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:c542eeda69212fa10a7ada75e668876fdec5f856cd3d06829e6aa64ad17c8dfe"},
{file = "typed_ast-1.5.4-cp310-cp310-win_amd64.whl", hash = "sha256:a9916d2bb8865f973824fb47436fa45e1ebf2efd920f2b9f99342cb7fab93f72"},
{file = "typed_ast-1.5.4-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:79b1e0869db7c830ba6a981d58711c88b6677506e648496b1f64ac7d15633aec"},
{file = "typed_ast-1.5.4-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a94d55d142c9265f4ea46fab70977a1944ecae359ae867397757d836ea5a3f47"},
{file = "typed_ast-1.5.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:183afdf0ec5b1b211724dfef3d2cad2d767cbefac291f24d69b00546c1837fb6"},
{file = "typed_ast-1.5.4-cp36-cp36m-win_amd64.whl", hash = "sha256:639c5f0b21776605dd6c9dbe592d5228f021404dafd377e2b7ac046b0349b1a1"},
{file = "typed_ast-1.5.4-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:cf4afcfac006ece570e32d6fa90ab74a17245b83dfd6655a6f68568098345ff6"},
{file = "typed_ast-1.5.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ed855bbe3eb3715fca349c80174cfcfd699c2f9de574d40527b8429acae23a66"},
{file = "typed_ast-1.5.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:6778e1b2f81dfc7bc58e4b259363b83d2e509a65198e85d5700dfae4c6c8ff1c"},
{file = "typed_ast-1.5.4-cp37-cp37m-win_amd64.whl", hash = "sha256:0261195c2062caf107831e92a76764c81227dae162c4f75192c0d489faf751a2"},
{file = "typed_ast-1.5.4-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:2efae9db7a8c05ad5547d522e7dbe62c83d838d3906a3716d1478b6c1d61388d"},
{file = "typed_ast-1.5.4-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:7d5d014b7daa8b0bf2eaef684295acae12b036d79f54178b92a2b6a56f92278f"},
{file = "typed_ast-1.5.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:370788a63915e82fd6f212865a596a0fefcbb7d408bbbb13dea723d971ed8bdc"},
{file = "typed_ast-1.5.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:4e964b4ff86550a7a7d56345c7864b18f403f5bd7380edf44a3c1fb4ee7ac6c6"},
{file = "typed_ast-1.5.4-cp38-cp38-win_amd64.whl", hash = "sha256:683407d92dc953c8a7347119596f0b0e6c55eb98ebebd9b23437501b28dcbb8e"},
{file = "typed_ast-1.5.4-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:4879da6c9b73443f97e731b617184a596ac1235fe91f98d279a7af36c796da35"},
{file = "typed_ast-1.5.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3e123d878ba170397916557d31c8f589951e353cc95fb7f24f6bb69adc1a8a97"},
{file = "typed_ast-1.5.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ebd9d7f80ccf7a82ac5f88c521115cc55d84e35bf8b446fcd7836eb6b98929a3"},
{file = "typed_ast-1.5.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:98f80dee3c03455e92796b58b98ff6ca0b2a6f652120c263efdba4d6c5e58f72"},
{file = "typed_ast-1.5.4-cp39-cp39-win_amd64.whl", hash = "sha256:0fdbcf2fef0ca421a3f5912555804296f0b0960f0418c440f5d6d3abb549f3e1"},
{file = "typed_ast-1.5.4.tar.gz", hash = "sha256:39e21ceb7388e4bb37f4c679d72707ed46c2fbf2a5609b8b8ebc4b067d977df2"},
]
[[package]]
name = "types-pyopenssl"
name = "types-pyopenssl"
version = "23.0.0.4"
version = "23.0.0.4"
description = "Typing stubs for pyOpenSSL"
description = "Typing stubs for pyOpenSSL"
...
@@ -937,4 +1104,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more
...
@@ -937,4 +1104,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more
[metadata]
[metadata]
lock-version = "2.0"
lock-version = "2.0"
python-versions = "^3.7"
python-versions = "^3.7"
content-hash = "
63e8cb34a8f2a6da1d6d6be94d532bc4b064a1a8c7d7d49cbbc14228a297e984
"
content-hash = "
761917877edc7498df27420c2308e6e0d2ef4ca441ed706ba5df5523c59303b6
"
pyproject.toml
View file @
d8264cd8
...
@@ -18,13 +18,15 @@ python-decouple = "^3.8"
...
@@ -18,13 +18,15 @@ python-decouple = "^3.8"
redis-om
=
"^0.1.2"
redis-om
=
"^0.1.2"
pydantic
=
{extras
=
["email"]
,
version
=
"^1.10.6"
}
pydantic
=
{extras
=
["email"]
,
version
=
"^1.10.6"
}
passlib
=
"^1.7.4"
passlib
=
"^1.7.4"
black
=
"^23.1.0"
ruff
=
"^0.0.254"
[tool.poe.tasks.start]
[tool.poe.tasks.start]
shell
=
"poetry run uvicorn main:app --reload --port 6973 --host 0.0.0.0"
shell
=
"poetry run uvicorn main:app --reload --port 6973 --host 0.0.0.0"
help
=
"Start the microservice on port 6973"
help
=
"Start the microservice on port 6973"
[tool.poe.tasks.lint]
[tool.poe.tasks.lint]
shell
=
"poetry run black . && poetry run ruff --fix .
&& poetry run mypy backend
"
shell
=
"poetry run black . && poetry run ruff --fix . "
help
=
"Lint the most important parts of the microservice with black"
help
=
"Lint the most important parts of the microservice with black"
[build-system]
[build-system]
...
...
sredis/models.py
View file @
d8264cd8
from
redis_om
import
HashModel
,
Field
from
redis_om
import
HashModel
,
Field
from
pydantic
import
EmailStr
from
pydantic
import
EmailStr
class
PUser
(
HashModel
):
class
PUser
(
HashModel
):
username
:
str
=
Field
(
index
=
True
)
username
:
str
=
Field
(
index
=
True
)
email
:
EmailStr
email
:
EmailStr
password
:
str
password
:
str
admin
:
bool
=
False
admin
:
bool
=
False
sredis/sredis.py
View file @
d8264cd8
...
@@ -5,13 +5,13 @@ from passlib.hash import pbkdf2_sha256
...
@@ -5,13 +5,13 @@ from passlib.hash import pbkdf2_sha256
from
core.models
import
User
from
core.models
import
User
from
core.auth
import
hash_pass
from
core.auth
import
hash_pass
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
from
redis_om
import
Migrator
r
=
redis
.
Redis
(
host
=
'localhost'
,
port
=
6379
,
db
=
0
,
decode_responses
=
True
)
r
=
redis
.
Redis
(
host
=
"localhost"
,
port
=
6379
,
db
=
0
,
decode_responses
=
True
)
r
.
set
(
"datacenters_cnt"
,
1
)
r
.
set
(
"datacenters_cnt"
,
1
)
all_keys
=
list
(
r
.
hgetall
(
'datacenters_hash'
)
.
keys
())
all_keys
=
list
(
r
.
hgetall
(
"datacenters_hash"
)
.
keys
())
if
all_keys
:
if
all_keys
:
r
.
hdel
(
'datacenters_hash'
,
*
all_keys
)
r
.
hdel
(
"datacenters_hash"
,
*
all_keys
)
def
add_datacenter
(
datacenter
:
str
):
def
add_datacenter
(
datacenter
:
str
):
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
...
@@ -19,35 +19,38 @@ def add_datacenter(datacenter: str):
...
@@ -19,35 +19,38 @@ def add_datacenter(datacenter: str):
r
.
incr
(
"datacenters_cnt"
)
r
.
incr
(
"datacenters_cnt"
)
r
.
set
(
"roundrobin_cnt"
,
1
)
r
.
set
(
"roundrobin_cnt"
,
1
)
def
rr_get
():
def
rr_get
():
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
rr
=
int
(
r
.
get
(
"roundrobin_cnt"
))
rr
=
int
(
r
.
get
(
"roundrobin_cnt"
))
if
rr
+
1
>=
cnt
:
if
rr
+
1
>=
cnt
:
r
.
set
(
"roundrobin_cnt"
,
1
)
r
.
set
(
"roundrobin_cnt"
,
1
)
else
:
else
:
r
.
incr
(
"roundrobin_cnt"
)
r
.
incr
(
"roundrobin_cnt"
)
return
str
(
r
.
hget
(
"datacenters_hash"
,
rr
))
return
str
(
r
.
hget
(
"datacenters_hash"
,
rr
))
def
set_token
(
username
:
str
,
datacenter
:
str
,
token
:
str
):
def
set_token
(
username
:
str
,
datacenter
:
str
,
token
:
str
):
print
(
f
"tokens:{username}"
+
datacenter
)
print
(
f
"tokens:{username}"
+
datacenter
)
r
.
hset
(
f
"tokens:{username}"
,
datacenter
,
token
)
r
.
hset
(
f
"tokens:{username}"
,
datacenter
,
token
)
def
get_datacenter_token
(
username
:
str
,
datacenter
:
str
):
def
get_datacenter_token
(
username
:
str
,
datacenter
:
str
):
return
str
(
r
.
hget
(
f
"tokens:{username}"
,
datacenter
))
return
str
(
r
.
hget
(
f
"tokens:{username}"
,
datacenter
))
def
check_user
(
data
:
UserLoginSchema
):
def
check_user
(
data
:
UserLoginSchema
):
user
=
PUser
.
find
(
PUser
.
username
==
data
.
username
)
.
all
()
user
=
PUser
.
find
(
PUser
.
username
==
data
.
username
)
.
all
()
if
pbkdf2_sha256
.
verify
(
data
.
password
,
user
[
0
]
.
password
):
if
pbkdf2_sha256
.
verify
(
data
.
password
,
user
[
0
]
.
password
):
return
user
[
0
]
return
user
[
0
]
return
False
return
False
def
create_puser
(
user
:
User
):
def
create_puser
(
user
:
User
):
s
=
PUser
.
find
(
PUser
.
username
==
user
.
username
)
.
all
()
s
=
PUser
.
find
(
PUser
.
username
==
user
.
username
)
.
all
()
if
s
:
if
s
:
raise
HTTPException
(
status_code
=
403
,
detail
=
"User already exists"
)
raise
HTTPException
(
status_code
=
403
,
detail
=
"User already exists"
)
user
=
PUser
(
user
=
PUser
(
username
=
user
.
username
,
username
=
user
.
username
,
email
=
user
.
email
,
password
=
hash_pass
(
user
.
password
)
email
=
user
.
email
,
password
=
hash_pass
(
user
.
password
)
)
)
user
.
save
()
user
.
save
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment