UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

88 lines 4.23 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var AuthService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthService = void 0; const common_1 = require("@nestjs/common"); const cqrs_1 = require("@nestjs/cqrs"); const rxjs_1 = require("rxjs"); const ts_md5_1 = require("ts-md5"); const uuid_1 = require("uuid"); const auth_providers_1 = require("./auth.providers"); const events_1 = require("./events"); let AuthService = AuthService_1 = class AuthService { constructor(eventBus, refreshMargin) { this.eventBus = eventBus; this.refreshMargin = refreshMargin; this.logger = new common_1.Logger(AuthService_1.name); this.subscriptions = []; this.authState = {}; this.credentials = new rxjs_1.Subject(); this.authData = new rxjs_1.Subject(); this.communityData = new rxjs_1.Subject(); this.subscriptions.push(this.credentials .pipe((0, rxjs_1.filter)((creds) => creds !== undefined), (0, rxjs_1.map)((creds) => ({ ...creds, clientId: creds?.clientId ?? this.newClientId, })), (0, rxjs_1.map)((creds) => new events_1.CredentialsChangedEvent(creds))) .subscribe((event) => { this.eventBus.publish(event); }), (0, rxjs_1.combineLatest)([this.authData, this.communityData]) .pipe((0, rxjs_1.filter)((value) => value !== undefined), (0, rxjs_1.map)((value) => value), (0, rxjs_1.tap)(([authData, communityData]) => { this.authState.accountAuth = authData; this.authState.bffAuth = communityData; }), (0, rxjs_1.tap)(([authData, communityData]) => this.eventBus.publish(new events_1.AuthenticatedEvent(authData.accountId, authData.clientId, authData.oauth, communityData))), (0, rxjs_1.switchMap)(([authData]) => (0, rxjs_1.timer)(Math.max(0, authData.oauth.expiresAt - this.refreshMargin - Date.now())).pipe((0, rxjs_1.map)(() => new events_1.AuthExpiringEvent(authData.accountId, authData.clientId, authData.oauth))))) .subscribe((event) => this.eventBus.publish(event))); } get newClientId() { return ts_md5_1.Md5.hashStr(Buffer.from((0, uuid_1.v4)() + new Date().getMilliseconds().toString()).toString('utf8'), false); } get isAuthExpired() { if (!this.state.accountAuth?.oauth?.expiresAt) { return true; } return this.expiresAt < Date.now(); } get expiresAt() { return this.state.accountAuth?.oauth?.expiresAt ?? Date.now(); } get state() { return this.authState; } get accountAuth() { return this.state; } setCredentials(credentials) { this.credentials.next(credentials); } setAuthData(authData) { this.authData.next(authData); } setCommunityData(authData) { this.communityData.next(authData); } closeSubscriptions() { this.subscriptions.forEach((sub) => sub.unsubscribe); } onModuleDestroy() { this.closeSubscriptions(); } }; exports.AuthService = AuthService; exports.AuthService = AuthService = AuthService_1 = __decorate([ (0, common_1.Injectable)(), __param(1, auth_providers_1.InjectRefreshMargin), __metadata("design:paramtypes", [cqrs_1.EventBus, Number]) ], AuthService); //# sourceMappingURL=auth.service.js.map