Skip to content

Commit 9e0a74e

Browse files
authored
fix(authentication-link): display link in webview instead of output (#23)
1 parent af6d923 commit 9e0a74e

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
workspace,
2626
StatusBarItem,
2727
StatusBarAlignment,
28+
WebviewView,
2829
} from "vscode";
2930
import { GGShieldResolver } from "./lib/ggshield-resolver";
3031
import { getCurrentFile, isGitInstalled } from "./utils";
@@ -240,10 +241,10 @@ export function activate(context: ExtensionContext) {
240241
}
241242
),
242243
commands.registerCommand("gitguardian.authenticate", async () => {
243-
outputChannel.show();
244244
const isAuthenticated = await loginGGShield(
245245
ggshieldResolver.configuration,
246-
outputChannel
246+
outputChannel,
247+
ggshieldViewProvider.getView() as WebviewView
247248
);
248249
if (isAuthenticated) {
249250
authStatus = true;
@@ -259,7 +260,6 @@ export function activate(context: ExtensionContext) {
259260
}
260261
}),
261262
commands.registerCommand("gitguardian.logout", async () => {
262-
outputChannel.show();
263263
logoutGGShield(ggshieldResolver.configuration);
264264
updateStatusBarItem(StatusBarStatus.unauthenticated, statusBar);
265265
commands.executeCommand('setContext', 'isAuthenticated', false);

src/ggshield-webview/gitguardian-webview-view.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
4545
});
4646
}
4747

48+
public getView(): vscode.WebviewView | undefined {
49+
return this._view;
50+
}
51+
52+
4853
private async checkAuthenticationStatus() {
4954
this.isAuthenticated = ggshieldAuthStatus(this.ggshieldConfiguration);
5055
}
@@ -103,15 +108,35 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
103108
<h1 style="margin-bottom:0px;">Welcome to GitGuardian</h1>
104109
<p>Protect your code from secrets leakage</p>
105110
<button class="button large" id="authenticate">Link your IDE to your account</button>
111+
112+
<p id="authMessage" style="display:none;">
113+
If your browser doesn't open automatically, <a id="authLink" href="#" target="_blank">click here</a>.
114+
</p>
106115
</div>
116+
107117
<script>
108118
const vscode = acquireVsCodeApi();
119+
120+
// Button click event to trigger authentication
109121
document.getElementById('authenticate').addEventListener('click', () => {
110122
vscode.postMessage({ type: 'authenticate' });
111123
});
124+
125+
// Listener for authentication link
126+
window.addEventListener('message', event => {
127+
const message = event.data;
128+
129+
if (message.type === 'authLink') {
130+
const authMessage = document.getElementById('authMessage');
131+
const authLink = document.getElementById('authLink');
132+
authLink.href = message.link; // Set the authentication link
133+
authMessage.style.display = 'block'; // Show the message
134+
}
135+
});
112136
</script>
113137
</body>
114-
</html>`;
138+
</html>
139+
`;
115140
}
116141
}
117142

src/lib/ggshield-api.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
spawn,
77
spawnSync,
88
} from "child_process";
9-
import { workspace, window } from "vscode";
9+
import { workspace, window, WebviewView } from "vscode";
1010
import axios from 'axios';
1111
import { GGShieldConfiguration } from "./ggshield-configuration";
1212
import { GGShieldScanResults } from "./api-types";
@@ -28,7 +28,7 @@ export function runGGShieldCommand(
2828
GITGUARDIAN_API_URL: string;
2929
GG_USER_AGENT: string;
3030
GITGUARDIAN_DONT_LOAD_ENV: string;
31-
GITGUARDIAN_API_KEY?: string; // Note the ? to indicate this property is optional
31+
GITGUARDIAN_API_KEY?: string;
3232
} = {
3333
GITGUARDIAN_API_URL: apiUrl,
3434
GG_USER_AGENT: "gitguardian-vscode",
@@ -208,7 +208,8 @@ export function ggshieldScanFile(
208208

209209
export async function loginGGShield(
210210
configuration: GGShieldConfiguration,
211-
outputChannel: any
211+
outputChannel: any,
212+
webviewView: WebviewView,
212213
): Promise<boolean> {
213214
const { ggshieldPath, apiUrl, apiKey } = configuration;
214215

@@ -230,8 +231,19 @@ export async function loginGGShield(
230231
const proc = spawn(ggshieldPath, args, options);
231232

232233
proc.stdout.on("data", (data) => {
233-
outputChannel.appendLine(`ggshield stdout: ${data.toString()}`);
234+
const output = data.toString();
235+
outputChannel.appendLine(`ggshield stdout: ${output}`);
236+
237+
const urlLine = output.match(/https:\/\/[^\s]+/);
238+
if (urlLine) {
239+
const authUrl = urlLine[0];
240+
webviewView.webview.postMessage({
241+
type: 'authLink',
242+
link: authUrl,
243+
});
244+
}
234245
});
246+
;
235247

236248
proc.stderr.on("data", (data) => {
237249
outputChannel.appendLine(`ggshield stderr: ${data.toString()}`);
@@ -257,7 +269,7 @@ export async function loginGGShield(
257269
export function logoutGGShield(
258270
configuration: GGShieldConfiguration
259271
): void {
260-
const proc = runGGShieldCommand(configuration, ["auth", "logout"]);
272+
runGGShieldCommand(configuration, ["auth", "logout"]);
261273
}
262274

263275
export function ggshieldAuthStatus(

0 commit comments

Comments
 (0)