Removed circulare dep Logging -> TenantStorage
MODIFY
src/server/ocpi/OCPIServer.ts
src/server/ocpi/OCPIServer.ts
@@ -8,6 +8,8 @@ import Logging from '../../utils/Logging';
import OCPIServiceConfiguration from '../../types/configuration/OCPIServiceConfiguration';
import OCPIServices from './OCPIServices';
import { ServerUtils } from '../ServerUtils';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import Utils from '../../utils/Utils';
const MODULE_NAME = 'OCPIServer';
@@ -22,6 +24,8 @@ export default class OCPIServer {
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, ocpiRestConfig.debug);
// Log Express Request
+ this.expressApplication.use(this.resolveTenant.bind(this));
+ // Log Express Request
this.expressApplication.use(Logging.traceExpressRequest.bind(this));
// New OCPI Services Instances
const ocpiServices = new OCPIServices(this.ocpiRestConfig);
@@ -46,8 +50,29 @@ export default class OCPIServer {
}
// Start the server
- start(): void {
+ public start(): void {
ServerUtils.startHttpServer(this.ocpiRestConfig, ServerUtils.createHttpServer(this.ocpiRestConfig, this.expressApplication), MODULE_NAME, 'OCPI');
}
+
+ private async resolveTenant(req: Request, res: Response, next: NextFunction): Promise<void> {
+ if (req.headers?.authorization?.startsWith('Token')) {
+ try {
+ if (req.headers?.authorization.startsWith('Token')) {
+ const token = req.headers.authorization.slice(6);
+ const decodedToken = JSON.parse(Buffer.from(token, 'base64').toString());
+ if (Utils.objectHasProperty(decodedToken, 'tid')) {
+ const tenantSubdomain = decodedToken['tid'];
+ const tenant = await TenantStorage.getTenantBySubdomain(tenantSubdomain);
+ if (tenant) {
+ req['tenant'] = tenant;
+ }
+ }
+ }
+ } catch (error) {
+ // Ignore
+ }
+ }
+ next();
+ }
}

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -18,7 +18,6 @@ import { OCPPStatus } from '../types/ocpp/OCPPClient';
import { OICPResult } from '../types/oicp/OICPResult';
import PerformanceStorage from '../storage/mongodb/PerformanceStorage';
import Tenant from '../types/Tenant';
-import TenantStorage from '../storage/mongodb/TenantStorage';
import User from '../types/User';
import UserToken from '../types/UserToken';
import Utils from './Utils';
@@ -233,30 +232,6 @@ export default class Logging {
const tenant = req['tenant'] as Tenant;
tenantID = tenant.id;
tenantSubdomain = tenant.subdomain;
- // Check OCPI
- } else if (req.headers?.authorization?.startsWith('Token')) {
- let token: string;
- try {
- if (req.headers?.authorization.startsWith('Token')) {
- token = req.headers.authorization.slice(6);
- }
- if (req.headers?.authorization.startsWith('Bearer')) {
- token = req.headers.authorization.slice(7);
- }
- // Try Base 64 decoding (OCPI)
- if (token) {
- const decodedToken = JSON.parse(Buffer.from(token, 'base64').toString());
- if (Utils.objectHasProperty(decodedToken, 'tid')) {
- tenantSubdomain = decodedToken['tid'];
- const tenant = await TenantStorage.getTenantBySubdomain(tenantSubdomain);
- if (tenant) {
- tenantID = tenant.id;
- }
- }
- }
- } catch (error) {
- // Ignore
- }
}
// Check User
if (req['user']) {

Removed circulare dep Logging -> TenantStorage