UNPKG

lavva.exalushome

Version:

Library implementing communication and abstraction layers for ExalusHome system

257 lines 13.3 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Api } from "../../Api"; import { ExalusConnectionService } from "../ExalusConnectionService"; import { TypedEvent } from "../../TypedEvent"; import { AccessLevel, Gender } from '../Users/IUser'; import { LoginError } from "./ISessionService"; import { DataFrame, Method, Status } from '../../DataFrame'; import { DependencyContainer } from "../../DependencyContainer"; import { User } from "../Users/User"; import { ControllerConfigurationService } from "../Controller/ControllerConfigurationService"; import { WebApiCacheService } from "../WebApi/WebApiCacheService"; export class SessionService { constructor() { this._onUserLoggedInEvent = new TypedEvent(); this._onUserLoggedOutEvent = new TypedEvent(); this._email = ""; this._password = ""; this._authToken = ""; this._alreadySubsribedToNetworkEvents = false; this._isLoggedIn = false; this._user = null; } isLoginError(value) { // Enums in TS are numbers at runtime; check both type and that it's a valid enum member return typeof value === "number" && LoginError[value] !== undefined; } RestoreSessionAsync() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `Creating RestoreSessionAsync ${this._email} ${this._password}`); if (this._user === undefined || this._user === null || !this._isLoggedIn) return false; let res = yield this.UserLogInAsync(this._email, this._password); if (this.isLoginError(res)) { (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Error(SessionService.ServiceName, `RestoreSessionAsync failed, user login failed`); if (res == LoginError.WrongAuthData) throw new Error("Wrong authentication data!"); return false; } (_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Debug(SessionService.ServiceName, `RestoreSessionAsync completed successfully`); return true; }); } WaitForSessionCreationAsync() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { if (this._isLoggedIn && this._user != null) { return resolve(); } const h = (user) => { this._onUserLoggedInEvent.Unsubscribe(h); resolve(); }; this._onUserLoggedInEvent.Subscribe(h); }); }); } OnUserLoggedInEvent() { return this._onUserLoggedInEvent; } OnUserLoggedOutEvent() { return this._onUserLoggedOutEvent; } get User() { return this._user; } UserLogOutAsync() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; let logoutRequest = new DataFrame(); logoutRequest.Resource = "/users/user/logout"; logoutRequest.Method = Method.Put; this._email = ""; this._password = ""; const result = yield Api.Get(ExalusConnectionService.ServiceName) .SendAndWaitForResponseAsync(logoutRequest, 20000, false); if (result.Status == Status.OK) { this._onUserLoggedOutEvent.Invoke(this._user); (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, "User has been logged out."); this._user = null; Api.Get(WebApiCacheService.ServiceName).ClearCache(); } else (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, "Failed to logout user!"); }); } AuthorizeAppAsync(authToken) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e; let connection = Api.Get(ExalusConnectionService.ServiceName); const result = yield connection.SendAndWaitForResponseAsync(new AuthorizeAppRequest(authToken), 20000, false); if (result.Status == Status.OK && result.Data != null) { const shouldClearCache = !this._isLoggedIn || ((_a = this._user) === null || _a === void 0 ? void 0 : _a.Guid) !== result.Data.Guid; this._authToken = authToken; let user = new User(); this._user = user; user.AccessLevel = result.Data.AccessLevel; user.Email = result.Data.Email; this._email = result.Data.Email; user.Gender = result.Data.Gender; user.Guid = result.Data.Guid; user.IsAccountOnline = result.Data.IsAccountOnline; user.Name = result.Data.Name; user.IsActive = result.Data.IsActive; user.IsBanned = result.Data.IsBanned; user.Phone = result.Data.Phone; user.Surname = result.Data.Surname; user.SoftwareVersion = result.Data.SoftwareVersion; let conf = Api.Get(ControllerConfigurationService.ServiceName); if (result.Data.ConfigurationTime !== undefined && result.Data.ConfigurationTime != null) { ControllerConfigurationService.ConfigurationTimeRetrieved = new Date(result.Data.ConfigurationTime); yield conf.CheckIfConfigurationTimeHasChangedAsync(new Date(result.Data.ConfigurationTime)); } else yield conf.CheckIfConfigurationHasChangedAsync(); if (shouldClearCache) Api.Get(WebApiCacheService.ServiceName).ClearCache(); if (!this._alreadySubsribedToNetworkEvents) { this._alreadySubsribedToNetworkEvents = true; Api.Get(ExalusConnectionService.ServiceName).SubscribeTo("/info/users/user/loggedOut", (data) => __awaiter(this, void 0, void 0, function* () { var _a, _b; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `User logged out: ${JSON.stringify(data.Data)}`); this._isLoggedIn = false; (_b = this._onUserLoggedOutEvent) === null || _b === void 0 ? void 0 : _b.Invoke(this._user); this._user = null; Api.Get(WebApiCacheService.ServiceName).ClearCache(); })); } (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, `Did logging in succeded?: ${result.Status == Status.OK}`); this._isLoggedIn = true; this._onUserLoggedInEvent.Invoke(this._user); return user; } else if (result.Status === Status.OperationNotPermitted) { (_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Debug(SessionService.ServiceName, `App login response: ${result.Data}`); return LoginError.AuthDisabled; } else if (result.Status === Status.ResourceDoesNotExists) { (_d = DependencyContainer.Log) === null || _d === void 0 ? void 0 : _d.Debug(SessionService.ServiceName, `App login response: ${result.Data}`); return LoginError.MethodNotSupported; } else { (_e = DependencyContainer.Log) === null || _e === void 0 ? void 0 : _e.Debug(SessionService.ServiceName, `App login response: ${result.Data}`); return LoginError.WrongAuthData; } }); } UserLogInAsync(email, password) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; let connection = Api.Get(ExalusConnectionService.ServiceName); const result = yield connection.SendAndWaitForResponseAsync(new LoginUserRequest(email, password), 20000, false); if (result.Status == Status.OK && result.Data != null) { const shouldClearCache = !this._isLoggedIn || ((_a = this._user) === null || _a === void 0 ? void 0 : _a.Guid) !== result.Data.Guid; this._email = email; this._password = password; let user = new User(); this._user = user; user.AccessLevel = result.Data.AccessLevel; user.Email = result.Data.Email; user.Gender = result.Data.Gender; user.Guid = result.Data.Guid; user.IsAccountOnline = result.Data.IsAccountOnline; user.Name = result.Data.Name; user.IsActive = result.Data.IsActive; user.IsBanned = result.Data.IsBanned; user.Phone = result.Data.Phone; user.Surname = result.Data.Surname; user.SoftwareVersion = result.Data.SoftwareVersion; let conf = Api.Get(ControllerConfigurationService.ServiceName); if (result.Data.ConfigurationTime !== undefined && result.Data.ConfigurationTime != null) { ControllerConfigurationService.ConfigurationTimeRetrieved = new Date(result.Data.ConfigurationTime); yield conf.CheckIfConfigurationTimeHasChangedAsync(new Date(result.Data.ConfigurationTime)); } else yield conf.CheckIfConfigurationHasChangedAsync(); if (shouldClearCache) Api.Get(WebApiCacheService.ServiceName).ClearCache(); if (!this._alreadySubsribedToNetworkEvents) { this._alreadySubsribedToNetworkEvents = true; Api.Get(ExalusConnectionService.ServiceName).SubscribeTo("/info/users/user/loggedOut", (data) => __awaiter(this, void 0, void 0, function* () { var _a, _b; (_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug(SessionService.ServiceName, `User logged out: ${JSON.stringify(data.Data)}`); this._isLoggedIn = false; (_b = this._onUserLoggedOutEvent) === null || _b === void 0 ? void 0 : _b.Invoke(this._user); this._user = null; Api.Get(WebApiCacheService.ServiceName).ClearCache(); })); } (_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug(SessionService.ServiceName, `Did logging in succeded?: ${result.Status == Status.OK}`); this._isLoggedIn = true; this._onUserLoggedInEvent.Invoke(this._user); return user; } else { (_c = DependencyContainer.Log) === null || _c === void 0 ? void 0 : _c.Debug(SessionService.ServiceName, `User login response: ${result.Data}`); return LoginError.WrongAuthData; } }); } GetServiceName() { return SessionService.ServiceName; } } SessionService.ServiceName = "SessionService"; export class UserData { constructor() { this.Guid = ""; this.Name = ""; this.Surname = ""; this.Password = ""; this.Email = ""; this.Phone = ""; this.IsActive = true; this.IsBanned = false; this.IsAccountOnline = false; this.AccessLevel = AccessLevel.Any; this.Gender = Gender.Unknown; this.ConfigurationTime = ""; this.IsGeolocationSet = false; this.SoftwareVersion = ""; this.UpdatesChannel = ""; } } class UserLoginData { constructor(email, password) { this.Email = ""; this.Password = ""; this.Email = email; this.Password = password; } } class AuthorizeAppRequest extends DataFrame { constructor(authToken) { super(); this.Data = authToken; this.Resource = "/applications/app/auth"; this.Method = Method.Put; } } class LoginUserRequest extends DataFrame { constructor(email, password) { super(); this.Data = new UserLoginData(email, password); this.Resource = "/users/user/login"; this.Method = Method.Put; } } //# sourceMappingURL=SessionService.js.map