Mercurial > hg > thermostat-ng > web-client
view src/app/components/auth/basic-auth.service.ts @ 274:bdbc30b9f43c default tip master
Alternative authService migration approach
Allows one service instance to be shared between Angular and AngularJS.
Reviewed-by: jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025509.html
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Thu, 26 Oct 2017 10:47:34 -0400 |
parents | f13e9da11350 |
children |
line wrap: on
line source
/** * Copyright 2012-2017 Red Hat, Inc. * * Thermostat is distributed under the GNU General Public License, * version 2 or any later version (with a special exception described * below, commonly known as the "Classpath Exception"). * * A copy of GNU General Public License (GPL) is included in this * distribution, in the file COPYING. * * Linking Thermostat code with other modules is making a combined work * based on Thermostat. Thus, the terms and conditions of the GPL * cover the whole combination. * * As a special exception, the copyright holders of Thermostat give you * permission to link this code with independent modules to produce an * executable, regardless of the license terms of these independent * modules, and to copy and distribute the resulting executable under * terms of your choice, provided that you also meet, for each linked * independent module, the terms and conditions of the license of that * module. An independent module is a module which is not derived from * or based on Thermostat code. If you modify Thermostat, you may * extend this exception to your version of the software, but you are * not obligated to do so. If you do not wish to do so, delete this * exception statement from your version. */ import * as angular from "angular"; import { Injectable } from "@angular/core"; import * as url from "url"; import { IAuthService } from "./auth-service.interface"; import { StateService } from "@uirouter/angularjs"; import { LocalStorageService } from "../../shared/services/local-storage.service.js"; const SESSION_EXPIRY_MINUTES = 15; @Injectable() export class BasicAuthService implements IAuthService { private q: ng.IQService; private $state: StateService; private localStorage: LocalStorageService; private pass: string = null; private scope: ng.IRootScopeService; private remember: boolean; public constructor($injector: ng.auto.IInjectorService) { this.q = $injector.get("$q"); this.$state = $injector.get("$state"); this.localStorage = $injector.get("localStorage"); this.pass = null; } public set rootScope(rootScope: ng.IRootScopeService) { this.scope = rootScope; } public status(): boolean { return this._sessionIsValid(); } public login(user: string, pass: string, success = angular.noop): void { this.pass = pass; if (this.remember) { this.localStorage.setItem("username", user); } this.localStorage.setItem("loggedInUser", user); this._refreshSession(); this.scope.$broadcast("userLoginChanged"); success(); } public goToLogin(promise: ng.IDeferred<{}>): void { promise.resolve(this.$state.target("login")); } public logout(callback = angular.noop): void { this.pass = null; this.$state.go("login"); this.localStorage.removeItem("session"); this.localStorage.removeItem("loggedInUser"); this.scope.$broadcast("userLoginChanged"); callback(); } public refresh(): ng.IPromise<{}> { const defer = this.q.defer(); if (this._sessionIsValid()) { this._refreshSession(); defer.resolve(); } else { this.localStorage.removeItem("session"); this.localStorage.removeItem("loggedInUser"); defer.reject(); } return defer.promise; } public get authHeader(): string { return "Basic " + btoa(this.username + ":" + this.pass); } public get username(): string { return this.localStorage.getItem("loggedInUser"); } public get rememberedUsername(): string { return this.localStorage.getItem("username"); } public getCommandChannelUrl(baseUrl: string): string { const parsed = url.parse(baseUrl); if (this.username == null && this.pass == null) { // no-op } if (this.username != null && this.pass == null) { parsed.auth = this.username; } if (this.username != null && this.pass != null) { parsed.auth = this.username + ":" + this.pass; } return url.format(parsed); } public rememberUser(rememberUser: boolean): void { this.remember = rememberUser; if (!this.remember) { this.localStorage.removeItem("username"); } } private _refreshSession(): void { const now = new Date(); const expiry = new Date(now); expiry.setMinutes(now.getMinutes() + SESSION_EXPIRY_MINUTES); this.localStorage.setItem("session", expiry); } private _sessionIsValid(): boolean { if (!this.localStorage.hasItem("session")) { return false; } const session = new Date(this.localStorage.getItem("session")); const now = new Date(); return session.getTime() >= now.getTime(); } }