Added token type to JWT payload
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -116,26 +116,19 @@ export class AuthService extends BaseService {
|
||||
* Get Access Token
|
||||
* Retrieve an access token for the authenticated user.
|
||||
* @endpoint post /api/auth/v2/access
|
||||
* @param authorization
|
||||
* @param bodyGetAccessToken
|
||||
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
|
||||
* @param reportProgress flag to report request and response progress.
|
||||
*/
|
||||
public getAccessToken(authorization: string, bodyGetAccessToken: BodyGetAccessToken, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<{ [key: string]: string | null; }>;
|
||||
public getAccessToken(authorization: string, bodyGetAccessToken: BodyGetAccessToken, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpResponse<{ [key: string]: string | null; }>>;
|
||||
public getAccessToken(authorization: string, bodyGetAccessToken: BodyGetAccessToken, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpEvent<{ [key: string]: string | null; }>>;
|
||||
public getAccessToken(authorization: string, bodyGetAccessToken: BodyGetAccessToken, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<any> {
|
||||
if (authorization === null || authorization === undefined) {
|
||||
throw new Error('Required parameter authorization was null or undefined when calling getAccessToken.');
|
||||
}
|
||||
public getAccessToken(bodyGetAccessToken: BodyGetAccessToken, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<{ [key: string]: string | null; }>;
|
||||
public getAccessToken(bodyGetAccessToken: BodyGetAccessToken, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpResponse<{ [key: string]: string | null; }>>;
|
||||
public getAccessToken(bodyGetAccessToken: BodyGetAccessToken, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpEvent<{ [key: string]: string | null; }>>;
|
||||
public getAccessToken(bodyGetAccessToken: BodyGetAccessToken, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<any> {
|
||||
if (bodyGetAccessToken === null || bodyGetAccessToken === undefined) {
|
||||
throw new Error('Required parameter bodyGetAccessToken was null or undefined when calling getAccessToken.');
|
||||
}
|
||||
|
||||
let localVarHeaders = this.defaultHeaders;
|
||||
if (authorization !== undefined && authorization !== null) {
|
||||
localVarHeaders = localVarHeaders.set('Authorization', String(authorization));
|
||||
}
|
||||
|
||||
// authentication (HTTPBearer) required
|
||||
localVarHeaders = this.configuration.addCredentialToHeaders('HTTPBearer', 'Authorization', localVarHeaders, 'Bearer ');
|
||||
|
||||
Reference in New Issue
Block a user