UNPKG

@panoptic-it-solutions/unifi-api-client

Version:

A Node.js client library for the UniFi Controller API

127 lines (126 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UniFiAuth = void 0; const auth_session_storage_1 = require("./auth-session-storage"); class UniFiAuth { constructor(httpClient, site = 'default') { this.isLoggedIn = false; this.sessionStorage = new auth_session_storage_1.SessionStorage(); this.twoFactorRequired = false; this.httpClient = httpClient; this.site = site; } async login(username, password, twoFactorCode) { this.credentials = { username, password }; try { const response = await this.httpClient.post('/api/login', { username, password, remember: true, ...(twoFactorCode ? { token: twoFactorCode } : {}), }); if (response && response.meta && response.meta.rc === 'error' && response.meta.msg && response.meta.msg.includes('2fa')) { this.twoFactorRequired = true; this.twoFactorSessionId = response.meta.session_id; return false; } this.isLoggedIn = response && response.meta && response.meta.rc === 'ok'; this.twoFactorRequired = false; return this.isLoggedIn; } catch (error) { this.isLoggedIn = false; throw new Error(`Authentication failed: ${error.message}`); } } async logout() { try { const response = await this.httpClient.post('/api/logout'); this.isLoggedIn = false; this.httpClient.clearCookies(); return response && response.meta && response.meta.rc === 'ok'; } catch (error) { throw new Error(`Logout failed: ${error.message}`); } } isAuthenticated() { return this.isLoggedIn; } getSite() { return this.site; } setSite(site) { this.site = site; } async validateSession() { try { // Example: GET /api/self returns user info if session is valid const response = await this.httpClient.get('/api/self'); return response && response.meta && response.meta.rc === 'ok'; } catch (_a) { return false; } } async refreshSession() { if (this.credentials) { return this.login(this.credentials.username, this.credentials.password); } return false; } canReuseCredentials() { return !!this.credentials; } saveSession() { const session = { cookies: this.httpClient.cookies, credentials: this.credentials, site: this.site, expiresAt: this.sessionExpiresAt, }; this.sessionStorage.save(session); } restoreSession() { const session = this.sessionStorage.load(); if (!session) return false; this.httpClient.cookies = session.cookies || {}; this.credentials = session.credentials; this.site = session.site; this.sessionExpiresAt = session.expiresAt; this.isLoggedIn = !!session.cookies && !!session.credentials; return this.isLoggedIn; } setSessionExpiration(expiryMs) { this.sessionExpiresAt = Date.now() + expiryMs; } isSessionExpired() { return (this.sessionExpiresAt !== undefined && Date.now() > this.sessionExpiresAt); } isTwoFactorRequired() { return this.twoFactorRequired; } async submitTwoFactorCode(code) { if (!this.twoFactorSessionId) throw new Error('No 2FA session in progress'); try { const response = await this.httpClient.post('/api/login', { token: code, session_id: this.twoFactorSessionId, }); this.isLoggedIn = response && response.meta && response.meta.rc === 'ok'; this.twoFactorRequired = false; return this.isLoggedIn; } catch (error) { this.isLoggedIn = false; throw new Error(`2FA failed: ${error.message}`); } } } exports.UniFiAuth = UniFiAuth;