33
44package software.aws.toolkits.jetbrains.core.credentials
55
6+ import com.intellij.openapi.application.ApplicationManager
7+ import com.intellij.openapi.fileEditor.FileDocumentManager
68import software.amazon.awssdk.profiles.Profile
79import software.amazon.awssdk.profiles.ProfileFile
810import software.amazon.awssdk.profiles.ProfileFileLocation
@@ -15,6 +17,8 @@ import software.aws.toolkits.core.utils.touch
1517import software.aws.toolkits.core.utils.tryDirOp
1618import software.aws.toolkits.core.utils.tryFileOp
1719import software.aws.toolkits.core.utils.writeText
20+ import software.aws.toolkits.jetbrains.core.credentials.profiles.ProfileWatcher
21+ import software.aws.toolkits.jetbrains.core.credentials.profiles.SsoSessionConstants
1822import software.aws.toolkits.jetbrains.core.credentials.profiles.ssoSessions
1923import java.nio.file.Path
2024
@@ -35,6 +39,9 @@ interface ConfigFilesFacade {
3539 fun appendProfileToCredentials (profile : Profile )
3640 fun appendSectionToConfig (sectionName : String , profile : Profile )
3741 fun updateSectionInConfig (sectionName : String , profile : Profile )
42+
43+ fun deleteSsoConnectionFromConfig (sessionName : String )
44+ fun deleteSsoProfileScopesFromConfig (sessionName : String )
3845}
3946
4047class DefaultConfigFilesFacade (
@@ -173,6 +180,49 @@ class DefaultConfigFilesFacade(
173180 }
174181 }
175182
183+ override fun deleteSsoConnectionFromConfig (sessionName : String ) {
184+ val filePath = configPath
185+ val lines = filePath.inputStreamIfExists()?.reader()?.readLines().orEmpty()
186+ val ssoHeaderLine = lines.indexOfFirst { it.startsWith(" [${SsoSessionConstants .SSO_SESSION_SECTION_NAME } $sessionName ]" ) }
187+ if (ssoHeaderLine == - 1 ) return
188+ val nextHeaderLine = lines.subList(ssoHeaderLine + 1 , lines.size).indexOfFirst { it.startsWith(" [" ) }
189+ val endIndex = if (nextHeaderLine == - 1 ) lines.size else ssoHeaderLine + nextHeaderLine + 1
190+ val updatedArray = lines.subList(0 , ssoHeaderLine) + lines.subList(endIndex, lines.size)
191+ val profileHeaderLine = getCorrespondingSsoSessionProfilePosition(updatedArray, sessionName)
192+ filePath.writeText(profileHeaderLine.joinToString(" \n " ))
193+
194+ val applicationManager = ApplicationManager .getApplication()
195+ if (applicationManager != null && ! applicationManager.isUnitTestMode) {
196+ FileDocumentManager .getInstance().saveAllDocuments()
197+ ProfileWatcher .getInstance().forceRefresh()
198+ }
199+ }
200+
201+ override fun deleteSsoProfileScopesFromConfig (sessionName : String ) {
202+ val filePath = configPath
203+ val lines = filePath.inputStreamIfExists()?.reader()?.readLines().orEmpty().toMutableList()
204+ val ssoHeaderLine = lines.indexOfFirst { it.startsWith(" [${SsoSessionConstants .SSO_SESSION_SECTION_NAME } $sessionName ]" ) }
205+ if (ssoHeaderLine == - 1 ) return
206+ val nextHeaderLine = lines.subList(ssoHeaderLine + 1 , lines.size).indexOfFirst { it.startsWith(" [" ) }
207+ val endIndex = if (nextHeaderLine == - 1 ) lines.size else ssoHeaderLine + nextHeaderLine + 1
208+
209+ // Find and remove the sso_registration_scopes line
210+ for (i in ssoHeaderLine until endIndex) {
211+ if (lines[i].trim().startsWith(" sso_registration_scopes=" )) {
212+ lines.removeAt(i)
213+ break
214+ }
215+ }
216+
217+ filePath.writeText(lines.joinToString(" \n " ))
218+
219+ val applicationManager = ApplicationManager .getApplication()
220+ if (applicationManager != null && ! applicationManager.isUnitTestMode) {
221+ FileDocumentManager .getInstance().saveAllDocuments()
222+ ProfileWatcher .getInstance().forceRefresh()
223+ }
224+ }
225+
176226 private fun getCorrespondingSsoSessionProfilePosition (updatedArray : List <String >, sessionName : String ): List <String > {
177227 var content = updatedArray
178228 val finalContent = mutableListOf<String >()
0 commit comments