Fix login auto-retry deadlock
This commit is contained in:
@@ -27,6 +27,7 @@ import type {
|
||||
|
||||
type RetryableAxiosRequestConfig = AxiosRequestConfig & {
|
||||
_agrarmonitorRetry?: boolean;
|
||||
_agrarmonitorLoginRequest?: boolean;
|
||||
};
|
||||
|
||||
type AgrarmonitorCustomerPage = {
|
||||
@@ -557,17 +558,20 @@ export class AgrarmonitorConnector implements AgrarmonitorConnectorResult {
|
||||
async response => {
|
||||
await this.options.cookieStore.save(this.cookieJar);
|
||||
|
||||
if (this.autoRetry && this.isLoginRequiredResponse(response)) {
|
||||
return this.retryAfterLogin(response.config);
|
||||
const config = response.config as RetryableAxiosRequestConfig;
|
||||
|
||||
if (this.autoRetry && !config._agrarmonitorLoginRequest && this.isLoginRequiredResponse(response)) {
|
||||
return this.retryAfterLogin(config);
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
async error => {
|
||||
const response = error.response as AxiosResponse | undefined;
|
||||
const config = error.config as RetryableAxiosRequestConfig | undefined;
|
||||
|
||||
if (this.autoRetry && response && this.isLoginRequiredResponse(response)) {
|
||||
return this.retryAfterLogin(error.config);
|
||||
if (this.autoRetry && response && config && !config._agrarmonitorLoginRequest && this.isLoginRequiredResponse(response)) {
|
||||
return this.retryAfterLogin(config);
|
||||
}
|
||||
|
||||
throw error;
|
||||
@@ -663,12 +667,16 @@ export class AgrarmonitorConnector implements AgrarmonitorConnectorResult {
|
||||
|
||||
await this.performRedirectLogin();
|
||||
|
||||
if (!(await this.isSessionValid({ skipAutoRetry: true }))) {
|
||||
throw new Error('Agrarmonitor-Login durchgefuehrt, Session weiterhin ungueltig - Geraet freigeschaltet? Credentials korrekt?');
|
||||
}
|
||||
|
||||
await this.options.cookieStore.save(this.cookieJar);
|
||||
this.logger?.info?.('Agrarmonitor-Login erfolgreich');
|
||||
}
|
||||
|
||||
private async performRedirectLogin(): Promise<void> {
|
||||
const loginPageResponse = await this.http.get('/');
|
||||
const loginPageResponse = await this.http.get('/', this.loginRequestConfig());
|
||||
const loginPageText = typeof loginPageResponse.data === 'string' ? loginPageResponse.data : '';
|
||||
|
||||
if (!this.isLoginPageText(loginPageText)) {
|
||||
@@ -684,11 +692,14 @@ export class AgrarmonitorConnector implements AgrarmonitorConnectorResult {
|
||||
ssoReturn: "",
|
||||
};
|
||||
|
||||
const response = await this.http.post('/login/api/login.php', loginData, {
|
||||
const loginPostConfig: RetryableAxiosRequestConfig = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
_agrarmonitorLoginRequest: true,
|
||||
};
|
||||
|
||||
const response = await this.http.post('/login/api/login.php', loginData, loginPostConfig);
|
||||
|
||||
const responseText = typeof response.data === 'string' ? response.data : '';
|
||||
|
||||
@@ -697,15 +708,21 @@ export class AgrarmonitorConnector implements AgrarmonitorConnectorResult {
|
||||
}
|
||||
}
|
||||
|
||||
private async isSessionValid(): Promise<boolean> {
|
||||
private async isSessionValid(options: { skipAutoRetry?: boolean } = {}): Promise<boolean> {
|
||||
try {
|
||||
const response = await this.http.get('/');
|
||||
const response = await this.http.get('/', options.skipAutoRetry ? this.loginRequestConfig() : undefined);
|
||||
return !this.isLoginRequiredResponse(response);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private loginRequestConfig(): RetryableAxiosRequestConfig {
|
||||
return {
|
||||
_agrarmonitorLoginRequest: true,
|
||||
};
|
||||
}
|
||||
|
||||
private isLoginRequiredResponse(response: AxiosResponse): boolean {
|
||||
const responseUrl = this.getResponseUrl(response);
|
||||
const responseText = typeof response.data === 'string' ? response.data : '';
|
||||
|
||||
Reference in New Issue
Block a user