Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,55 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
from
|
| 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 |
-
username
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
to_encode = data.copy()
|
| 32 |
-
if expires_delta:
|
| 33 |
-
expire = datetime.utcnow() + expires_delta
|
| 34 |
-
else:
|
| 35 |
-
expire = datetime.utcnow() + timedelta(minutes=15)
|
| 36 |
-
to_encode.update({"exp": expire})
|
| 37 |
-
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
| 38 |
-
return encoded_jwt
|
| 39 |
|
| 40 |
-
@app.post("/token", response_model=Token)
|
| 41 |
-
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
| 42 |
-
user = fake_users_db.get(form_data.username)
|
| 43 |
-
if not user or form_data.password != "secret": # In real app, verify hashed password
|
| 44 |
-
raise HTTPException(
|
| 45 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 46 |
-
detail="Incorrect username or password",
|
| 47 |
-
)
|
| 48 |
access_token = create_access_token(
|
| 49 |
-
data={"sub":
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
-
payload = jwt.decode(token, SECRET_KEY, algorithms=[
|
| 59 |
username = payload.get("sub")
|
| 60 |
if username is None:
|
| 61 |
-
raise
|
| 62 |
-
return {"username": username}
|
| 63 |
-
except jwt.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from auth import (
|
| 4 |
+
authenticate_user,
|
| 5 |
+
create_access_token,
|
| 6 |
+
register_user,
|
| 7 |
+
ACCESS_TOKEN_EXPIRE_MINUTES,
|
| 8 |
+
SECRET_KEY
|
| 9 |
+
)
|
| 10 |
+
import jwt
|
| 11 |
+
from datetime import timedelta
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
|
| 15 |
+
@app.route("/register", methods=["POST"])
|
| 16 |
+
def register():
|
| 17 |
+
data = request.get_json()
|
| 18 |
+
try:
|
| 19 |
+
result = register_user(data["username"], data["password"])
|
| 20 |
+
return jsonify(result), 201
|
| 21 |
+
except ValueError as e:
|
| 22 |
+
return jsonify({"detail": str(e)}), 400
|
| 23 |
+
|
| 24 |
+
@app.route("/token", methods=["POST"])
|
| 25 |
+
def login():
|
| 26 |
+
username = request.form.get("username")
|
| 27 |
+
password = request.form.get("password")
|
| 28 |
+
user = authenticate_user(username, password)
|
| 29 |
+
if not user:
|
| 30 |
+
return jsonify({"detail": "Invalid username or password"}), 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
access_token = create_access_token(
|
| 33 |
+
data={"sub": username}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 34 |
+
)
|
| 35 |
+
return jsonify({"access_token": access_token, "token_type": "bearer"})
|
| 36 |
+
|
| 37 |
+
@app.route("/users/me", methods=["GET"])
|
| 38 |
+
def get_user():
|
| 39 |
+
auth_header = request.headers.get("Authorization")
|
| 40 |
+
if not auth_header or not auth_header.startswith("Bearer "):
|
| 41 |
+
return jsonify({"detail": "Missing or invalid token"}), 401
|
| 42 |
+
token = auth_header.split(" ")[1]
|
| 43 |
try:
|
| 44 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
| 45 |
username = payload.get("sub")
|
| 46 |
if username is None:
|
| 47 |
+
raise jwt.InvalidTokenError
|
| 48 |
+
return jsonify({"username": username})
|
| 49 |
+
except jwt.ExpiredSignatureError:
|
| 50 |
+
return jsonify({"detail": "Token expired"}), 401
|
| 51 |
+
except jwt.InvalidTokenError:
|
| 52 |
+
return jsonify({"detail": "Invalid token"}), 401
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
app.run(host="0.0.0.0", port=8000)
|