Ich verstehe nicht, wo das Problem liegt. Alles scheint gut zu funktionieren. Ich habe den gesamten Code unten eingefügt.
Meine Codestruktur sieht folgendermaßen aus:
service.py
import uuid
from typing import Optional
from fastapi import Depends, Request
from fastapi_users import BaseUserManager, UUIDIDMixin
from fastapi_users.authentication import (
AuthenticationBackend,
BearerTransport,
JWTStrategy,
)
from fastapi_users.password import PasswordHelper
from pwdlib import PasswordHash, exceptions
from pwdlib.hashers.argon2 import Argon2Hasher
from app import config, db
from . import crud
class SecurityConfig:
__slots__ = ()
PASSWORD_HASH = PasswordHash((Argon2Hasher(),))
SECRET = config.UserSecret().secret
PASSWORD_HELPER = PasswordHelper(PASSWORD_HASH)
BEARER_TRANSPORT = BearerTransport(tokenUrl="auth/jwt/login")
class UserManager(UUIDIDMixin, BaseUserManager[db.models.User, uuid.UUID]):
reset_password_token_secret = SecurityConfig().SECRET
verification_token_secret = SecurityConfig().SECRET
async def on_after_register(
self, user: db.models.User, request: Optional[Request] = None
):
print(f"User {user.id} has registered.")
async def on_after_forgot_password(
self, user: db.models.User, token: str, request: Optional[Request] = None
):
print(f"User {user.id} has forgot their password. Reset token: {token}")
async def on_after_request_verify(
self, user: db.models.User, token: str, request: Optional[Request] = None
):
print(f"Verification requested for user {user.id}. Verification token: {token}")
async def get_user_manager(user_db=Depends(crud.get_user_db)):
"""
Yields a UserManager instance with the given user_db.
The UserManager is an instance of BaseUserManager that is used to
manage users in the application. It provides methods for registering,
authenticating, and verifying users.
Args:
user_db: The user database to use for the UserManager.
"""
yield UserManager(user_db, SecurityConfig().PASSWORD_HELPER)
def get_jwt_strategy() -> JWTStrategy:
"""
Returns the JWT strategy for the application.
The strategy is used by the auth backend to authenticate users.
It uses the SECRET key to sign the JWT and has a lifetime of 3600*24*7 seconds (7 days).
Returns:
JWTStrategy: The JWT strategy for the application.
"""
return JWTStrategy(secret=SecurityConfig().SECRET, lifetime_seconds=3600 * 24 * 7)
AUTH_BACKEND = AuthenticationBackend(
name="jwt",
transport=SecurityConfig().BEARER_TRANSPORT,
get_strategy=get_jwt_strategy,
)
async def get_user_db(session: db.AsyncSession = Depends(db.get_session_depend)):
"""
Yields a SQLAlchemyUserDatabase instance with the given session and User model.
The SQLAlchemyUserDatabase is an instance of BaseUserDatabase that is used to
manage users in the application. It provides methods for registering,
authenticating, and verifying users.
Args:
session: The database session to use for the UserManager.
model: The SQLAlchemy model to use for the UserManager.
"""
yield SQLAlchemyUserDatabase(session, db.models.User)
Ich verstehe nicht, wo das [url=viewtopic.php?t=26065]Problem[/url] liegt. Alles scheint gut zu funktionieren. Ich habe den gesamten Code unten eingefügt. Meine Codestruktur sieht folgendermaßen aus: service.py [code]import uuid from typing import Optional
from fastapi import Depends, Request from fastapi_users import BaseUserManager, UUIDIDMixin from fastapi_users.authentication import ( AuthenticationBackend, BearerTransport, JWTStrategy, ) from fastapi_users.password import PasswordHelper from pwdlib import PasswordHash, exceptions from pwdlib.hashers.argon2 import Argon2Hasher
print(f"Verification requested for user {user.id}. Verification token: {token}")
async def get_user_manager(user_db=Depends(crud.get_user_db)): """ Yields a UserManager instance with the given user_db.
The UserManager is an instance of BaseUserManager that is used to manage users in the application. It provides methods for registering, authenticating, and verifying users.
Args: user_db: The user database to use for the UserManager. """ yield UserManager(user_db, SecurityConfig().PASSWORD_HELPER)
def get_jwt_strategy() -> JWTStrategy: """ Returns the JWT strategy for the application.
The strategy is used by the auth backend to authenticate users. It uses the SECRET key to sign the JWT and has a lifetime of 3600*24*7 seconds (7 days).
Returns: JWTStrategy: The JWT strategy for the application. """ return JWTStrategy(secret=SecurityConfig().SECRET, lifetime_seconds=3600 * 24 * 7)
AUTH_BACKEND = AuthenticationBackend( name="jwt", transport=SecurityConfig().BEARER_TRANSPORT, get_strategy=get_jwt_strategy, ) [/code] get_user_db für fastapi_users: [code]async def get_user_db(session: db.AsyncSession = Depends(db.get_session_depend)): """ Yields a SQLAlchemyUserDatabase instance with the given session and User model.
The SQLAlchemyUserDatabase is an instance of BaseUserDatabase that is used to manage users in the application. It provides methods for registering, authenticating, and verifying users.
Args: session: The database session to use for the UserManager. model: The SQLAlchemy model to use for the UserManager. """ yield SQLAlchemyUserDatabase(session, db.models.User) [/code] Tests: [code]from typing import AsyncGenerator
import pytest from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker
Beim Versuch, die JWT-Bearer-Authentifizierung in meiner ASP.NET Core 8-Web-API mit Keycloak zu verwenden, tritt ein Fehler auf. Wenn ich eine Anfrage an die API sende, erhalte ich die folgende...
Um Ihnen etwas Kontext zu geben:
Ich versuche, mithilfe von ASPNET Core eine Aktualisierungs-/Zugriffstoken-Authentifizierungssicherheit für eine einfache Web-API zu implementieren. Bisher läuft es...
Problemzusammenfassung
Ab dem 1. September 2025 geben die StoreKit-Zertifikatendpunkte von Apple 401 nicht autorisierte Fehler für Anfragen zurück, die am 31. August 2025 einwandfrei funktionierten....
Jemand schlug vor, dass ich meine iOS /Backend -Kaufüberprüfung durch die Verwendung von Quittungen zu Transaktionen ändere, da es in Storekit 2.
ist, also habe ich es in meinem Node.js Backend...