Fixed cannot set headers after they are sent to the client when Token is invalid
MODIFY
src/server/rest/v1/service/SessionHashService.ts
src/server/rest/v1/service/SessionHashService.ts
@@ -3,8 +3,6 @@ import { NextFunction, Request, Response } from 'express';
import AppError from '../../../../exception/AppError';
import Constants from '../../../../utils/Constants';
import { HTTPError } from '../../../../types/HTTPError';
-import Logging from '../../../../utils/Logging';
-import { ServerAction } from '../../../../types/Server';
import { StatusCodes } from 'http-status-codes';
import Tenant from '../../../../types/Tenant';
import TenantStorage from '../../../../storage/mongodb/TenantStorage';
@@ -32,10 +30,13 @@ export default class SessionHashService {
if (!tenant) {
throw new AppError({
errorCode: StatusCodes.UNAUTHORIZED,
- message: 'Tenant does not exist',
- module: MODULE_NAME,
- method: 'checkUserAndTenantValidity',
- user: req.user
+ message: `Tenant ID '${tenant.id}' does not exist`,
+ module: MODULE_NAME, method: 'checkUserAndTenantValidity',
+ user: req.user,
+ detailedMessages: {
+ request: req.url,
+ headers: res.getHeaders(),
+ }
});
}
// Get User
@@ -43,10 +44,13 @@ export default class SessionHashService {
if (!user) {
throw new AppError({
errorCode: StatusCodes.UNAUTHORIZED,
- message: 'User does not exist',
- module: MODULE_NAME,
- method: 'checkUserAndTenantValidity',
- user: req.user
+ message: `User ID '${userID}' does not exist`,
+ module: MODULE_NAME, method: 'checkUserAndTenantValidity',
+ user: req.user,
+ detailedMessages: {
+ request: req.url,
+ headers: res.getHeaders(),
+ }
});
}
// Set in HTTP request
@@ -61,25 +65,31 @@ export default class SessionHashService {
if (userHashID !== SessionHashService.buildUserHashID(user)) {
throw new AppError({
errorCode: HTTPError.USER_ACCOUNT_CHANGED,
- message: 'User has been updated and will be logged off',
- module: MODULE_NAME,
- method: 'checkUserAndTenantValidity',
- user: req.user
+ message: 'Request rejected: User data in token is outdated',
+ module: MODULE_NAME, method: 'checkUserAndTenantValidity',
+ user: req.user,
+ detailedMessages: {
+ request: req.url,
+ headers: res.getHeaders(),
+ }
});
}
// Check Tenant's Hash
if (tenantHashID !== SessionHashService.buildTenantHashID(tenant)) {
throw new AppError({
errorCode: HTTPError.TENANT_COMPONENT_CHANGED,
- message: 'Tenant has been updated and all users will be logged off',
- module: MODULE_NAME,
- method: 'checkUserAndTenantValidity',
- user: req.user
+ message: 'Request rejected: Tenant data in token is outdated',
+ module: MODULE_NAME, method: 'checkUserAndTenantValidity',
+ user: req.user,
+ detailedMessages: {
+ request: req.url,
+ headers: res.getHeaders(),
+ }
});
}
next();
} catch (err) {
- await Logging.logActionExceptionMessageAndSendResponse(ServerAction.SESSION_HASH_SERVICE, err, req, res, next);
+ next(err);
}
}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
import { Action, DynamicAuthorizationDataSourceName, Entity } from '../../../../types/Authorization';
-import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
-import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
import AuthorizationService from './AuthorizationService';
import ChargingStationStorage from '../../../../storage/mongodb/ChargingStationStorage';
import Constants from '../../../../utils/Constants';
import DynamicAuthorizationFactory from '../../../../authorization/DynamicAuthorizationFactory';
+import { HTTPError } from '../../../../types/HTTPError';
import Logging from '../../../../utils/Logging';
import LoggingHelper from '../../../../utils/LoggingHelper';
import { ServerAction } from '../../../../types/Server';

Fixed cannot set headers after they are sent to the client when Token is invalid