Skip to content
Open
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
13 changes: 6 additions & 7 deletions api/app/db/redis_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import redis
import redis.asyncio as aioredis

redis = redis.Redis(host="redis")
redis = aioredis.Redis(host="redis")


def remove_cache(path):
async def remove_cache(path):
"""
Remove the cache for the specified path.

Expand All @@ -29,13 +29,12 @@ def remove_cache(path):
"""
# Pattern da cercare nelle chiavi (ad esempio 'testop')
pattern = "*{}*".format(path)

# Itera su tutte le chiavi che corrispondono al pattern

cursor = 0
while True:
cursor, keys = redis.scan(cursor=cursor, match=pattern)
cursor, keys = await redis.scan(cursor=cursor, match=pattern)
if keys:
# Cancella le chiavi trovate
redis.delete(*keys)
await redis.delete(*keys)
if cursor == 0:
break
15 changes: 4 additions & 11 deletions api/app/v1/endpoints/create/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _extract_bearer_token(authorization: str | None) -> str:
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid authorization header format",
)
return authorization[len(prefix) :].strip()
return authorization[len(prefix):].strip()


def _ttl_from_exp(exp: int | float | str | None) -> int:
Expand Down Expand Up @@ -89,14 +89,7 @@ async def login(
async def refresh_token(authorization: str | None = Header(default=None)):
token = _extract_bearer_token(authorization)

if REDIS and redis.get(token) is not None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has been revoked",
headers={"WWW-Authenticate": "Bearer"},
)

if REDIS and redis.get(token) is not None:
if REDIS and await redis.get(token) is not None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has been revoked",
Expand All @@ -113,7 +106,7 @@ async def refresh_token(authorization: str | None = Header(default=None)):

if REDIS:
expire = payload.get("exp")
redis.set(token, "refreshed", ex=_ttl_from_exp(expire))
await redis.set(token, "refreshed", ex=_ttl_from_exp(expire))

access_token, expire = create_refresh_token(payload)

Expand Down Expand Up @@ -147,7 +140,7 @@ async def logout(authorization: str | None = Header(default=None)):

if REDIS:
expire = payload.get("exp")
redis.set(token, "logged_out", ex=_ttl_from_exp(expire))
await redis.set(token, "logged_out", ex=_ttl_from_exp(expire))

return JSONResponse(
status_code=status.HTTP_200_OK,
Expand Down