UNPKG

homebridge-config-ui-x

Version:

A web based management, configuration and control platform for Homebridge.

66 lines 2.62 kB
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); } }; import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '../../config/config.service.js'; let CookieAuthGuard = class CookieAuthGuard { jwtService; configService; constructor(jwtService, configService) { this.jwtService = jwtService; this.configService = configService; } canActivate(context) { if (this.configService.ui.auth === 'none') { return true; } const request = context.switchToHttp().getRequest(); const token = this.extractTokenFromCookie(request.headers?.cookie); if (!token) { throw new UnauthorizedException(); } try { this.jwtService.verify(token); return true; } catch { throw new UnauthorizedException(); } } extractTokenFromCookie(cookieHeader) { if (!cookieHeader) { return null; } for (const part of cookieHeader.split(';')) { const eqIndex = part.indexOf('='); if (eqIndex === -1) { continue; } const key = part.slice(0, eqIndex).trim(); const value = part.slice(eqIndex + 1).trim(); if (key === 'hb-session' && value) { return value; } } return null; } }; CookieAuthGuard = __decorate([ Injectable(), __param(0, Inject(JwtService)), __param(1, Inject(ConfigService)), __metadata("design:paramtypes", [JwtService, ConfigService]) ], CookieAuthGuard); export { CookieAuthGuard }; //# sourceMappingURL=cookie-auth.guard.js.map