Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions api/src/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""FastAPI application."""
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware

from dependencies.common_key_header import common_key_header
from middlewares import client_auth
Expand All @@ -8,6 +9,17 @@

app = FastAPI(title="NetWorkers API", version="1.0.0")

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allow_headers=["X-Common-Key", "Content-Type"],
expose_headers=["X-Common-Key"],
)

app.add_middleware(client_auth.ClientAuth)

@app.get("/", summary="Get app version", dependencies=[Depends(common_key_header)])
async def get_info() -> dict:
"""Get the app info."""
Expand All @@ -17,8 +29,6 @@ async def get_info() -> dict:
info["author"] = "BORGO, IUT Vélizy"
return info

app.add_middleware(client_auth.ClientAuth)

app.include_router(auth.router, prefix="/auth", tags=["auth"],
dependencies=[Depends(common_key_header)])
app.include_router(users.router, prefix="/users", tags=["users"],
Expand Down
8 changes: 6 additions & 2 deletions api/src/dependencies/common_key_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import os

from fastapi import Header, HTTPException
from fastapi import Header, HTTPException, Request


async def common_key_header(x_common_key: str = Header(...)) -> None:
async def common_key_header(request: Request, x_common_key: str = Header(...)) -> None:
"""Add Common Key Header in Swagger.

Args:
request (Request): Request object.
x_common_key (str, optional): Clé AES. Defaults to Header(...).

Raises:
HTTPException: _description_

"""
if request.method == "OPTIONS":
return None # noqa: RET501

if x_common_key != os.getenv("COMMON_KEY"):
raise HTTPException(status_code=403, detail="Invalid X-Common-Key")
3 changes: 3 additions & 0 deletions api/src/middlewares/client_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ async def dispatch(self, request: Request, call_next: callable) -> Response:
if request.url.path in ["/docs", "/redoc", "/openapi.json"]:
return await call_next(request)

if request.method == "OPTIONS":
return await call_next(request)

common_key = request.headers.get("X-Common-Key")
if common_key != COMMON_KEY:
return JSONResponse(status_code=403,
Expand Down
10 changes: 9 additions & 1 deletion api/src/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
create_access_token,
create_refresh_token,
get_hashed_password,
verify_jwt,
verify_password,
)

Expand Down Expand Up @@ -37,7 +38,14 @@ async def login(auth: Auth) -> dict:
@router.post("/refresh", summary="Refresh the access token")
async def refresh(refresh_token: RefreshToken) -> dict:
"""Refresh the access token."""
return {"access_token": create_access_token(refresh_token)}
# Verify the refresh token
token = verify_jwt(refresh_token.refresh_token)
if not token:
raise HTTPException(status_code=400, detail="Invalid refresh token")

# Create a new access token
access_token = create_access_token(token["sub"])
return {"access_token": access_token}

@router.post("/register", summary="Register to the app")
async def register(auth: Auth) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from jose import JWTError, jwt
from passlib.context import CryptContext

ACCESS_TOKEN_EXPIRE_MINUTES = 30 # 30 minutes
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 1 day
REFRESH_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 7 days
ALGORITHM = "HS256"
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY")
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ services:
- .env/db
networks:
- app-network

web:
image: node:23.0
container_name: networkers-web
working_dir: /opt/web
command: bash -c "npm install && npm run dev"
volumes:
- ./front-js:/opt/web
depends_on:
- api
restart: always
ports:
- "3000:3000"
networks:
- app-network

networks:
app-network:
Expand Down
118 changes: 118 additions & 0 deletions front-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions front-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
},
"dependencies": {
"@nextui-org/spacer": "^2.2.4",
"axios": "^1.7.9",
"js-cookie": "^3.0.5",
"next": "15.1.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"sass": "^1.83.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3",
"@types/js-cookie": "^3.0.6",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
Expand Down
Loading