fun requestCredentialsForCodeCatalyst()

in plugins/toolkit/jetbrains-core/src/software/aws/toolkits/jetbrains/core/gettingstarted/ToolkitGettingStartedAuthUtils.kt [25:118]


fun requestCredentialsForCodeCatalyst(
    project: Project?,
    popupBuilderIdTab: Boolean = true,
    initialConnectionCount: Long = getConnectionCount(),
    initialAuthConnections: String = getEnabledConnections(
        project
    ),
    isFirstInstance: Boolean = false,
    connectionInitiatedFromExplorer: Boolean = false,
): Boolean? {
    if (isQWebviewsAvailable() && project != null) {
        ToolkitWebviewPanel.getInstance(project).browser?.prepareBrowser(BrowserState(FeatureId.Codecatalyst, true)) // TODO: consume data
        ShowToolkitListener.showWebview(project)

        return null
    }

    val authenticationDialog = when (ApplicationInfo.getInstance().build.productCode) {
        "GW" -> {
            GatewaySetupAuthenticationDialog(
                project,
                state = GatewaySetupAuthenticationDialogState().also {
                    it.selectedTab.set(GatewaySetupAuthenticationTabs.BUILDER_ID)
                },
                tabSettings = emptyMap(),
                scopes = CODECATALYST_SCOPES,
                promptForIdcPermissionSet = false
            )
        }

        else -> {
            requireNotNull(project) { "project must not be null when requesting credentials outside of gateway" }

            SetupAuthenticationDialog(
                project,
                state = SetupAuthenticationDialogState().also {
                    if (popupBuilderIdTab) {
                        it.selectedTab.set(SetupAuthenticationTabs.BUILDER_ID)
                    }
                },
                tabSettings = mapOf(
                    SetupAuthenticationTabs.IAM_LONG_LIVED to AuthenticationTabSettings(
                        disabled = true,
                        notice = SetupAuthenticationNotice(
                            SetupAuthenticationNotice.NoticeType.ERROR,
                            message("gettingstarted.setup.codecatalyst.no_iam"),
                            CAWS_DOCS
                        )
                    )
                ),
                scopes = CODECATALYST_SCOPES,
                promptForIdcPermissionSet = false,
                sourceOfEntry = SourceOfEntry.CODECATALYST,
                featureId = FeatureId.Codecatalyst,
                isFirstInstance = isFirstInstance,
                connectionInitiatedFromExplorer = connectionInitiatedFromExplorer
            )
        }
    }

    val isAuthenticationSuccessful = authenticationDialog.showAndGet()
    if (isAuthenticationSuccessful) {
        Telemetry.auth.addConnection.use {
            it.source(getSourceOfEntry(SourceOfEntry.CODECATALYST, isFirstInstance, connectionInitiatedFromExplorer))
                .featureId(FeatureId.Codecatalyst)
                .credentialSourceId(authenticationDialog.authType)
                .isAggregated(true)
                .attempts(authenticationDialog.attempts + 1)
                .result(MetricResult.Succeeded)
                .isReAuth(false)
        }
        AuthTelemetry.addedConnections(
            project,
            source = getSourceOfEntry(SourceOfEntry.CODECATALYST, isFirstInstance, connectionInitiatedFromExplorer),
            authConnectionsCount = initialConnectionCount,
            newAuthConnectionsCount = getConnectionCount() - initialConnectionCount,
            enabledAuthConnections = initialAuthConnections,
            newEnabledAuthConnections = getEnabledConnections(project),
            attempts = authenticationDialog.attempts + 1,
            result = Result.Succeeded
        )
    } else {
        Telemetry.auth.addConnection.use {
            it.source(getSourceOfEntry(SourceOfEntry.CODECATALYST, isFirstInstance, connectionInitiatedFromExplorer))
                .featureId(FeatureId.Codecatalyst)
                .credentialSourceId(authenticationDialog.authType)
                .isAggregated(false)
                .attempts(authenticationDialog.attempts + 1)
                .result(MetricResult.Cancelled)
                .isReAuth(false)
        }
    }
    return isAuthenticationSuccessful
}