Index: apps/mt-api/src/app/auth/auth.service.ts =================================================================== diff -u -rc434930351d0a3ec148b14837f65172719481858 -r08e6c591678c9f90c5b7e7031fa77e24d783c076 --- apps/mt-api/src/app/auth/auth.service.ts (.../auth.service.ts) (revision c434930351d0a3ec148b14837f65172719481858) +++ apps/mt-api/src/app/auth/auth.service.ts (.../auth.service.ts) (revision 08e6c591678c9f90c5b7e7031fa77e24d783c076) @@ -1,6 +1,7 @@ -import {HttpService, Injectable, Logger} from '@nestjs/common'; +import {Injectable, Logger} from '@nestjs/common'; import {Client, Issuer, TokenSet} from "openid-client"; import {ConfigService} from "@nestjs/config"; +import {Store} from "../store"; @Injectable() export class AuthService { @@ -9,11 +10,15 @@ private logger: Logger = new Logger('AuthService'); constructor( - private readonly http: HttpService, - private readonly config: ConfigService + private readonly config: ConfigService, + private readonly _store: Store, ) { } + get store() { + return this._store; + } + async client() { if (!this.Client) { const issuer = await Issuer.discover(this.config.get('KC_ISSUER')); @@ -28,21 +33,15 @@ return this.Client; } - get accessToken() { - return this._tokenSet.access_token; - } - get refreshToken() { - return this._tokenSet.refresh_token; - } - async login() { const Client = await this.client(); return Client.grant({ grant_type: 'client_credentials', + scope: 'offline_access', }).then((c) => { this.logger.debug('Get access token'); - this._tokenSet = c; + this.store.setToken(c); return c; }).catch(error => { this.logger.error(`Error login ${error}`); @@ -51,9 +50,9 @@ async refresh() { const Client = await this.client(); - return Client.refresh(this._tokenSet.refresh_token).then((c) => { + return Client.refresh(this.store.token.refresh_token).then((c) => { this.logger.debug('Refresh token'); - this._tokenSet = c; + this.store.setToken(c); return c; }).catch(error => { this.logger.error(`Error refreshing token ${error}`); @@ -62,22 +61,24 @@ } get isTokenExpired(): boolean { - return this._tokenSet.expired(); + return this.store.token.expired(); } async isActive() { const Client = await this.client(); - return Client.introspect(this.accessToken).then(({active}) => active); + return Client.introspect(this.store.accessToken).then(({active}) => active); } async tokenLife() { - const expiresIn = this._tokenSet.expires_in / 60; + const expiresIn = this.store.token.expires_in / 60; const lifeTime = Number(this.config.get('TOKEN_LIFETIME')); + + console.log('expiresIn', expiresIn, 'lifeTime', lifeTime); return Number((100 - (lifeTime - expiresIn) * 100 / lifeTime).toFixed(1)); } async logout() { const Client = await this.client(); - return await Client.revoke(this._tokenSet.access_token); + return await Client.revoke(this.store.accessToken); } }