urls dynamic integration

This commit is contained in:
2026-10-18 10:20:49 +03:30
parent 47447d1153
commit e7236884db
6 changed files with 103 additions and 44 deletions
+36 -15
View File
@@ -11,6 +11,7 @@ import { isAxiosError } from "axios";
import { Model } from "mongoose";
import { firstValueFrom } from "rxjs";
import {
getFanavaranApiRoot,
getFanavaranClientProfile,
type FanavaranAuthConfig,
type FanavaranClientKey,
@@ -44,10 +45,13 @@ interface TenantBackoffState {
export class FanavaranAuthService {
private readonly logger = new Logger(FanavaranAuthService.name);
private readonly getAppTokenUrl =
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/GetAppToken";
private readonly loginUrl =
"https://apimanager.iraneit.com/BimeApiManager/api/EITAuthentication/Login";
private getAppTokenUrl(clientKey: FanavaranClientKey): string {
return `${getFanavaranApiRoot(clientKey)}/EITAuthentication/GetAppToken`;
}
private loginUrl(clientKey: FanavaranClientKey): string {
return `${getFanavaranApiRoot(clientKey)}/EITAuthentication/Login`;
}
/** When Fanavaran says "try again later", pause all tenant calls. */
static readonly TRANSIENT_BACKOFF_MS = 5 * 60 * 1000;
@@ -181,8 +185,12 @@ export class FanavaranAuthService {
* Stable fingerprint of tenant auth. Any change (location, credentials, …)
* forces a fresh GetAppToken+Login even before Tehran midnight.
*/
static authFingerprint(auth: FanavaranAuthConfig): string {
static authFingerprint(
clientKey: FanavaranClientKey,
auth: FanavaranAuthConfig,
): string {
return [
getFanavaranApiRoot(clientKey),
auth.appName,
auth.secret,
auth.username,
@@ -211,6 +219,7 @@ export class FanavaranAuthService {
): Promise<string> {
this.assertNotInBackoff(clientKey);
const fingerprint = FanavaranAuthService.authFingerprint(
clientKey,
getFanavaranClientProfile(clientKey).auth,
);
@@ -277,9 +286,17 @@ export class FanavaranAuthService {
auditSession?: FanavaranAuditSession,
): Promise<string> {
const profile = getFanavaranClientProfile(clientKey);
const fingerprint = FanavaranAuthService.authFingerprint(profile.auth);
const appToken = await this.fetchAppToken(profile.auth, auditSession);
const fingerprint = FanavaranAuthService.authFingerprint(
clientKey,
profile.auth,
);
const appToken = await this.fetchAppToken(
clientKey,
profile.auth,
auditSession,
);
const authenticationToken = await this.fetchLoginToken(
clientKey,
appToken,
profile.auth,
auditSession,
@@ -413,9 +430,11 @@ export class FanavaranAuthService {
}
private async fetchAppToken(
clientKey: FanavaranClientKey,
config: Pick<FanavaranAuthConfig, "appName" | "secret">,
auditSession?: FanavaranAuditSession,
): Promise<string> {
const url = this.getAppTokenUrl(clientKey);
const startedAt = Date.now();
const requestHeaders = {
appname: config.appName,
@@ -427,7 +446,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.GET_APP_TOKEN,
status: FanavaranAuditStatus.STARTED,
requestUrl: this.getAppTokenUrl,
requestUrl: url,
requestMethod: "POST",
requestHeaders,
requestBody: "",
@@ -437,7 +456,7 @@ export class FanavaranAuthService {
try {
const response = await firstValueFrom(
this.httpService.post(this.getAppTokenUrl, "", {
this.httpService.post(url, "", {
headers: requestHeaders,
transformRequest: this.emptyBodyTransformRequest(),
}),
@@ -462,7 +481,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.GET_APP_TOKEN,
status: FanavaranAuditStatus.SUCCESS,
requestUrl: this.getAppTokenUrl,
requestUrl: url,
requestMethod: "POST",
httpStatus: response.status,
requestHeaders: exchange.requestHeaders,
@@ -485,7 +504,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.GET_APP_TOKEN,
status: FanavaranAuditStatus.FAILURE,
requestUrl: this.getAppTokenUrl,
requestUrl: url,
requestMethod: "POST",
httpStatus: exchange.httpStatus,
requestHeaders: exchange.requestHeaders,
@@ -506,10 +525,12 @@ export class FanavaranAuthService {
}
private async fetchLoginToken(
clientKey: FanavaranClientKey,
appToken: string,
config: Pick<FanavaranAuthConfig, "username" | "password">,
auditSession?: FanavaranAuditSession,
): Promise<string> {
const url = this.loginUrl(clientKey);
const startedAt = Date.now();
const requestHeaders = {
appToken,
@@ -522,7 +543,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.LOGIN,
status: FanavaranAuditStatus.STARTED,
requestUrl: this.loginUrl,
requestUrl: url,
requestMethod: "POST",
requestHeaders,
requestBody: "",
@@ -532,7 +553,7 @@ export class FanavaranAuthService {
try {
const response = await firstValueFrom(
this.httpService.post(this.loginUrl, "", {
this.httpService.post(url, "", {
headers: requestHeaders,
transformRequest: this.emptyBodyTransformRequest(),
}),
@@ -562,7 +583,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.LOGIN,
status: FanavaranAuditStatus.SUCCESS,
requestUrl: this.loginUrl,
requestUrl: url,
requestMethod: "POST",
httpStatus: response.status,
requestHeaders: exchange.requestHeaders,
@@ -588,7 +609,7 @@ export class FanavaranAuthService {
session: auditSession,
step: FanavaranAuditStep.LOGIN,
status: FanavaranAuditStatus.FAILURE,
requestUrl: this.loginUrl,
requestUrl: url,
requestMethod: "POST",
httpStatus: exchange.httpStatus,
requestHeaders: exchange.requestHeaders,