From 91c59b2707cbd96e7044690c55c98097949c3df4 Mon Sep 17 00:00:00 2001
From: TheRealJan <jan.m.tennert@gmail.com>
Date: Sat, 21 Oct 2023 12:56:05 +0200
Subject: [PATCH] Add SSO PKCE & LinkedInOIDC Provider

Signed-off-by: TheRealJan <jan.m.tennert@gmail.com>
---
 .../kotlin/io/github/jan/supabase/gotrue/GoTrueImpl.kt | 10 ++++++++++
 .../github/jan/supabase/gotrue/providers/Providers.kt  |  6 ++++++
 2 files changed, 16 insertions(+)

diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/GoTrueImpl.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/GoTrueImpl.kt
index 09471463..c2913431 100644
--- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/GoTrueImpl.kt
+++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/GoTrueImpl.kt
@@ -118,6 +118,12 @@ internal class GoTrueImpl(
         config: (Config.() -> Unit)?
     ): SSO.Result {
         val createdConfig = type.config.apply { config?.invoke(this) }
+        var codeChallenge: String? = null
+        if (this.config.flowType == FlowType.PKCE) {
+            val codeVerifier = generateCodeVerifier()
+            codeVerifierCache.saveCodeVerifier(codeVerifier)
+            codeChallenge = generateCodeChallenge(codeVerifier)
+        }
         return api.postJson("sso", buildJsonObject {
             redirectUrl?.let { put("redirect_to", it) }
             createdConfig.captchaToken?.let {
@@ -125,6 +131,10 @@ internal class GoTrueImpl(
                     put("captcha_token", it)
                 })
             }
+            codeChallenge?.let {
+                put("code_challenge", it)
+                put("code_challenge_method", "s256")
+            }
             when (createdConfig) {
                 is SSO.Config.Domain -> put("domain", createdConfig.domain)
                 is SSO.Config.Provider -> put("provider_id", createdConfig.providerId)
diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/providers/Providers.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/providers/Providers.kt
index 9e98cd7f..93df6386 100644
--- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/providers/Providers.kt
+++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/providers/Providers.kt
@@ -37,6 +37,12 @@ data object LinkedIn : OAuthProvider() {
 
 }
 
+data object LinkedInOIDC : OAuthProvider() {
+
+    override val name = "linkedin_oidc"
+
+}
+
 data object Notion : OAuthProvider() {
 
     override val name = "notion"