Skip to content

Commit 500bd9b

Browse files
committed
Improve Login to DevSandbox button workflow
Signed-off-by: Jessica He <jhe@redhat.com>
1 parent 9815ad2 commit 500bd9b

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/openshift/cluster.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,16 @@ export class Cluster extends OpenShiftItem {
500500
);
501501
}
502502

503+
static async validateLoginToken(userClusterUrl: string, userToken: string): Promise<boolean> {
504+
try {
505+
await Cluster.cli.executeTool(Command.odoLoginWithToken(userClusterUrl, userToken.trim()), undefined, true);
506+
await Cluster.cli.executeTool(Command.odoLogout());
507+
return true;
508+
} catch { //token is invalid
509+
return false;
510+
}
511+
}
512+
503513
@vsCommand('openshift.explorer.login.clipboard')
504514
static async loginUsingClipboardToken(apiEndpointUrl: string, oauthRequestTokenUrl: string): Promise<string | null> {
505515
const clipboard = await Cluster.readFromClipboard();

src/webview/cluster/app/sandboxView.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ export default function addSandboxView(props): JSX.Element {
382382
postMessage('sandboxLoginUsingDataInClipboard', {apiEndpointUrl: currentState.apiEndpoint, oauthRequestTokenUrl: `${currentState.oauthTokenEndpoint}/request`});
383383
};
384384

385+
const invalidToken = currentState.errorCode === 'invalidToken';
386+
const loginSandboxTitle = !invalidToken ? 'Login to DevSandbox OpenShift cluster with token from clipboard' : 'Token in clipboard is invalid. Select the Get Token option and copy to clipboard';
387+
385388
return (
386389
<>
387390
{( currentState.action === 'sandboxPageProvisioned' ) && (
@@ -413,8 +416,8 @@ export default function addSandboxView(props): JSX.Element {
413416
<Tooltip title='Copy token from DevSandbox console page in browser' placement='bottom'>
414417
<Button variant='contained' className='button' href={`${currentState.oauthTokenEndpoint}/request`}>Get token</Button>
415418
</Tooltip>
416-
<Tooltip title='Login to DevSandbox OpenShift cluster with token from clipboard' placement='bottom'>
417-
<Button variant='contained' className='buttonSecondary' onClick={handleLoginButton}>Login to DevSandbox</Button>
419+
<Tooltip title={loginSandboxTitle} placement='bottom'>
420+
<span><Button variant='contained' disabled={invalidToken} className='button' onClick={handleLoginButton}>Login to DevSandbox</Button></span>
418421
</Tooltip>
419422
</div>
420423
)}

src/webview/cluster/clusterViewLoader.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ async function clusterEditorMessageListener (event: any ): Promise<any> {
110110
} else {
111111
if (signupStatus.status.ready) {
112112
const oauthInfo = await sandboxAPI.getOauthServerInfo(signupStatus.apiEndpoint);
113-
panel.webview.postMessage({action: 'sandboxPageProvisioned', statusInfo: signupStatus.username, consoleDashboard: signupStatus.consoleURL, apiEndpoint: signupStatus.apiEndpoint, oauthTokenEndpoint: oauthInfo.token_endpoint });
113+
let errCode = '';
114+
if (!await Cluster.validateLoginToken(signupStatus.apiEndpoint, await vscode.env.clipboard.readText())) {
115+
errCode = 'invalidToken';
116+
}
117+
panel.webview.postMessage({ action: 'sandboxPageProvisioned', statusInfo: signupStatus.username, consoleDashboard: signupStatus.consoleURL, apiEndpoint: signupStatus.apiEndpoint, oauthTokenEndpoint: oauthInfo.token_endpoint, errorCode: errCode });
118+
pollClipboard(signupStatus);
114119
} else {
115120
// cluster is not ready and the reason is
116121
if (signupStatus.status.verificationRequired) {
@@ -185,6 +190,22 @@ async function clusterEditorMessageListener (event: any ): Promise<any> {
185190
}
186191
}
187192

193+
async function pollClipboard(signupStatus) {
194+
while (panel) {
195+
const previousContent = await vscode.env.clipboard.readText();
196+
await new Promise(r => setTimeout(r, 500));
197+
const currentContent = await vscode.env.clipboard.readText();
198+
if (previousContent && previousContent !== currentContent) {
199+
const oauthInfo = await sandboxAPI.getOauthServerInfo(signupStatus.apiEndpoint);
200+
let errCode = '';
201+
if (!await Cluster.validateLoginToken(signupStatus.apiEndpoint, currentContent)){
202+
errCode = 'invalidToken';
203+
}
204+
panel.webview.postMessage({action: 'sandboxPageProvisioned', statusInfo: signupStatus.username, consoleDashboard: signupStatus.consoleURL, apiEndpoint: signupStatus.apiEndpoint, oauthTokenEndpoint: oauthInfo.token_endpoint, errorCode: errCode});
205+
}
206+
}
207+
}
208+
188209
export default class ClusterViewLoader {
189210
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
190211
static get extensionPath() {

0 commit comments

Comments
 (0)