From 1b251f1cea03450772f4dc4b17622651e03a6f2d Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Mon, 8 Feb 2021 19:07:37 +0000 Subject: [PATCH] fix(auth): auto submit auth form if needed --- src/auth/spec/AuthManager.spec.ts | 11 +++++++++-- src/request/RequestClient.ts | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/auth/spec/AuthManager.spec.ts b/src/auth/spec/AuthManager.spec.ts index 8af9cae..29b01ad 100644 --- a/src/auth/spec/AuthManager.spec.ts +++ b/src/auth/spec/AuthManager.spec.ts @@ -141,10 +141,17 @@ describe('AuthManager', () => { loginForm: { name: 'test' } }) ) - mockedAxios.post.mockImplementation(() => + mockedAxios.post.mockImplementationOnce(() => Promise.resolve({ data: mockLoginAuthoriseRequiredResponse, - config: { url: 'https://test.com/SASLogon/login' } + config: { url: 'https://test.com/SASLogon/login' }, + request: { responseURL: 'https://test.com/OAuth/authorize' } + }) + ) + + mockedAxios.get.mockImplementationOnce(() => + Promise.resolve({ + data: mockLoginAuthoriseRequiredResponse }) ) diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index f30ba0f..e0ff19b 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -332,9 +332,13 @@ export class RequestClient implements HttpClient { private handleError = async (e: any, callback: any) => { const response = e.response as AxiosResponse if (e instanceof AuthorizeError) { - const res = await this.get(e.confirmUrl, undefined, 'text/plain') - if (isAuthorizeFormRequired(res.result as string)) { - await this.authorize(res.result as string) + const res = await this.httpClient.get(e.confirmUrl, { + responseType: 'text', + headers: { 'Content-Type': 'text/plain', Accept: '*/*' } + }) + + if (isAuthorizeFormRequired(res?.data as string)) { + await this.authorize(res.data as string) } return await callback() } @@ -386,6 +390,16 @@ const throwIfError = (response: AxiosResponse) => { throw new LoginRequiredError() } + if ( + typeof response.data === 'string' && + isAuthorizeFormRequired(response.data) + ) { + throw new AuthorizeError( + 'Authorization required', + response.request.responseURL + ) + } + if ( typeof response.data === 'string' && isLogInRequired(response.data) &&