From d94042b288a1caf3b140bb1427ebf9a14a83a0b7 Mon Sep 17 00:00:00 2001
From: Christian Zheng <czheng@takima.fr>
Date: Thu, 7 Apr 2022 13:39:14 +0200
Subject: [PATCH] fix(login): fix some side use cases & refactor logging

---
 .../core/keycloakOAuth2DeviceFlowConnection.ts | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts
index d985267c..27160216 100644
--- a/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts
+++ b/deadlock-plugins/deadlock-extension/src/core/keycloakOAuth2DeviceFlowConnection.ts
@@ -21,7 +21,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 
 	public async getToken(args: { refreshToken?: string; openLink: (link: string) => void }) {
 		const { refreshToken, openLink } = args;
-		if (refreshToken) {
+		if (!!refreshToken) {
 			await this.createUserAuthentication({
 				url: this._tokenUrl,
 				body: (() => {
@@ -36,7 +36,6 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 			return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
 		}
 		if (!this._deviceIsRegistered()) {
-			console.log('--- device not registered');
 			await this._registerDevice();
 		}
 		try {
@@ -52,12 +51,10 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 					return params.toString();
 				})(),
 			});
-			console.log('access token: ', this._accessToken);
-			console.log('refresh token: ', this._refreshToken);
-			Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
+			return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
 		} catch (error: any) {
 			err(error);
-			Promise.reject(error);
+			return Promise.reject(error);
 		}
 	}
 
@@ -66,6 +63,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 	}
 
 	private async _registerDevice() {
+		log('Device not registered. Registering ...');
 		const deviceAuthorizationRequestResponse: Response = await this.createDeviceAuthorization({
 			url: this._deviceUrl,
 			body: (() => {
@@ -76,11 +74,11 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 		});
 		this._deviceAuthorizationRequestResponseData =
 			(await deviceAuthorizationRequestResponse.json()) as DeviceAuthorizationRequestResponseData;
-		console.log('DeviceAuthorizationRequestResponseData', this._deviceAuthorizationRequestResponseData);
 	}
 
 	private async createDeviceAuthorization(args: { url: string; body: string }): Promise<Response> {
 		const { url, body } = args;
+		log(`[POST] ${url} \n ${body}`);
 		return fetch(url, {
 			method: 'POST',
 			headers: {
@@ -100,6 +98,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 		const { url, body } = args;
 		let userAuthenticationRequestResponseCode = HttpStatusCode.I_AM_A_TEAPOT;
 		while (userAuthenticationRequestResponseCode !== HttpStatusCode.OK) {
+			log(`[POST] ${url} \n ${body}`);
 			const userAuthenticationRequestResponse: Response = await fetch(url, {
 				method: 'POST',
 				headers: {
@@ -114,8 +113,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 					{
 						const badRequestResponse =
 							(await userAuthenticationRequestResponse.json()) as FailedAuthenticationReponseData;
-						log(badRequestResponse.error_description);
-						log(badRequestResponse.error!);
+						log(`${badRequestResponse.error!}: ${badRequestResponse.error_description}`);
 						const errorCode = TokenFetchErrorCode[badRequestResponse.error!];
 						switch (errorCode) {
 							case TokenFetchErrorCode.invalid_client:
@@ -144,7 +142,6 @@ export default class KeycloakOAuth2DeviceFlowConnection {
 						(await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData;
 					this._accessToken = successRequestResponse.access_token ?? '';
 					this._refreshToken = successRequestResponse.refresh_token ?? '';
-					console.log(successRequestResponse);
 					break;
 				}
 				default: {
@@ -210,7 +207,6 @@ class WaitDuration {
 	}
 	public increase(): void {
 		if (this._index < this._durations.length - 1) {
-			console.log('index ', this._index);
 			this._index += 1;
 		}
 	}
-- 
GitLab