forked from Shared/esg
requests fix
This commit is contained in:
@@ -61,7 +61,9 @@ export class AmitisProvider {
|
||||
|
||||
/**
|
||||
* Get access token for a specific provider and inquiry type
|
||||
* Handles token caching, refresh, and daily expiration
|
||||
* Handles token caching, refresh, and 24-hour expiration
|
||||
* - Access token expires in 20 minutes
|
||||
* - Refresh token expires in 24 hours from creation
|
||||
*/
|
||||
async getAccessToken(
|
||||
providerName: ProviderName,
|
||||
@@ -72,13 +74,13 @@ export class AmitisProvider {
|
||||
const tokenKey = this.getTokenKey(providerName, inquiryType);
|
||||
const latestToken = await this.generalTokenService.getLatestToken(tokenKey);
|
||||
|
||||
// Use cached token if valid and from same day
|
||||
if (latestToken && !this.isExpired(latestToken) && this.isSameTokenDay(latestToken)) {
|
||||
// Use cached token if valid and within 24-hour window
|
||||
if (latestToken && !this.isExpired(latestToken) && this.isWithin24Hours(latestToken)) {
|
||||
return latestToken.accessToken;
|
||||
}
|
||||
|
||||
// Try to refresh if we have a refresh token and it's from the same day
|
||||
if (latestToken?.refreshToken && this.isSameTokenDay(latestToken)) {
|
||||
// Try to refresh if we have a refresh token and it's within 24 hours
|
||||
if (latestToken?.refreshToken && this.isWithin24Hours(latestToken)) {
|
||||
try {
|
||||
return await this.refreshAccessToken(latestToken, providerName, inquiryType);
|
||||
} catch (error) {
|
||||
@@ -100,18 +102,19 @@ export class AmitisProvider {
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<string> {
|
||||
// Use URLSearchParams for application/x-www-form-urlencoded format
|
||||
const formData = new URLSearchParams();
|
||||
// Use FormData for multipart/form-data format
|
||||
const FormData = require('form-data');
|
||||
const formData = new FormData();
|
||||
formData.append('username', username);
|
||||
formData.append('password', password);
|
||||
|
||||
try {
|
||||
const response = await this.httpClient.post<CentInsurTokenResponse>(
|
||||
this.config.loginPath,
|
||||
formData.toString(),
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
...formData.getHeaders(),
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -198,25 +201,25 @@ export class AmitisProvider {
|
||||
}
|
||||
|
||||
private isExpired(token: GeneralTokenDocument): boolean {
|
||||
// Access token expires in 20 minutes, add 60 second buffer
|
||||
const refreshBufferMs = 60 * 1000;
|
||||
return !token.expiresAt || Date.now() + refreshBufferMs >= token.expiresAt.getTime();
|
||||
}
|
||||
|
||||
private isSameTokenDay(token: GeneralTokenDocument): boolean {
|
||||
/**
|
||||
* Check if token is within 24-hour window from creation
|
||||
* Refresh token is valid for 24 hours after initial login
|
||||
*/
|
||||
private isWithin24Hours(token: GeneralTokenDocument): boolean {
|
||||
if (!token.createdAt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.getDateKey(token.createdAt) === this.getDateKey(new Date());
|
||||
}
|
||||
|
||||
private getDateKey(date: Date): string {
|
||||
return new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone: this.config.tokenTimeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
}).format(date);
|
||||
const tokenCreationTime = token.createdAt.getTime();
|
||||
const currentTime = Date.now();
|
||||
const twentyFourHoursMs = 24 * 60 * 60 * 1000;
|
||||
|
||||
return currentTime - tokenCreationTime < twentyFourHoursMs;
|
||||
}
|
||||
|
||||
private getTokenKey(providerName: ProviderName, inquiryType: InquiryType): string {
|
||||
|
||||
Reference in New Issue
Block a user