11import {
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" ;
3431import { GitGuardianQuotaWebviewProvider } from "./ggshield-webview/gitguardian-quota-webview" ;
3532import { GitGuardianRemediationMessageWebviewProvider } from "./ggshield-webview/gitguardian-remediation-message-view" ;
33+ import {
34+ AuthenticationStatus ,
35+ loginGGShield ,
36+ logoutGGShield ,
37+ updateAuthenticationStatus ,
38+ } from "./lib/authentication" ;
3639
3740function registerOpenViewsCommands (
3841 context : ExtensionContext ,
@@ -67,9 +70,6 @@ function registerOpenViewsCommands(
6770}
6871
6972export 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
0 commit comments