Files
standardnotes-server/packages/auth/src/Domain/UseCase/DeleteOtherSessionsForUser.ts
T
Karol Sójko f39d3aca5b feat(auth): invalidate other sessions for user if the email or password are changed (#684)
* feat(auth): invalidate other sessions for user if the email or password are changed

* fix(auth): handling credentials change in a legacy protocol scenario

* fix(auth): leave only the newly created session when changing credentials
2023-08-07 10:02:47 +02:00

47 lines
1.7 KiB
TypeScript

import { inject, injectable } from 'inversify'
import { Result, UseCaseInterface, Uuid } from '@standardnotes/domain-core'
import TYPES from '../../Bootstrap/Types'
import { Session } from '../Session/Session'
import { SessionRepositoryInterface } from '../Session/SessionRepositoryInterface'
import { SessionServiceInterface } from '../Session/SessionServiceInterface'
import { DeleteOtherSessionsForUserDTO } from './DeleteOtherSessionsForUserDTO'
@injectable()
export class DeleteOtherSessionsForUser implements UseCaseInterface<void> {
constructor(
@inject(TYPES.Auth_SessionRepository) private sessionRepository: SessionRepositoryInterface,
@inject(TYPES.Auth_SessionService) private sessionService: SessionServiceInterface,
) {}
async execute(dto: DeleteOtherSessionsForUserDTO): Promise<Result<void>> {
const userUuidOrError = Uuid.create(dto.userUuid)
if (userUuidOrError.isFailed()) {
return Result.fail(userUuidOrError.getError())
}
const userUuid = userUuidOrError.getValue()
const currentSessionUuidOrError = Uuid.create(dto.currentSessionUuid)
if (currentSessionUuidOrError.isFailed()) {
return Result.fail(currentSessionUuidOrError.getError())
}
const currentSessionUuid = currentSessionUuidOrError.getValue()
const sessions = await this.sessionRepository.findAllByUserUuid(dto.userUuid)
if (dto.markAsRevoked) {
await Promise.all(
sessions.map(async (session: Session) => {
if (session.uuid !== currentSessionUuid.value) {
await this.sessionService.createRevokedSession(session)
}
}),
)
}
await this.sessionRepository.deleteAllByUserUuidExceptOne({ userUuid, currentSessionUuid })
return Result.ok()
}
}