Skip to content

Commit

Permalink
auth todo
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Mar 27, 2024
1 parent 696a96a commit a5a77a7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.gitpod.toolbox.auth

import com.jetbrains.toolbox.gateway.ToolboxServiceLocator
import com.jetbrains.toolbox.gateway.auth.Account
import com.jetbrains.toolbox.gateway.auth.AuthConfiguration
import com.jetbrains.toolbox.gateway.auth.ContentType
import com.jetbrains.toolbox.gateway.auth.OAuthToken
import com.jetbrains.toolbox.gateway.auth.PluginAuthManager
import com.jetbrains.toolbox.gateway.auth.RefreshConfiguration
import okhttp3.internal.wait
import org.slf4j.LoggerFactory
import java.net.URI
import java.util.concurrent.Future
import java.util.concurrent.FutureTask

class GitpodAuthManager(serviceLocator: ToolboxServiceLocator) {
private val logger = LoggerFactory.getLogger(javaClass)
private val manager: PluginAuthManager<GitpodAccount, GitpodLoginConfiguration>

init {
manager = serviceLocator.getAuthManager(
"gitpod",
GitpodAccount::class.java,
{ it.toStoredData() },
{ GitpodAccount.fromStoredData(it) },
{ oauthToken, authCfg ->
getAuthenticatedUser(authCfg.baseUrl, oauthToken)
},
{ oauthToken, gpAccount ->
getAuthenticatedUser(gpAccount.getHost(), oauthToken)
},
{ gpLoginCfg ->
val authParams = mapOf(
"client_id" to "toolbox-gateway-gitpod-plugin",
"redirect_uri" to "jetbrains://gateway/io.gitpod.toolbox.gateway/complete-oauth",
"scope" to "function:*",
)
val tokenParams =
mapOf("grant_type" to "authorization_code", "client_id" to "toolbox-gateway-gitpod-plugin")
AuthConfiguration(
authParams,
tokenParams,
gpLoginCfg.host,
gpLoginCfg.host+"/api/oauth/authorize",
gpLoginCfg.host+"/api/oauth/token",
"code_challenge",
"S256",
"code_verifier",
"Bearer"
)
},
{ account ->
RefreshConfiguration("", mapOf(), "", ContentType.JSON)
},
)

manager.addEventListener {
logger.info("============hwen.login.managerEvent${it.accountId} ${it.type.name}")
}
}

fun getLoginUrl(gitpodHost: String): String {
logger.info("get oauth url of $gitpodHost")
return manager.initiateLogin(GitpodLoginConfiguration(gitpodHost))
}

fun getAuthenticatedUser(gitpodHost: String, oAuthToken: OAuthToken): Future<GitpodAccount> {
logger.info("=================hwen.login $gitpodHost : ${oAuthToken.authorizationHeader}")
return FutureTask {
GitpodAccount(oAuthToken.authorizationHeader, "", "hwen-test", gitpodHost)
}
}

fun tryHandle(uri: URI): Boolean {
if (!this.manager.canHandle(uri)) {
return false
}
val t = this.manager.handle(uri)
val t2 = t.wait()
logger.info("============hwen.login.tryHandle ${t2} ${uri.path}")
return true
}
}

class GitpodLoginConfiguration(public val host: String)

class GitpodAccount(private val credentials: String, private val id: String, private val name: String, private val host: String) : Account {
override fun getId(): String {
return id
}

override fun getFullName(): String {
return name
}

fun getHost(): String {
return host
}

fun toStoredData(): String {
return "${credentials}:${host}:${id}:${name}"

This comment has been minimized.

Copy link
@jeanp413

jeanp413 Mar 27, 2024

Member

Can we use encode using json?

This comment has been minimized.

Copy link
@mustard-mh

mustard-mh Apr 1, 2024

Author Contributor

Updated, will push later, this is a draft code to make it work

}

companion object {
fun fromStoredData(str: String): GitpodAccount {
val arr = str.split(":")
if (arr.size != 4) throw IllegalArgumentException("Invalid stored data")
return GitpodAccount(arr[0], arr[1], arr[2], arr[3])
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import okhttp3.OkHttpClient
class GitpodGatewayExtension : GatewayExtension {
override fun createRemoteProviderPluginInstance(serviceLocator: ToolboxServiceLocator): RemoteProvider {
return GitpodRemoteProvider(
serviceLocator.getService(OkHttpClient::class.java),
serviceLocator.getService(RemoteEnvironmentConsumer::class.java),
serviceLocator.getService(CoroutineScope::class.java),
serviceLocator,
serviceLocator.getService(OkHttpClient::class.java),
serviceLocator.getService(RemoteEnvironmentConsumer::class.java),
serviceLocator.getService(CoroutineScope::class.java),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package io.gitpod.toolbox.gateway
import com.jetbrains.toolbox.gateway.ProviderVisibilityState
import com.jetbrains.toolbox.gateway.RemoteEnvironmentConsumer
import com.jetbrains.toolbox.gateway.RemoteProvider
import com.jetbrains.toolbox.gateway.ToolboxServiceLocator
import com.jetbrains.toolbox.gateway.deploy.DiagnosticInfoCollector
import io.gitpod.toolbox.auth.GitpodAuthManager
import io.gitpod.toolbox.data.GitpodPublicApiManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
Expand All @@ -12,17 +14,23 @@ import java.net.URI
import okhttp3.OkHttpClient

class GitpodRemoteProvider(
private val serviceLocator: ToolboxServiceLocator,
private val httpClient: OkHttpClient,
private val consumer: RemoteEnvironmentConsumer,
coroutineScope: CoroutineScope,
) : RemoteProvider {
private val logger = LoggerFactory.getLogger(javaClass)
private val publicApi = GitpodPublicApiManager(logger)
private val authManger = GitpodAuthManager(serviceLocator)

init {
coroutineScope.launch {

logger.info("============hwen.2.${authManger.getLoginUrl("https://exp-migration.preview.gitpod-dev.com")}")

val resp = publicApi.listWorkspaces(publicApi.getCurrentOrganizationId())
consumer.consumeEnvironments(resp.workspacesList.map { GitpodRemoteProviderEnvironment(it, publicApi, httpClient, coroutineScope, logger) })

}
}

Expand All @@ -42,7 +50,14 @@ class GitpodRemoteProvider(
override fun removeEnvironmentsListener(listener: RemoteEnvironmentConsumer) {}

override fun handleUri(uri: URI) {
logger.debug("External request: {}", uri)
when (uri.path) {
"/complete-oauth" -> {
authManger.tryHandle(uri)
}
else -> {
logger.warn("Unknown request: {}", uri)
}
}
}

override fun getDiagnosticInfoCollector(): DiagnosticInfoCollector? {
Expand Down

0 comments on commit a5a77a7

Please sign in to comment.