Spaces:
Running
Running
login stuff
Browse files- app.py +2 -2
- tokenManagement.py +28 -0
app.py
CHANGED
|
@@ -292,7 +292,7 @@ def login(user:UserBody):
|
|
| 292 |
print(result)
|
| 293 |
access_token = encode_jwt(user_id=user_id,access_token=accessToken)
|
| 294 |
return {"refreshToken":refreshToken,"accessToken":access_token}
|
| 295 |
-
return JSONResponse(status_code=401,content="Invalid login details")
|
| 296 |
|
| 297 |
|
| 298 |
@app.post("/auth/signup",tags=["Authentication"])
|
|
@@ -336,7 +336,7 @@ def refresh_access_token(refresh_token:Token, authorization: str = Header(...)):
|
|
| 336 |
|
| 337 |
# Here, you would validate the token (e.g., check with a JWT library)
|
| 338 |
decoded_user_id,decoded_access_token = decode_jwt(token)
|
| 339 |
-
is_valid =
|
| 340 |
if is_valid != True: # Example check
|
| 341 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 342 |
new_access_token = create_accessToken(db_uri=MONGO_URI,user_id=decoded_user_id,refresh_token=refresh_token.refreshToken)
|
|
|
|
| 292 |
print(result)
|
| 293 |
access_token = encode_jwt(user_id=user_id,access_token=accessToken)
|
| 294 |
return {"refreshToken":refreshToken,"accessToken":access_token}
|
| 295 |
+
return JSONResponse(status_code=401,content={"detail":"Invalid login details"})
|
| 296 |
|
| 297 |
|
| 298 |
@app.post("/auth/signup",tags=["Authentication"])
|
|
|
|
| 336 |
|
| 337 |
# Here, you would validate the token (e.g., check with a JWT library)
|
| 338 |
decoded_user_id,decoded_access_token = decode_jwt(token)
|
| 339 |
+
is_valid = verify_refresh_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
|
| 340 |
if is_valid != True: # Example check
|
| 341 |
raise HTTPException(status_code=401, detail="Invalid token")
|
| 342 |
new_access_token = create_accessToken(db_uri=MONGO_URI,user_id=decoded_user_id,refresh_token=refresh_token.refreshToken)
|
tokenManagement.py
CHANGED
|
@@ -145,6 +145,34 @@ def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
|
|
| 145 |
pass
|
| 146 |
return False
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
|
|
|
|
| 145 |
pass
|
| 146 |
return False
|
| 147 |
|
| 148 |
+
def verify_refresh_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
|
| 149 |
+
|
| 150 |
+
current_time = datetime.datetime.now()
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
"""
|
| 154 |
+
# Connect to MongoDB
|
| 155 |
+
client = MongoClient(db_uri)
|
| 156 |
+
db = client["crayonics"]
|
| 157 |
+
collection = db["AccessToken"]
|
| 158 |
+
docs = collection.find({"user_id":user_id})
|
| 159 |
+
for doc in docs:
|
| 160 |
+
|
| 161 |
+
if doc==None:
|
| 162 |
+
return False
|
| 163 |
+
else:
|
| 164 |
+
if str(doc['_id']) == access_token:
|
| 165 |
+
streaks_doc={}
|
| 166 |
+
streaks_doc['user_id'] = str(user_id)
|
| 167 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
| 168 |
+
return True
|
| 169 |
+
else:
|
| 170 |
+
streaks_doc={}
|
| 171 |
+
streaks_doc['user_id'] = str(user_id)
|
| 172 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
| 173 |
+
pass
|
| 174 |
+
return False
|
| 175 |
+
|
| 176 |
|
| 177 |
|
| 178 |
|