- Notifications
You must be signed in to change notification settings - Fork3
impl: support for OAuth2 [WIP]#209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
fioan89 wants to merge13 commits intomainChoose a base branch fromimpl-support-for-oauth
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
8bfee5e build: simplify install folder resolution
fioan891a3415b impl: setup auth manager with auth and token endpoints
fioan897685feb impl: retrieve supported response type and the dynamic client registr…
fioan8952648a0 impl: models for dynamic client registration
fioan8972a902f impl: pixy secure code generator
fioan890e03b03 impl: retrofit API for endpoint discovery and dynamic client registra…
fioan8979ba4cb impl: factory method for the auth manager
fioan8959d2abd impl: improve auth manager config
fioan89decb082 refactor: simplify OAuth manager architecture and improve dependency …
fioan89d432a76 fix: inject mocked PluginAuthManager into UTs
fioan892a28cee impl: handle the redirect URI
fioan896462f14 fix: wrong client app registration endpoint
fioan890e46da0 impl: simple way of triggering the OAuth flow.
fioan89File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 2 additions & 10 deletionsbuild.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/CoderToolboxContext.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/CoderToolboxExtension.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/AuthorizationServer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.coder.toolbox.oauth | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
| @JsonClass(generateAdapter = true) | ||
| data class AuthorizationServer( | ||
| @field:Json(name = "authorization_endpoint") val authorizationEndpoint: String, | ||
| @field:Json(name = "token_endpoint") val tokenEndpoint: String, | ||
| @field:Json(name = "registration_endpoint") val registrationEndpoint: String, | ||
| @property:Json(name = "response_types_supported") val supportedResponseTypes: List<String>, | ||
| @property:Json(name = "token_endpoint_auth_methods_supported") val authMethodForTokenEndpoint: List<TokenEndpointAuthMethod>, | ||
| ) | ||
| enum class TokenEndpointAuthMethod { | ||
| @Json(name = "none") | ||
| NONE, | ||
| @Json(name = "client_secret_post") | ||
| CLIENT_SECRET_POST, | ||
| @Json(name = "client_secret_basic") | ||
| CLIENT_SECRET_BASIC, | ||
| } |
14 changes: 14 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/ClientRegistrationRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.coder.toolbox.oauth | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
| @JsonClass(generateAdapter = true) | ||
| data class ClientRegistrationRequest( | ||
| @field:Json(name = "client_name") val clientName: String, | ||
| @field:Json(name = "redirect_uris") val redirectUris: List<String>, | ||
| @field:Json(name = "grant_types") val grantTypes: List<String>, | ||
| @field:Json(name = "response_types") val responseTypes: List<String>, | ||
| @field:Json(name = "scope") val scope: String, | ||
| @field:Json(name = "token_endpoint_auth_method") val tokenEndpointAuthMethod: String? = null | ||
| ) |
23 changes: 23 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/ClientRegistrationResponse.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.coder.toolbox.oauth | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
| /** | ||
| * DCR response | ||
| */ | ||
| @JsonClass(generateAdapter = true) | ||
| data class ClientRegistrationResponse( | ||
| @field:Json(name = "client_id") val clientId: String, | ||
| @field:Json(name = "client_secret") val clientSecret: String, | ||
| @field:Json(name = "client_name") val clientName: String, | ||
| @field:Json(name = "redirect_uris") val redirectUris: List<String>, | ||
| @field:Json(name = "grant_types") val grantTypes: List<String>, | ||
| @field:Json(name = "response_types") val responseTypes: List<String>, | ||
| @field:Json(name = "scope") val scope: String, | ||
| @field:Json(name = "token_endpoint_auth_method") val tokenEndpointAuthMethod: String, | ||
| @field:Json(name = "client_id_issued_at") val clientIdIssuedAt: Long?, | ||
| @field:Json(name = "client_secret_expires_at") val clientSecretExpiresAt: Long?, | ||
| @field:Json(name = "registration_client_uri") val registrationClientUri: String, | ||
| @field:Json(name = "registration_access_token") val registrationAccessToken: String | ||
| ) |
5 changes: 5 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/CoderAccount.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.coder.toolbox.oauth | ||
| import com.jetbrains.toolbox.api.core.auth.Account | ||
| data class CoderAccount(override val id: String, override val fullName: String) : Account |
16 changes: 16 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/CoderAuthorizationApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.coder.toolbox.oauth | ||
| import retrofit2.Response | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.GET | ||
| import retrofit2.http.POST | ||
| interface CoderAuthorizationApi { | ||
| @GET(".well-known/oauth-authorization-server") | ||
| suspend fun discoveryMetadata(): Response<AuthorizationServer> | ||
| @POST("oauth2/register") | ||
| suspend fun registerClient( | ||
| @Body request: ClientRegistrationRequest | ||
| ): Response<ClientRegistrationResponse> | ||
| } |
92 changes: 92 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/CoderOAuthManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.coder.toolbox.oauth | ||
| import com.jetbrains.toolbox.api.core.auth.AuthConfiguration | ||
| import com.jetbrains.toolbox.api.core.auth.ContentType | ||
| import com.jetbrains.toolbox.api.core.auth.ContentType.FORM_URL_ENCODED | ||
| import com.jetbrains.toolbox.api.core.auth.OAuthToken | ||
| import com.jetbrains.toolbox.api.core.auth.PluginAuthInterface | ||
| import com.jetbrains.toolbox.api.core.auth.RefreshConfiguration | ||
| class CoderOAuthManager : PluginAuthInterface<CoderAccount, CoderOAuthCfg> { | ||
| private lateinit var refreshConf: CoderRefreshConfig | ||
| override fun serialize(account: CoderAccount): String = "${account.id}|${account.fullName}" | ||
| override fun deserialize(string: String): CoderAccount = CoderAccount( | ||
| string.split('|')[0], | ||
| string.split('|')[1] | ||
| ) | ||
| override suspend fun createAccount( | ||
| token: OAuthToken, | ||
| config: AuthConfiguration | ||
| ): CoderAccount { | ||
| TODO("Not yet implemented") | ||
| } | ||
| override suspend fun updateAccount( | ||
| token: OAuthToken, | ||
| account: CoderAccount | ||
| ): CoderAccount { | ||
| TODO("Not yet implemented") | ||
| } | ||
| override fun createAuthConfig(loginConfiguration: CoderOAuthCfg): AuthConfiguration { | ||
| val codeVerifier = PKCEGenerator.generateCodeVerifier() | ||
| val codeChallenge = PKCEGenerator.generateCodeChallenge(codeVerifier) | ||
| refreshConf = loginConfiguration.toRefreshConf() | ||
| return AuthConfiguration( | ||
| authParams = mapOf( | ||
| "client_id" to loginConfiguration.clientId, | ||
| "response_type" to "code", | ||
| "code_challenge" to codeChallenge | ||
| ), | ||
| tokenParams = mapOf( | ||
| "grant_type" to "authorization_code", | ||
| "client_id" to loginConfiguration.clientId, | ||
| "code_verifier" to codeVerifier | ||
| ), | ||
| baseUrl = loginConfiguration.baseUrl, | ||
| authUrl = loginConfiguration.authUrl, | ||
| tokenUrl = loginConfiguration.tokenUrl, | ||
| codeChallengeParamName = "code_challenge", | ||
| codeChallengeMethod = "S256", | ||
| verifierParamName = "code_verifier", | ||
| authorization = null | ||
| ) | ||
| } | ||
| override fun createRefreshConfig(account: CoderAccount): RefreshConfiguration { | ||
| return object : RefreshConfiguration { | ||
| override val refreshUrl: String = refreshConf.refreshUrl | ||
| override val parameters: Map<String, String> = mapOf( | ||
| "grant_type" to "refresh_token", | ||
| "client_id" to refreshConf.clientId, | ||
| "client_secret" to refreshConf.clientSecret | ||
| ) | ||
| override val authorization: String? = null | ||
| override val contentType: ContentType = FORM_URL_ENCODED | ||
| } | ||
| } | ||
| } | ||
| data class CoderOAuthCfg( | ||
| val baseUrl: String, | ||
| val authUrl: String, | ||
| val tokenUrl: String, | ||
| val clientId: String, | ||
| val clientSecret: String, | ||
| ) | ||
| private data class CoderRefreshConfig( | ||
| val refreshUrl: String, | ||
| val clientId: String, | ||
| val clientSecret: String, | ||
| ) | ||
| private fun CoderOAuthCfg.toRefreshConf() = CoderRefreshConfig( | ||
| refreshUrl = this.tokenUrl, | ||
| clientId = this.clientId, | ||
| this.clientSecret | ||
| ) |
42 changes: 42 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/oauth/PKCEGenerator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.coder.toolbox.oauth | ||
| import java.security.MessageDigest | ||
| import java.security.SecureRandom | ||
| import java.util.Base64 | ||
| private const val CODE_VERIFIER_LENGTH = 128 | ||
| /** | ||
| * Generates OAuth2 PKCE code verifier and code challenge | ||
| */ | ||
| object PKCEGenerator { | ||
| /** | ||
| * Generates a cryptographically random code verifier 128 chars in size | ||
| * @return Base64 URL-encoded code verifier | ||
| */ | ||
| fun generateCodeVerifier(): String { | ||
| val secureRandom = SecureRandom() | ||
| val bytes = ByteArray(CODE_VERIFIER_LENGTH) | ||
| secureRandom.nextBytes(bytes) | ||
| return Base64.getUrlEncoder() | ||
| .withoutPadding() | ||
| .encodeToString(bytes) | ||
| .take(CODE_VERIFIER_LENGTH) | ||
| } | ||
| /** | ||
| * Generates code challenge from code verifier using S256 method | ||
| * @param codeVerifier The code verifier string | ||
| * @return Base64 URL-encoded SHA-256 hash of the code verifier | ||
| */ | ||
| fun generateCodeChallenge(codeVerifier: String): String { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| val hash = digest.digest(codeVerifier.toByteArray(Charsets.US_ASCII)) | ||
| return Base64.getUrlEncoder() | ||
| .withoutPadding() | ||
| .encodeToString(hash) | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletionssrc/main/kotlin/com/coder/toolbox/util/URLExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.