Added token type to JWT payload
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
Mats Paul Tončik
2026-06-25 17:33:24 +02:00
parent d8a991cc3a
commit 91ec7d841c
3 changed files with 29 additions and 21 deletions
+3 -3
View File
@@ -15,9 +15,9 @@ class UserRole(StrEnum):
class TokenType(StrEnum):
ID_TOKEN = "ID_TOKEN"
REFRESH_TOKEN = "REFRESH_TOKEN"
ACCESS_TOKEN = "ACCESS_TOKEN"
ID_TOKEN = "ID"
REFRESH_TOKEN = "REFRESH"
ACCESS_TOKEN = "ACCESS"
class Durable(SQLModel):
+22 -7
View File
@@ -9,7 +9,7 @@ from typing import Annotated, Any, Dict, Literal, Optional
import jwt
from argon2 import PasswordHasher
from argon2.exceptions import InvalidHashError, VerificationError, VerifyMismatchError
from fastapi import Depends, HTTPException, Header, Request, Response, status
from fastapi import Depends, HTTPException, Request, Response, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from inse.auth.model import (
AccessToken,
@@ -18,6 +18,7 @@ from inse.auth.model import (
Organization,
RefreshToken,
Secret,
TokenType,
UserRole,
)
from inse.config import get_settings
@@ -44,7 +45,7 @@ settings = get_settings()
logger = logging.getLogger(__name__)
security_scheme = HTTPBearer(auto_error=False)
security_scheme = HTTPBearer()
_password_hasher = PasswordHasher()
@@ -668,7 +669,9 @@ def _refresh_access_token(
USER_FINGERPRINT_HASH_KEY: refresh_token.user_fingerprint_hash,
}
return _build_token(
payload=access_payload, expiration_min=ACCESS_TOKEN_EXPIRATION_MINUTES
payload=access_payload,
expiration_min=ACCESS_TOKEN_EXPIRATION_MINUTES,
token_type=TokenType.ACCESS_TOKEN,
)
@@ -690,7 +693,11 @@ def _build_id_token(response: Response, real_account_id: uuid.UUID, epoch: int)
EPOCH_KEY: epoch,
USER_FINGERPRINT_HASH_KEY: user_fingerprint_hash,
}
return _build_token(payload=payload, expiration_min=ID_TOKEN_EXPIRATION_MINUTES)
return _build_token(
payload=payload,
expiration_min=ID_TOKEN_EXPIRATION_MINUTES,
token_type=TokenType.ID_TOKEN,
)
def _build_action_tokens(
@@ -710,20 +717,27 @@ def _build_action_tokens(
return {
"access_token": _build_token(
payload=payload, expiration_min=ACCESS_TOKEN_EXPIRATION_MINUTES
payload=payload,
expiration_min=ACCESS_TOKEN_EXPIRATION_MINUTES,
token_type=TokenType.ACCESS_TOKEN,
),
"refresh_token": _build_token(
payload=payload, expiration_min=REFRESH_TOKEN_EXPIRATION_MINUTES
payload=payload,
expiration_min=REFRESH_TOKEN_EXPIRATION_MINUTES,
token_type=TokenType.REFRESH_TOKEN,
),
}
def _build_token(payload: Dict[str, Any], expiration_min: int) -> str:
def _build_token(
payload: Dict[str, Any], expiration_min: int, token_type: TokenType
) -> str:
"""Builds a JWT token with the given payload and expiration time.
Args:
payload (Dict[str, Any]): The payload to encode in the JWT.
expiration_min (int): The expiration time in minutes.
token_type (TokenType): The type of the token to build.
Returns:
str: The encoded JWT token.
@@ -736,6 +750,7 @@ def _build_token(payload: Dict[str, Any], expiration_min: int) -> str:
"iss": settings.token_issuer_id,
"iat": now,
"nbf": now,
"type": token_type.value,
}
)
if not settings.token_hmac_key: