Skip to content

Commit

Permalink
[chore] improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Apr 10, 2024
1 parent ece61a9 commit 2df1206
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fixbackend/auth/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
import logging
import re
import secrets
from typing import Annotated, Any, AsyncIterator, Optional, Tuple
Expand Down Expand Up @@ -41,6 +42,7 @@

# do not change this without regenerating MFA recovery codes in the db
crypt_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
log = logging.getLogger(__name__)


def bcrypt_hash(password: str) -> str:
Expand Down Expand Up @@ -97,20 +99,23 @@ async def on_after_login(
self, user: User, request: Optional[Request] = None, response: Optional[Response] = None
) -> None:
await super().on_after_login(user, request, response)
log.info(f"User logged in: {user.email} ({user.id})")
await self.domain_events_publisher.publish(UserLoggedIn(user.id, user.email))

async def add_to_workspace(self, user: User) -> None:
if (
pending_invitation := await self.invitation_repository.get_invitation_by_email(user.email)
) and pending_invitation.accepted_at:
if workspace := await self.workspace_repository.get_workspace(pending_invitation.workspace_id):
log.info(f"Add user {user.email} to workspace {workspace.id}")
await self.workspace_repository.add_to_workspace(workspace.id, user.id)
else:
# wtf?
workspace = await self.create_default_workspace(user)
await self.invitation_repository.delete_invitation(pending_invitation.id)
else:
workspace = await self.create_default_workspace(user)
log.info(f"Create new workspace {workspace.id} for {user.email}.")

await self.domain_events_publisher.publish(
UserRegistered(user_id=user.id, email=user.email, tenant_id=workspace.id)
Expand All @@ -121,6 +126,7 @@ async def create_default_workspace(self, user: User) -> Workspace:
return await self.workspace_repository.create_workspace(user.email, org_slug, user)

async def remove_oauth_account(self, account_id: UUID) -> None:
log.info(f"Remove oauth account with id {account_id}")
await self.user_repository.remove_oauth_account(account_id)

async def get(self, id: UserId) -> User:
Expand Down Expand Up @@ -236,6 +242,7 @@ async def compute_hash(code: str) -> None:
return recovery_codes, hashes

async def recreate_mfa(self, user: User) -> OTPConfig:
log.info(f"Recreate MFA for user {user.email}")
assert not user.is_mfa_active, "User already has MFA enabled."
user_secret = pyotp.random_base32()
# create recovery codes
Expand All @@ -245,13 +252,15 @@ async def recreate_mfa(self, user: User) -> OTPConfig:
return OTPConfig(secret=user_secret, recovery_codes=recovery_codes)

async def enable_mfa(self, user: User, otp: str) -> bool:
log.info(f"Enable MFA for user {user.email}")
assert not user.is_mfa_active, "User already has MFA enabled."
if (secret := user.otp_secret) and not pyotp.TOTP(secret).verify(otp, valid_window=1):
return False
await self.user_repository.update(user, {"is_mfa_active": True})
return True

async def disable_mfa(self, user: User, otp: Optional[str], recovery_code: Optional[str]) -> bool:
log.info(f"Disable MFA for user {user.email}")
if not user.is_mfa_active:
return True
if await self.check_otp(user, otp, recovery_code):
Expand Down

0 comments on commit 2df1206

Please sign in to comment.