async def register( username: str, email: pydantic.EmailStr, password: str, user_service: auth.UserService = fastapi.Depends( service_locator.default().user_service ), password_hasher: auth.PasswordHashService = fastapi.Depends( service_locator.default().password_hasher ), ) -> response.UserResponse: pw_hash = password_hasher.create(pydantic.SecretStr(password)) user = user_service.create_user( username=username, email=email, password_hash=pw_hash ) return response.UserResponse.from_domain(user)
async def login( form_data: OAuth2PasswordRequestForm = fastapi.Depends(), user_service: auth.UserService = fastapi.Depends( service_locator.default().user_service ), password_hasher: auth.PasswordHashService = fastapi.Depends( service_locator.default().password_hasher ), token_service: auth.TokenService = fastapi.Depends( service_locator.default().token_service ), ) -> auth.Token: try: user = user_service.get_user(form_data.username) except core.exception.UserNotFound: raise exceptions.CREDENTIALS_EXCEPTION else: if password_hasher.verify( hashed_password=user.password_hash, plain_password=pydantic.SecretStr(form_data.password), ): return token_service.create({"sub": user.username}) else: raise exceptions.CREDENTIALS_EXCEPTION
class DiscordConfig(pdt.BaseModel): """ Discord configuration model """ auth_channel_id: int = pdt.Field( default=0, description=( "Authentication channel ID. This is where users will start " "authentication challenges. Only required if ``discord.enable`` " "is set to ``True``."), ) enabled: bool = pdt.Field( default=False, description="Wether to activate the Discord connector.", ) log_channel_id: int = pdt.Field( default=0, description=( "Logging channel ID. This is where the Discorb bot will log " "events. Only required if ``discord.enable`` is set to ``True``."), ) server_id: int = pdt.Field( default=0, description="Discord server (or guild) ID.", ) token: pdt.SecretStr = pdt.Field( default=pdt.SecretStr(""), description=( "Discord bot token. Only required if ``discord.enable`` is set to " "``True``."), )
class TeamspeakConfig(pdt.BaseModel): """ Teamspeak configuration model """ auth_group_name: str = pdt.Field( default="SNI TS AUTH", description=( "Name of the auth group. Authenticated Teamspeak users are " "automatically added to this group. " "Only required if ``teamspeak.enable`` is set to ``True``."), ) bot_name: str = pdt.Field( default="SeAT Navy Issue", description=( "Name of the Teamspeak bot. " "Only required if ``teamspeak.enable`` is set to ``True``."), ) enabled: bool = pdt.Field( default=False, description="Wether to activate the Teamspeak connector.", ) host: str = pdt.Field( default="", description=( "Name, hostname, or IP address of the Teamspeak server. " "Only required if ``teamspeak.enable`` is set to ``True``."), ) password: pdt.SecretStr = pdt.Field( default=pdt.SecretStr(""), description=( "Query server password. " "Only required if ``teamspeak.enable`` is set to ``True``."), ) port: int = pdt.Field( default=10011, description=( "Port of the query server associated to the Teamspeak server. " "Only required if ``teamspeak.enable`` is set to ``True``."), ge=0, le=65535, ) server_id: int = pdt.Field( default=0, description=( "Teamspeak server ID. " "Only required if ``teamspeak.enable`` is set to ``True``."), ) username: str = pdt.Field( default="sni", description=( "Query server username. " "Only required if ``teamspeak.enable`` is set to ``True``."), )
import dataclasses import typing import pydantic from src import auth, api from src.api.routes import get_current_active_user from src.api.routes.auth import login, get_user, register from src.auth import domain def dummy_hasher(plain_password: pydantic.SecretStr) -> str: return plain_password.get_secret_value() * 15 PASSWORD = pydantic.SecretStr("1234") PASSWORD_HASH = dummy_hasher(PASSWORD) ACCESS_TOKEN = "test_user." + PASSWORD_HASH class DummyPasswordHasher(auth.PasswordHashService): def create(self, /, plain_password: pydantic.SecretStr) -> str: return dummy_hasher(plain_password) def verify(self, *, hashed_password: str, plain_password: pydantic.SecretStr) -> bool: return dummy_hasher(plain_password) == hashed_password class DummyTokenService(auth.TokenService): def create(self, /, data: typing.Dict[str, typing.Any]) -> domain.Token: