UNPKG

matterbridge-roborock-vacuum-plugin

Version:
73 lines 3.56 kB
import { debugStringify } from 'matterbridge/logger'; import { AuthenticationError, InvalidCredentialsError, TokenExpiredError, VerificationCodeExpiredError, } from '../errors/index.js'; /** Core authentication service handling low-level login operations. */ export class AuthenticationService { authGateway; logger; constructor(authGateway, logger) { this.authGateway = authGateway; this.logger = logger; } /** Login with email verification code. */ async loginWithVerificationCode(email, code) { try { this.logger.debug('Authenticating with verification code for email:', email); const userdata = await this.authGateway.authenticate2FA(email, code); this.logger.notice('Successfully authenticated with verification code'); return userdata; } catch (error) { this.logger.error(`Failed to login with verification code: ${debugStringify({ email, error })}`); const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('expired') || errorMessage.includes('invalid')) { throw new VerificationCodeExpiredError(email); } throw new AuthenticationError('Authentication failed. Please verify your email and code are correct.', { email, originalError: errorMessage, }); } } /** Login with cached token (validates and refreshes if needed). */ async loginWithCachedToken(username, userData) { try { this.logger.debug('Authenticating with cached token for user:', username); userData.username = username; const validatedUserData = await this.authGateway.refreshToken(userData); this.logger.notice('[loginWithCachedToken]: Successfully authenticated with cached token'); return validatedUserData; } catch (error) { this.logger.error('Failed to authenticate with cached token:', { username, error }); const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('expired') || errorMessage.includes('token')) { throw new TokenExpiredError(); } throw new AuthenticationError('Session expired or invalid. Please login again.', { username, originalError: errorMessage, }); } } /** Login with password. */ async loginWithPassword(username, password) { try { this.logger.debug('Authenticating with password for user:', username); const newUserData = await this.authGateway.authenticatePassword(username, password); this.logger.notice('Successfully authenticated with password'); return newUserData; } catch (error) { this.logger.error('Failed to login with password:', { username, error }); const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('invalid') || errorMessage.includes('incorrect') || errorMessage.includes('wrong')) { throw new InvalidCredentialsError(username); } throw new AuthenticationError('Authentication failed. Please use verification code login.', { username, originalError: errorMessage, }); } } } //# sourceMappingURL=authenticationService.js.map