5554-renovar_token #1566
|
@ -1,6 +1,8 @@
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('renewToken', {
|
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: [],
|
accepts: [],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'Object',
|
type: 'Object',
|
||||||
|
@ -22,16 +24,10 @@ module.exports = Self => {
|
||||||
const differenceMilliseconds = now - created;
|
const differenceMilliseconds = now - created;
|
||||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||||
|
|
||||||
const accessTokenConfig = await models.AccessTokenConfig.findOne();
|
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']});
|
||||||
if (differenceSeconds <= accessTokenConfig.renewPeriod) {
|
|
||||||
const response = {
|
if (differenceSeconds <= accessTokenConfig.renewPeriod)
|
||||||
statusCode: 200,
|
throw new UserError(`The renew period has not been exceeded`);
|
||||||
data: {
|
|
||||||
message: 'Token is active',
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
await Self.logout(tokenId);
|
await Self.logout(tokenId);
|
||||||
const user = await Self.findById(userId);
|
const user = await Self.findById(userId);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import UserError from 'core/lib/user-error';
|
||||||
* @property {Boolean} loggedIn Whether the user is currently logged
|
* @property {Boolean} loggedIn Whether the user is currently logged
|
||||||
*/
|
*/
|
||||||
export default class Auth {
|
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, {
|
Object.assign(this, {
|
||||||
$http,
|
$http,
|
||||||
$q,
|
$q,
|
||||||
|
@ -15,7 +15,6 @@ export default class Auth {
|
||||||
$transitions,
|
$transitions,
|
||||||
$window,
|
$window,
|
||||||
vnToken,
|
vnToken,
|
||||||
vnTokenCreated,
|
|
||||||
vnModules,
|
vnModules,
|
||||||
aclService,
|
aclService,
|
||||||
loggedIn: false
|
loggedIn: false
|
||||||
|
@ -65,8 +64,7 @@ export default class Auth {
|
||||||
}
|
}
|
||||||
|
|
||||||
onLoginOk(json, remember) {
|
onLoginOk(json, remember) {
|
||||||
this.vnToken.set(json.data.token, remember);
|
this.vnToken.set(json.data.token, json.data.created, remember);
|
||||||
this.vnTokenCreated.set(json.data.created, remember);
|
|
||||||
|
|
||||||
return this.loadAcls().then(() => {
|
return this.loadAcls().then(() => {
|
||||||
let continueHash = this.$state.params.continue;
|
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);
|
ngModule.service('vnAuth', Auth);
|
||||||
|
|
|
@ -11,5 +11,4 @@ import './report';
|
||||||
import './email';
|
import './email';
|
||||||
import './file';
|
import './file';
|
||||||
import './date';
|
import './date';
|
||||||
import './token-created';
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
|
|
@ -9,25 +9,33 @@ export default class Token {
|
||||||
constructor() {
|
constructor() {
|
||||||
try {
|
try {
|
||||||
this.token = sessionStorage.getItem('vnToken');
|
this.token = sessionStorage.getItem('vnToken');
|
||||||
if (!this.token)
|
this.created = sessionStorage.getItem('vnTokenCreated');
|
||||||
|
if (!this.token) {
|
||||||
this.token = localStorage.getItem('vnToken');
|
this.token = localStorage.getItem('vnToken');
|
||||||
|
this.created = localStorage.getItem('vnTokenCreated');
|
||||||
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
set(value, remember) {
|
set(token, created, remember) {
|
||||||
this.unset();
|
this.unset();
|
||||||
try {
|
try {
|
||||||
if (remember)
|
if (remember) {
|
||||||
localStorage.setItem('vnToken', value);
|
localStorage.setItem('vnToken', token);
|
||||||
else
|
localStorage.setItem('vnTokenCreated', created);
|
||||||
sessionStorage.setItem('vnToken', value);
|
} else {
|
||||||
|
sessionStorage.setItem('vnToken', token);
|
||||||
|
sessionStorage.setItem('vnTokenCreated', created);
|
||||||
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
this.token = value;
|
this.token = token;
|
||||||
|
this.created = created;
|
||||||
}
|
}
|
||||||
unset() {
|
unset() {
|
||||||
localStorage.removeItem('vnToken');
|
localStorage.removeItem('vnToken');
|
||||||
sessionStorage.removeItem('vnToken');
|
sessionStorage.removeItem('vnToken');
|
||||||
this.token = null;
|
this.token = null;
|
||||||
|
this.created = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,9 @@ import Component from 'core/lib/component';
|
||||||
import './style.scss';
|
import './style.scss';
|
||||||
|
|
||||||
export class Layout extends Component {
|
export class Layout extends Component {
|
||||||
constructor($element, $, vnModules, vnToken, vnTokenCreated) {
|
constructor($element, $, vnModules, vnToken) {
|
||||||
super($element, $);
|
super($element, $);
|
||||||
this.modules = vnModules.get();
|
this.modules = vnModules.get();
|
||||||
Object.assign(this, {
|
|
||||||
vnToken,
|
|
||||||
vnTokenCreated
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$onInit() {
|
$onInit() {
|
||||||
|
@ -50,7 +46,7 @@ export class Layout extends Component {
|
||||||
|
|
||||||
checkTokenValidity() {
|
checkTokenValidity() {
|
||||||
const now = new Date();
|
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);
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||||
|
|
||||||
if (differenceSeconds > window.localStorage.renewPeriod) {
|
if (differenceSeconds > window.localStorage.renewPeriod) {
|
||||||
|
@ -60,8 +56,7 @@ export class Layout extends Component {
|
||||||
let remember = true;
|
let remember = true;
|
||||||
if (window.sessionStorage.vnToken) remember = false;
|
if (window.sessionStorage.vnToken) remember = false;
|
||||||
|
|
||||||
this.vnToken.set(json.data.token, remember);
|
this.vnToken.set(json.data.token, json.data.created, remember);
|
||||||
this.vnTokenCreated.set(json.data.created, remember);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -71,7 +66,7 @@ export class Layout extends Component {
|
||||||
clearInterval(this.inservalId);
|
clearInterval(this.inservalId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated'];
|
Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken'];
|
||||||
|
|
||||||
ngModule.vnComponent('vnLayout', {
|
ngModule.vnComponent('vnLayout', {
|
||||||
template: require('./index.html'),
|
template: require('./index.html'),
|
||||||
|
|
|
@ -61,28 +61,28 @@ describe('Component vnLayout', () => {
|
||||||
describe('checkTokenValidity()', () => {
|
describe('checkTokenValidity()', () => {
|
||||||
it(`should not call renewToken and not set vnToken in the controller`, () => {
|
it(`should not call renewToken and not set vnToken in the controller`, () => {
|
||||||
localStorage.setItem('renewPeriod', 100);
|
localStorage.setItem('renewPeriod', 100);
|
||||||
controller.vnTokenCreated.created = new Date();
|
controller.vnToken.created = new Date();
|
||||||
|
|
||||||
controller.checkTokenValidity();
|
controller.checkTokenValidity();
|
||||||
|
|
||||||
expect(controller.vnToken.token).toBeNull();
|
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 = {
|
const response = {
|
||||||
token: 999,
|
token: 999,
|
||||||
created: new Date()
|
created: new Date()
|
||||||
};
|
};
|
||||||
localStorage.setItem('renewPeriod', 100);
|
localStorage.setItem('renewPeriod', 100);
|
||||||
const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000));
|
const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000));
|
||||||
controller.vnTokenCreated.created = oneHourBefore;
|
controller.vnToken.created = oneHourBefore;
|
||||||
|
|
||||||
$httpBackend.expect('POST', `VnUsers/renewToken`).respond(response);
|
$httpBackend.expect('POST', `VnUsers/renewToken`).respond(response);
|
||||||
controller.checkTokenValidity();
|
controller.checkTokenValidity();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
||||||
expect(controller.vnToken.token).toBe(999);
|
expect(controller.vnToken.token).toBe(999);
|
||||||
expect(controller.vnTokenCreated.created).toEqual(response.created);
|
expect(controller.vnToken.created).toEqual(response.created);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -84,7 +84,7 @@
|
||||||
"The current ticket can't be modified": "El ticket actual no puede ser modificado",
|
"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 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 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",
|
"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",
|
"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",
|
"NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada",
|
||||||
|
@ -293,5 +293,6 @@
|
||||||
"comercialName": "Comercial",
|
"comercialName": "Comercial",
|
||||||
"Invalid NIF for VIES": "Invalid NIF for VIES",
|
"Invalid NIF for VIES": "Invalid NIF for VIES",
|
||||||
"Ticket does not exist": "Este ticket no existe",
|
"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"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue