Skip to content

Commit 1d0cf12

Browse files
feat: refacto authentication
1 parent b1c5290 commit 1d0cf12

15 files changed

+520
-313
lines changed

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"gitguardian.apiUrl": {
4040
"type": "string",
41-
"default": "https://dashboard.gitguardian.com",
41+
"default": "",
4242
"markdownDescription": "You can override the value here for On Premise installations"
4343
},
4444
"gitguardian.allowSelfSigned": {
@@ -65,6 +65,10 @@
6565
"command": "gitguardian.logout",
6666
"title": "gitguardian: logout"
6767
},
68+
{
69+
"command": "gitguardian.updateAuthenticationStatus",
70+
"title": "gitguardian: Update authentication status"
71+
},
6872
{
6973
"command": "gitguardian.showOutput",
7074
"title": "Show Output"
@@ -107,7 +111,7 @@
107111
"id": "gitguardianRemediationMessageView",
108112
"name": "remediation message",
109113
"collapsed": true,
110-
"when": "isAuthenticated == true"
114+
"when": "false"
111115
},
112116
{
113117
"type": "webview",
@@ -160,4 +164,4 @@
160164
"dependencies": {
161165
"axios": "^1.7.7"
162166
}
163-
}
167+
}

src/extension.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {
22
cleanUpFileDiagnostics,
33
createDiagnosticCollection,
4-
ggshieldAuthStatus,
54
ignoreLastFound,
65
ignoreSecret,
7-
loginGGShield,
8-
logoutGGShield,
96
scanFile,
107
showAPIQuota,
118
} from "./lib/ggshield-api";
@@ -33,6 +30,12 @@ import {
3330
} from "./gitguardian-interface/gitguardian-hover-provider";
3431
import { GitGuardianQuotaWebviewProvider } from "./ggshield-webview/gitguardian-quota-webview";
3532
import { GitGuardianRemediationMessageWebviewProvider } from "./ggshield-webview/gitguardian-remediation-message-view";
33+
import {
34+
AuthenticationStatus,
35+
loginGGShield,
36+
logoutGGShield,
37+
updateAuthenticationStatus,
38+
} from "./lib/authentication";
3639

