Skip to content

Commit

Permalink
fix(authentication-link): display link in webview instead of output (#23
Browse files Browse the repository at this point in the history
)
  • Loading branch information
salome-voltz authored Sep 26, 2024
1 parent af6d923 commit 9e0a74e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
workspace,
StatusBarItem,
StatusBarAlignment,
WebviewView,
} from "vscode";
import { GGShieldResolver } from "./lib/ggshield-resolver";
import { getCurrentFile, isGitInstalled } from "./utils";
Expand Down Expand Up @@ -240,10 +241,10 @@ export function activate(context: ExtensionContext) {
}
),
commands.registerCommand("gitguardian.authenticate", async () => {
outputChannel.show();
const isAuthenticated = await loginGGShield(
ggshieldResolver.configuration,
outputChannel
outputChannel,
ggshieldViewProvider.getView() as WebviewView
);
if (isAuthenticated) {
authStatus = true;
Expand All @@ -259,7 +260,6 @@ export function activate(context: ExtensionContext) {
}
}),
commands.registerCommand("gitguardian.logout", async () => {
outputChannel.show();
logoutGGShield(ggshieldResolver.configuration);
updateStatusBarItem(StatusBarStatus.unauthenticated, statusBar);
commands.executeCommand('setContext', 'isAuthenticated', false);
Expand Down
27 changes: 26 additions & 1 deletion src/ggshield-webview/gitguardian-webview-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
});
}

public getView(): vscode.WebviewView | undefined {
return this._view;
}


private async checkAuthenticationStatus() {
this.isAuthenticated = ggshieldAuthStatus(this.ggshieldConfiguration);
}
Expand Down Expand Up @@ -103,15 +108,35 @@ export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider {
<h1 style="margin-bottom:0px;">Welcome to GitGuardian</h1>
<p>Protect your code from secrets leakage</p>
<button class="button large" id="authenticate">Link your IDE to your account</button>

<p id="authMessage" style="display:none;">
If your browser doesn't open automatically, <a id="authLink" href="#" target="_blank">click here</a>.
</p>
</div>

<script>
const vscode = acquireVsCodeApi();

// Button click event to trigger authentication
document.getElementById('authenticate').addEventListener('click', () => {
vscode.postMessage({ type: 'authenticate' });
});

// Listener for authentication link
window.addEventListener('message', event => {
const message = event.data;

if (message.type === 'authLink') {
const authMessage = document.getElementById('authMessage');
const authLink = document.getElementById('authLink');
authLink.href = message.link; // Set the authentication link
authMessage.style.display = 'block'; // Show the message
}
});
</script>
</body>
</html>`;
</html>
`;
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/lib/ggshield-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
spawn,
spawnSync,
} from "child_process";
import { workspace, window } from "vscode";
import { workspace, window, WebviewView } from "vscode";
import axios from 'axios';
import { GGShieldConfiguration } from "./ggshield-configuration";
import { GGShieldScanResults } from "./api-types";
Expand All @@ -28,7 +28,7 @@ export function runGGShieldCommand(
GITGUARDIAN_API_URL: string;
GG_USER_AGENT: string;
GITGUARDIAN_DONT_LOAD_ENV: string;
GITGUARDIAN_API_KEY?: string; // Note the ? to indicate this property is optional
GITGUARDIAN_API_KEY?: string;
} = {
GITGUARDIAN_API_URL: apiUrl,
GG_USER_AGENT: "gitguardian-vscode",
Expand Down Expand Up @@ -208,7 +208,8 @@ export function ggshieldScanFile(

export async function loginGGShield(
configuration: GGShieldConfiguration,
outputChannel: any
outputChannel: any,
webviewView: WebviewView,
): Promise<boolean> {
const { ggshieldPath, apiUrl, apiKey } = configuration;

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

proc.stdout.on("data", (data) => {
outputChannel.appendLine(`ggshield stdout: ${data.toString()}`);
const output = data.toString();
outputChannel.appendLine(`ggshield stdout: ${output}`);

const urlLine = output.match(/https:\/\/[^\s]+/);
if (urlLine) {
const authUrl = urlLine[0];
webviewView.webview.postMessage({
type: 'authLink',
link: authUrl,
});
}
});
;

proc.stderr.on("data", (data) => {
outputChannel.appendLine(`ggshield stderr: ${data.toString()}`);
Expand All @@ -257,7 +269,7 @@ export async function loginGGShield(
export function logoutGGShield(
configuration: GGShieldConfiguration
): void {
const proc = runGGShieldCommand(configuration, ["auth", "logout"]);
runGGShieldCommand(configuration, ["auth", "logout"]);
}

export function ggshieldAuthStatus(
Expand Down

0 comments on commit 9e0a74e

Please sign in to comment.