@@ -10,10 +10,8 @@ import com.jetbrains.toolbox.api.core.auth.*
10
10
import io.gitpod.publicapi.experimental.v1.UserServiceClient
11
11
import io.gitpod.toolbox.service.GitpodPublicApiManager
12
12
import io.gitpod.toolbox.service.Utils
13
- import io.gitpod.toolbox.utils.GitpodLogger
14
13
import kotlinx.coroutines.future.future
15
14
import kotlinx.serialization.Serializable
16
- import kotlinx.serialization.decodeFromString
17
15
import kotlinx.serialization.encodeToString
18
16
import kotlinx.serialization.json.Json
19
17
import kotlinx.serialization.json.jsonObject
@@ -48,22 +46,22 @@ class GitpodAuthManager {
48
46
GitpodAccount ::class .java,
49
47
{ it.encode() },
50
48
{ GitpodAccount .decode(it) },
51
- { oauthToken, authCfg -> getAuthenticatedUser(authCfg.baseUrl, oauthToken) },
49
+ { oauthToken, authCfg -> getAuthenticatedUser(URI .create( authCfg.baseUrl).host , oauthToken) },
52
50
{ oauthToken, gpAccount -> getAuthenticatedUser(gpAccount.getHost(), oauthToken) },
53
51
{ gpLoginCfg ->
54
52
val authParams = mapOf (
55
53
" response_type" to " code" ,
56
54
" client_id" to " toolbox-gateway-gitpod-plugin" ,
57
- " scope" to authScopesJetBrainsToolbox.joinToString(" " ),
55
+ " scope" to authScopesJetBrainsToolbox.joinToString(" %20 " ),
58
56
)
59
57
val tokenParams =
60
58
mapOf (" grant_type" to " authorization_code" , " client_id" to " toolbox-gateway-gitpod-plugin" )
61
59
AuthConfiguration (
62
60
authParams,
63
61
tokenParams,
64
- gpLoginCfg.host ,
65
- gpLoginCfg.host + " /api/oauth/authorize" ,
66
- gpLoginCfg.host + " /api/oauth/token" ,
62
+ gpLoginCfg.hostUrl ,
63
+ gpLoginCfg.hostUrl + " /api/oauth/authorize" ,
64
+ gpLoginCfg.hostUrl + " /api/oauth/token" ,
67
65
" code_challenge" ,
68
66
" S256" ,
69
67
" code_verifier" ,
@@ -76,13 +74,13 @@ class GitpodAuthManager {
76
74
manager.addEventListener {
77
75
when (it.type) {
78
76
AuthEvent .Type .LOGIN -> {
79
- GitpodLogger .info(" user logged in ${it.accountId} " )
77
+ Utils .logger .info(" user logged in ${it.accountId} " )
80
78
resetCurrentAccount(it.accountId)
81
79
loginListeners.forEach { it() }
82
80
}
83
81
84
82
AuthEvent .Type .LOGOUT -> {
85
- GitpodLogger .info(" user logged out ${it.accountId} " )
83
+ Utils .logger .info(" user logged out ${it.accountId} " )
86
84
resetCurrentAccount(it.accountId)
87
85
logoutListeners.forEach { it() }
88
86
}
@@ -92,7 +90,7 @@ class GitpodAuthManager {
92
90
93
91
private fun resetCurrentAccount (accountId : String ) {
94
92
val account = manager.accountsWithStatus.find { it.account.id == accountId }?.account ? : return
95
- GitpodLogger .debug(" reset settings for ${account.getHost()} " )
93
+ Utils .logger .debug(" reset settings for ${account.getHost()} " )
96
94
Utils .gitpodSettings.resetSettings(account.getHost())
97
95
}
98
96
@@ -132,8 +130,8 @@ class GitpodAuthManager {
132
130
}
133
131
134
132
fun getOAuthLoginUrl (gitpodHost : String ): String {
135
- GitpodLogger . info(" get oauth url of $gitpodHost " )
136
- return manager.initiateLogin(GitpodLoginConfiguration (gitpodHost))
133
+ Utils .logger. info(" get oauth url of https:// $gitpodHost " )
134
+ return manager.initiateLogin(GitpodLoginConfiguration (" https:// $ gitpodHost" ))
137
135
}
138
136
139
137
fun tryHandle (uri : URI ): Boolean {
@@ -156,7 +154,7 @@ class GitpodAuthManager {
156
154
private fun getAuthenticatedUser (gitpodHost : String , oAuthToken : OAuthToken ): Future <GitpodAccount > {
157
155
return Utils .coroutineScope.future {
158
156
val bearerToken = getBearerToken(oAuthToken)
159
- val client = GitpodPublicApiManager .createClient(URI ( gitpodHost).host , bearerToken)
157
+ val client = GitpodPublicApiManager .createClient(gitpodHost, bearerToken)
160
158
val user = GitpodPublicApiManager .tryGetAuthenticatedUser(UserServiceClient (client))
161
159
GitpodAccount (bearerToken, user.id, user.name, gitpodHost, authScopesJetBrainsToolbox)
162
160
}
@@ -178,7 +176,7 @@ class GitpodAuthManager {
178
176
179
177
}
180
178
181
- class GitpodLoginConfiguration (val host : String )
179
+ class GitpodLoginConfiguration (val hostUrl : String )
182
180
183
181
@Serializable
184
182
class GitpodAccount : Account {
@@ -192,7 +190,7 @@ class GitpodAccount : Account {
192
190
this .credentials = credentials
193
191
this .id = id
194
192
this .name = name
195
- this .host = URI (host). host
193
+ this .host = host
196
194
this .scopes = scopes
197
195
}
198
196
@@ -207,14 +205,9 @@ class GitpodAccount : Account {
207
205
}
208
206
209
207
suspend fun isValidate (): Boolean {
210
- // TODO(hw): Align host formatting everywhere
211
- val host = if (host.startsWith(" http" )) {
212
- host
213
- } else {
214
- " https://$host "
215
- }
216
- val client = GitpodPublicApiManager .createClient(URI (host).host, credentials)
217
- GitpodLogger .debug(" validating account $host " )
208
+ val hostUrl = " https://$host "
209
+ val client = GitpodPublicApiManager .createClient(URI (hostUrl).host, credentials)
210
+ Utils .logger.debug(" validating account $hostUrl " )
218
211
try {
219
212
GitpodPublicApiManager .tryGetAuthenticatedUser(UserServiceClient (client))
220
213
// TODO: Verify scopes
@@ -225,7 +218,7 @@ class GitpodAccount : Account {
225
218
" jsonrpc2: connection is closed"
226
219
))
227
220
) {
228
- GitpodLogger . error(" account $host is not valid" )
221
+ Utils .logger. error(" account $hostUrl is not valid" )
229
222
return false
230
223
}
231
224
return true
0 commit comments