5554-renovar_token #1566

Merged
vicent merged 24 commits from 5554-renovar_token into dev 2023-06-20 12:38:47 +00:00
8 changed files with 36 additions and 73 deletions
Showing only changes of commit 3fdb546d86 - Show all commits

View File

@ -1,6 +1,8 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('renewToken', {
description: 'Send email to the user',
description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it',
accepts: [],
returns: {
type: 'Object',
@ -22,16 +24,10 @@ module.exports = Self => {
const differenceMilliseconds = now - created;
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
vicent marked this conversation as resolved Outdated
Outdated
Review

Especificar fields siempre que se haga un select, de lo contrario se seleccionan todas las columnas.

Especificar fields siempre que se haga un select, de lo contrario se seleccionan todas las columnas.
const accessTokenConfig = await models.AccessTokenConfig.findOne();
if (differenceSeconds <= accessTokenConfig.renewPeriod) {
const response = {
statusCode: 200,
data: {
message: 'Token is active',
}
};
return response;
}
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']});
if (differenceSeconds <= accessTokenConfig.renewPeriod)
throw new UserError(`The renew period has not been exceeded`);
await Self.logout(tokenId);
const user = await Self.findById(userId);
vicent marked this conversation as resolved Outdated
Outdated
Review

Devolver un 'UserError' si todavía no se ha pasado el tiempo de renovación.

Devolver un 'UserError' si todavía no se ha pasado el tiempo de renovación.

View File

@ -7,7 +7,7 @@ import UserError from 'core/lib/user-error';
* @property {Boolean} loggedIn Whether the user is currently logged
*/
export default class Auth {
constructor($http, $q, $state, $transitions, $window, vnToken, vnTokenCreated, vnModules, aclService) {
constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) {
Object.assign(this, {
$http,
$q,
@ -15,7 +15,6 @@ export default class Auth {
$transitions,
$window,
vnToken,
vnTokenCreated,
vnModules,
aclService,
loggedIn: false
@ -65,8 +64,7 @@ export default class Auth {
}
onLoginOk(json, remember) {
this.vnToken.set(json.data.token, remember);
this.vnTokenCreated.set(json.data.created, remember);
this.vnToken.set(json.data.token, json.data.created, remember);
return this.loadAcls().then(() => {
let continueHash = this.$state.params.continue;
@ -103,6 +101,6 @@ export default class Auth {
});
}
}
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService'];
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService'];
ngModule.service('vnAuth', Auth);

View File

@ -11,5 +11,4 @@ import './report';
import './email';
import './file';
import './date';
import './token-created';

View File

@ -1,34 +0,0 @@
import ngModule from '../module';
/**
* Saves and loads the created for the current logged in user.
*
* @property {String} created The current login created or %null
*/
export default class created {
constructor() {
try {
this.created = sessionStorage.getItem('vnTokenCreated');
if (!this.created)
this.created = localStorage.getItem('vnTokenCreated');
} catch (e) {}
}
set(value, remember) {
this.unset();
try {
if (remember)
localStorage.setItem('vnTokenCreated', value);
else
sessionStorage.setItem('vnTokenCreated', value);
} catch (e) {}
this.created = value;
}
unset() {
localStorage.removeItem('vnTokenCreated');
sessionStorage.removeItem('vnTokenCreated');
this.created = null;
}
}
ngModule.service('vnTokenCreated', created);

View File

@ -9,25 +9,33 @@ export default class Token {
constructor() {
try {
this.token = sessionStorage.getItem('vnToken');
if (!this.token)
this.created = sessionStorage.getItem('vnTokenCreated');
if (!this.token) {
this.token = localStorage.getItem('vnToken');
this.created = localStorage.getItem('vnTokenCreated');
}
} catch (e) {}
}
set(value, remember) {
set(token, created, remember) {
this.unset();
try {
if (remember)
localStorage.setItem('vnToken', value);
else
sessionStorage.setItem('vnToken', value);
if (remember) {
localStorage.setItem('vnToken', token);
localStorage.setItem('vnTokenCreated', created);
} else {
sessionStorage.setItem('vnToken', token);
sessionStorage.setItem('vnTokenCreated', created);
}
} catch (e) {}
this.token = value;
this.token = token;
this.created = created;
}
unset() {
localStorage.removeItem('vnToken');
sessionStorage.removeItem('vnToken');
this.token = null;
this.created = null;
}
}

View File

@ -3,13 +3,9 @@ import Component from 'core/lib/component';
import './style.scss';
export class Layout extends Component {
constructor($element, $, vnModules, vnToken, vnTokenCreated) {
constructor($element, $, vnModules, vnToken) {
super($element, $);
this.modules = vnModules.get();
Object.assign(this, {
vnToken,
vnTokenCreated
});
}
$onInit() {
@ -50,7 +46,7 @@ export class Layout extends Component {
checkTokenValidity() {
const now = new Date();
const differenceMilliseconds = now - new Date(this.vnTokenCreated.created);
const differenceMilliseconds = now - new Date(this.vnToken.created);
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
if (differenceSeconds > window.localStorage.renewPeriod) {
@ -60,8 +56,7 @@ export class Layout extends Component {
let remember = true;
if (window.sessionStorage.vnToken) remember = false;
this.vnToken.set(json.data.token, remember);
this.vnTokenCreated.set(json.data.created, remember);
this.vnToken.set(json.data.token, json.data.created, remember);
}
});
}
@ -71,7 +66,7 @@ export class Layout extends Component {
clearInterval(this.inservalId);
}
}
Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated'];
Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken'];
ngModule.vnComponent('vnLayout', {
template: require('./index.html'),

View File

@ -61,28 +61,28 @@ describe('Component vnLayout', () => {
describe('checkTokenValidity()', () => {
it(`should not call renewToken and not set vnToken in the controller`, () => {
localStorage.setItem('renewPeriod', 100);
controller.vnTokenCreated.created = new Date();
controller.vnToken.created = new Date();
controller.checkTokenValidity();
expect(controller.vnToken.token).toBeNull();
});
it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => {
it(`should call renewToken and set vnToken properties in the controller`, () => {
const response = {
token: 999,
created: new Date()
};
localStorage.setItem('renewPeriod', 100);
const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000));
controller.vnTokenCreated.created = oneHourBefore;
controller.vnToken.created = oneHourBefore;
$httpBackend.expect('POST', `VnUsers/renewToken`).respond(response);
controller.checkTokenValidity();
$httpBackend.flush();
expect(controller.vnToken.token).toBe(999);
expect(controller.vnTokenCreated.created).toEqual(response.created);
expect(controller.vnToken.created).toEqual(response.created);
});
});
});

View File

@ -84,7 +84,7 @@
"The current ticket can't be modified": "El ticket actual no puede ser modificado",
"The current claim can't be modified": "La reclamación actual no puede ser modificada",
"The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas",
"The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)",
"The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)",
"Please select at least one sale": "Por favor selecciona al menos una linea",
"All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket",
"NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada",
@ -293,5 +293,6 @@
"comercialName": "Comercial",
"Invalid NIF for VIES": "Invalid NIF for VIES",
"Ticket does not exist": "Este ticket no existe",
"Ticket is already signed": "Este ticket ya ha sido firmado"
"Ticket is already signed": "Este ticket ya ha sido firmado",
"The renew period has not been exceeded": "El periodo de renovación no ha sido superado"
}