3740
function registerOpenViewsCommands(
3841
context: ExtensionContext,
@@ -67,9 +70,6 @@ function registerOpenViewsCommands(
6770
}
6871

6972
export function activate(context: ExtensionContext) {
70-
// Check if ggshield if available
71-
commands.executeCommand("setContext", "isAuthenticated", false);
72-
7373
const outputChannel = window.createOutputChannel("GitGuardian");
7474
let configuration = getConfiguration(context);
7575

@@ -123,20 +123,18 @@ export function activate(context: ExtensionContext) {
123123
languages.registerHoverProvider("*", new GitGuardianSecretHoverProvider())
124124
);
125125

126-
checkGitInstalled();
126+
if (!checkGitInstalled()) {
127+
updateStatusBarItem(StatusBarStatus.error);
128+
return;
129+
}
127130

128131
ggshieldResolver.checkGGShieldConfiguration();
129132

130-
// Check if ggshield is authenticated
131-
ggshieldAuthStatus(configuration, context).then(() => {
132-
if (context.globalState.get("isAuthenticated", false)) {
133-
updateStatusBarItem(StatusBarStatus.ready);
134-
ggshieldViewProvider.refresh();
135-
ggshieldRemediationMessageViewProvider.refresh();
136-
ggshieldQuotaViewProvider.refresh();
137-
} else {
138-
updateStatusBarItem(StatusBarStatus.unauthenticated);
139-
}
133+
// update authentication status
134+
updateAuthenticationStatus(context, configuration).then(() => {
135+
ggshieldViewProvider.refresh();
136+
ggshieldRemediationMessageViewProvider.refresh();
137+
ggshieldQuotaViewProvider.refresh();
140138
});
141139

142140
// Start scanning documents on activation events
@@ -146,10 +144,9 @@ export function activate(context: ExtensionContext) {
146144
workspace.onDidSaveTextDocument((textDocument) => {
147145
// Check if the document is inside the workspace
148146
const workspaceFolder = workspace.getWorkspaceFolder(textDocument.uri);
149-
if (
150-
context.globalState.get("isAuthenticated", false) &&
151-
workspaceFolder
152-
) {
147+
const authStatus: AuthenticationStatus | undefined =
148+
context.workspaceState.get("authenticationStatus");
149+
if (authStatus?.success && workspaceFolder) {
153150
scanFile(
154151
textDocument.fileName,
155152
textDocument.uri,
@@ -193,12 +190,8 @@ export function activate(context: ExtensionContext) {
193190
ggshieldViewProvider.getView() as WebviewView,
194191
context
195192
)
196-
.then(() => {
197-
if (context.globalState.get("isAuthenticated", false)) {
198-
updateStatusBarItem(StatusBarStatus.ready);
199-
} else {
200-
updateStatusBarItem(StatusBarStatus.unauthenticated);
201-
}
193+
.then(async () => {
194+
await updateAuthenticationStatus(context, configuration);
202195
ggshieldViewProvider.refresh();
203196
ggshieldRemediationMessageViewProvider.refresh();
204197
ggshieldQuotaViewProvider.refresh();
@@ -208,12 +201,20 @@ export function activate(context: ExtensionContext) {
208201
});
209202
}),
210203
commands.registerCommand("gitguardian.logout", async () => {
211-
logoutGGShield(ggshieldResolver.configuration, context);
212-
updateStatusBarItem(StatusBarStatus.unauthenticated);
204+
await logoutGGShield(ggshieldResolver.configuration, context);
213205
ggshieldViewProvider.refresh();
214206
ggshieldRemediationMessageViewProvider.refresh();
215207
ggshieldQuotaViewProvider.refresh();
216-
})
208+
}),
209+
commands.registerCommand(
210+
"gitguardian.updateAuthenticationStatus",
211+
async () => {
212+
await updateAuthenticationStatus(context, configuration);
213+
ggshieldViewProvider.refresh();
214+
ggshieldRemediationMessageViewProvider.refresh();
215+
ggshieldQuotaViewProvider.refresh();
216+
}
217+
)
217218
);
218219
}
219220

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

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1+
import { AuthenticationStatus } from "../lib/authentication";
12
import { getAPIquota } from "../lib/ggshield-api";
2-
import { GGShieldConfiguration } from "../lib/ggshield-configuration";
33
import * as vscode from "vscode";
4+
import { GGShieldConfiguration } from "../lib/ggshield-configuration";
45

56
export class GitGuardianQuotaWebviewProvider
67
implements vscode.WebviewViewProvider
78
{
89
public static readonly viewType = "gitguardian.gitguardianQuotaView";
910
private _view?: vscode.WebviewView;
10-
private isAuthenticated: boolean = false;
1111
private quota: number = 0;
1212
private isLoading: boolean = false;
13+
private isAuthenticated: boolean = false;
1314

1415
constructor(
1516
private ggshieldConfiguration: GGShieldConfiguration,
1617
private readonly _extensionUri: vscode.Uri,
1718
private context: vscode.ExtensionContext
18-
) {
19-
this.checkAuthenticationStatus();
20-
this.updateQuota();
21-
}
19+
) {}
2220

2321
public resolveWebviewView(
2422
webviewView: vscode.WebviewView,
@@ -36,64 +34,58 @@ export class GitGuardianQuotaWebviewProvider
3634
});
3735
}
3836

39-
private checkAuthenticationStatus() {
40-
this.isAuthenticated = this.context.globalState.get(
41-
"isAuthenticated",
42-
false
43-
);
44-
}
45-
4637
private updateQuota() {
47-
if (this.isAuthenticated) {
38+
const authStatus: AuthenticationStatus | undefined =
39+
this.context.workspaceState.get("authenticationStatus");
40+
this.isAuthenticated = authStatus?.success ?? false;
41+
if (authStatus?.success) {
4842
this.quota = getAPIquota(this.ggshieldConfiguration);
4943
}
5044
}
5145

52-
private updateWebViewContent(webviewView?: vscode.WebviewView) {
53-
if (webviewView) {
54-
webviewView.webview.html = this.getHtmlForWebview();
46+
private updateWebViewContent() {
47+
if (this._view === undefined) {
48+
return;
5549
}
56-
}
5750

58-
private getHtmlForWebview(): string {
51+
let computedHtml: string;
52+
5953
if (this.isLoading) {
60-
return `
54+
computedHtml = `
6155
<!DOCTYPE html>
6256
<html lang="en">
6357
<body>
6458
<p>Loading...</p>
6559
</body>
6660
</html>`;
67-
}
68-
69-
if (this.isAuthenticated) {
70-
return `
61+
} else if (this.quota !== undefined) {
62+
computedHtml = `
7163
<!DOCTYPE html>
7264
<html lang="en">
7365
<body>
7466
<p>Your current quota: ${this.quota}</p>
7567
</body>
7668
</html>`;
7769
} else {
78-
return `
70+
computedHtml = `
7971
<!DOCTYPE html>
8072
<html lang="en">
8173
<body>
8274
<p>Please authenticate to see your quota.</p>
8375
</body>
8476
</html>`;
8577
}
78+
this._view.webview.html = computedHtml;
8679
}
8780

8881
public refresh() {
8982
this.isLoading = true;
90-
this.updateWebViewContent(this._view);
83+
this.updateWebViewContent();
9184

92-
this.checkAuthenticationStatus();
9385
this.updateQuota();
9486

9587
this.isLoading = false;
96-
this.updateWebViewContent(this._view);
88+
this.updateWebViewContent();
9789
}
9890

9991
dispose(): void {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class GitGuardianRemediationMessageWebviewProvider
1616
private readonly _extensionUri: vscode.Uri,
1717
private context: vscode.ExtensionContext
1818
) {
19-
this.checkAuthenticationStatus();
2019
this.updateRemediationMessage();
2120
}
2221

@@ -37,8 +36,8 @@ export class GitGuardianRemediationMessageWebviewProvider
3736
}
3837

3938
private checkAuthenticationStatus() {
40-
this.isAuthenticated = this.context.globalState.get(
41-
"isAuthenticated",
39+
this.isAuthenticated = this.context.workspaceState.get(
40+
"authenticationStatus",
4241
false
4342
);
4443
}

0 commit comments

Comments
 (0)