11package com.coder.toolbox.oauth
22
3- import com.coder.toolbox.util.toBaseURL
43import com.jetbrains.toolbox.api.core.auth.AuthConfiguration
54import com.jetbrains.toolbox.api.core.auth.ContentType
65import com.jetbrains.toolbox.api.core.auth.ContentType.FORM_URL_ENCODED
76import com.jetbrains.toolbox.api.core.auth.OAuthToken
87import com.jetbrains.toolbox.api.core.auth.PluginAuthInterface
98import com.jetbrains.toolbox.api.core.auth.RefreshConfiguration
109
11- class CoderOAuthManager (
12- private val clientId : String ,
13- private val authServer : AuthorizationServer
14- ) : PluginAuthInterface<CoderAccount, CoderLoginCfg> {
10+ class CoderOAuthManager (private val cfg : CoderOAuthCfg ) : PluginAuthInterface<CoderAccount, CoderOAuthCfg> {
1511 override fun serialize (account : CoderAccount ): String = " ${account.id} |${account.fullName} "
1612
1713 override fun deserialize (string : String ): CoderAccount = CoderAccount (
@@ -33,28 +29,49 @@ class CoderOAuthManager(
3329 TODO (" Not yet implemented" )
3430 }
3531
36- override fun createAuthConfig (loginConfiguration : CoderLoginCfg ): AuthConfiguration = AuthConfiguration (
37- authParams = mapOf (" response_type" to " code" , " client_id" to clientId),
38- tokenParams = mapOf (" grant_type" to " authorization_code" , " client_id" to clientId),
39- baseUrl = authServer.authorizationEndpoint.toBaseURL().toString(),
40- authUrl = authServer.authorizationEndpoint,
41- tokenUrl = authServer.tokenEndpoint,
42- codeChallengeParamName = " code_challenge" ,
43- codeChallengeMethod = " S256" ,
44- verifierParamName = " code_verifier" ,
45- authorization = null
46- )
32+ override fun createAuthConfig (loginConfiguration : CoderOAuthCfg ): AuthConfiguration {
33+ val codeVerifier = PKCEGenerator .generateCodeVerifier()
34+ val codeChallenge = PKCEGenerator .generateCodeChallenge(codeVerifier)
4735
36+ return AuthConfiguration (
37+ authParams = mapOf (
38+ " client_id" to loginConfiguration.clientId,
39+ " response_type" to " code" ,
40+ " code_challenge" to codeChallenge
41+ ),
42+ tokenParams = mapOf (
43+ " grant_type" to " authorization_code" ,
44+ " client_id" to loginConfiguration.clientId,
45+ " code_verifier" to codeVerifier
46+ ),
47+ baseUrl = loginConfiguration.baseUrl,
48+ authUrl = loginConfiguration.authUrl,
49+ tokenUrl = loginConfiguration.tokenUrl,
50+ codeChallengeParamName = " code_challenge" ,
51+ codeChallengeMethod = " S256" ,
52+ verifierParamName = " code_verifier" ,
53+ authorization = null
54+ )
55+ }
4856
4957 override fun createRefreshConfig (account : CoderAccount ): RefreshConfiguration {
5058 return object : RefreshConfiguration {
51- override val refreshUrl: String = authServer.tokenEndpoint
52- override val parameters: Map <String , String > =
53- mapOf (" grant_type" to " refresh_token" , " client_id" to clientId)
59+ override val refreshUrl: String = cfg.tokenUrl
60+ override val parameters: Map <String , String > = mapOf (
61+ " grant_type" to " refresh_token" ,
62+ " client_id" to cfg.clientId,
63+ " client_secret" to cfg.clientSecret
64+ )
5465 override val authorization: String? = null
5566 override val contentType: ContentType = FORM_URL_ENCODED
5667 }
5768 }
5869}
5970
60- object CoderLoginCfg
71+ data class CoderOAuthCfg (
72+ val baseUrl : String ,
73+ val authUrl : String ,
74+ val tokenUrl : String ,
75+ val clientId : String ,
76+ val clientSecret : String ,
77+ )
0 commit comments