Skip to content
Snippets Groups Projects
Commit d94042b2 authored by Christian ZHENG's avatar Christian ZHENG
Browse files

fix(login): fix some side use cases & refactor logging

parent 32b114ec
No related branches found
No related tags found
2 merge requests!14feat: added mounted, .bashrc, .zshrc, added tests, added keycloak tests,!8feat(extension): login, open in devcontainer, automaticly save code, open briefing, publish extension
...@@ -21,7 +21,7 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -21,7 +21,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
public async getToken(args: { refreshToken?: string; openLink: (link: string) => void }) { public async getToken(args: { refreshToken?: string; openLink: (link: string) => void }) {
const { refreshToken, openLink } = args; const { refreshToken, openLink } = args;
if (refreshToken) { if (!!refreshToken) {
await this.createUserAuthentication({ await this.createUserAuthentication({
url: this._tokenUrl, url: this._tokenUrl,
body: (() => { body: (() => {
...@@ -36,7 +36,6 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -36,7 +36,6 @@ export default class KeycloakOAuth2DeviceFlowConnection {
return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken }); return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
} }
if (!this._deviceIsRegistered()) { if (!this._deviceIsRegistered()) {
console.log('--- device not registered');
await this._registerDevice(); await this._registerDevice();
} }
try { try {
...@@ -52,12 +51,10 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -52,12 +51,10 @@ export default class KeycloakOAuth2DeviceFlowConnection {
return params.toString(); return params.toString();
})(), })(),
}); });
console.log('access token: ', this._accessToken); return Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
console.log('refresh token: ', this._refreshToken);
Promise.resolve({ accessToken: this._accessToken, refreshToken: this._refreshToken });
} catch (error: any) { } catch (error: any) {
err(error); err(error);
Promise.reject(error); return Promise.reject(error);
} }
} }
...@@ -66,6 +63,7 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -66,6 +63,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
} }
private async _registerDevice() { private async _registerDevice() {
log('Device not registered. Registering ...');
const deviceAuthorizationRequestResponse: Response = await this.createDeviceAuthorization({ const deviceAuthorizationRequestResponse: Response = await this.createDeviceAuthorization({
url: this._deviceUrl, url: this._deviceUrl,
body: (() => { body: (() => {
...@@ -76,11 +74,11 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -76,11 +74,11 @@ export default class KeycloakOAuth2DeviceFlowConnection {
}); });
this._deviceAuthorizationRequestResponseData = this._deviceAuthorizationRequestResponseData =
(await deviceAuthorizationRequestResponse.json()) as DeviceAuthorizationRequestResponseData; (await deviceAuthorizationRequestResponse.json()) as DeviceAuthorizationRequestResponseData;
console.log('DeviceAuthorizationRequestResponseData', this._deviceAuthorizationRequestResponseData);
} }
private async createDeviceAuthorization(args: { url: string; body: string }): Promise<Response> { private async createDeviceAuthorization(args: { url: string; body: string }): Promise<Response> {
const { url, body } = args; const { url, body } = args;
log(`[POST] ${url} \n ${body}`);
return fetch(url, { return fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: {
...@@ -100,6 +98,7 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -100,6 +98,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
const { url, body } = args; const { url, body } = args;
let userAuthenticationRequestResponseCode = HttpStatusCode.I_AM_A_TEAPOT; let userAuthenticationRequestResponseCode = HttpStatusCode.I_AM_A_TEAPOT;
while (userAuthenticationRequestResponseCode !== HttpStatusCode.OK) { while (userAuthenticationRequestResponseCode !== HttpStatusCode.OK) {
log(`[POST] ${url} \n ${body}`);
const userAuthenticationRequestResponse: Response = await fetch(url, { const userAuthenticationRequestResponse: Response = await fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: {
...@@ -114,8 +113,7 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -114,8 +113,7 @@ export default class KeycloakOAuth2DeviceFlowConnection {
{ {
const badRequestResponse = const badRequestResponse =
(await userAuthenticationRequestResponse.json()) as FailedAuthenticationReponseData; (await userAuthenticationRequestResponse.json()) as FailedAuthenticationReponseData;
log(badRequestResponse.error_description); log(`${badRequestResponse.error!}: ${badRequestResponse.error_description}`);
log(badRequestResponse.error!);
const errorCode = TokenFetchErrorCode[badRequestResponse.error!]; const errorCode = TokenFetchErrorCode[badRequestResponse.error!];
switch (errorCode) { switch (errorCode) {
case TokenFetchErrorCode.invalid_client: case TokenFetchErrorCode.invalid_client:
...@@ -144,7 +142,6 @@ export default class KeycloakOAuth2DeviceFlowConnection { ...@@ -144,7 +142,6 @@ export default class KeycloakOAuth2DeviceFlowConnection {
(await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData; (await userAuthenticationRequestResponse.json()) as SuccessfulAuthenticationResponseData;
this._accessToken = successRequestResponse.access_token ?? ''; this._accessToken = successRequestResponse.access_token ?? '';
this._refreshToken = successRequestResponse.refresh_token ?? ''; this._refreshToken = successRequestResponse.refresh_token ?? '';
console.log(successRequestResponse);
break; break;
} }
default: { default: {
...@@ -210,7 +207,6 @@ class WaitDuration { ...@@ -210,7 +207,6 @@ class WaitDuration {
} }
public increase(): void { public increase(): void {
if (this._index < this._durations.length - 1) { if (this._index < this._durations.length - 1) {
console.log('index ', this._index);
this._index += 1; this._index += 1;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment