-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #688 from supabase-community/third-party-auth
Add support for third party auth and add Slack OIDC provider
- Loading branch information
Showing
12 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AccessToken.kt
This file contains 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,36 @@ | ||
package io.github.jan.supabase.gotrue | ||
|
||
import io.github.jan.supabase.SupabaseClient | ||
import io.github.jan.supabase.annotations.SupabaseInternal | ||
import io.github.jan.supabase.plugins.MainConfig | ||
import io.github.jan.supabase.plugins.MainPlugin | ||
|
||
/** | ||
* Returns the access token used for requests. The token is resolved in the following order: | ||
* 1. [jwtToken] if not null | ||
* 2. [SupabaseClient.resolveAccessToken] if not null | ||
* 3. [Auth.currentAccessTokenOrNull] if the Auth plugin is installed | ||
* 4. [SupabaseClient.supabaseKey] if [keyAsFallback] is true | ||
*/ | ||
@SupabaseInternal | ||
suspend fun SupabaseClient.resolveAccessToken( | ||
jwtToken: String? = null, | ||
keyAsFallback: Boolean = true | ||
): String? { | ||
val key = if(keyAsFallback) supabaseKey else null | ||
return jwtToken ?: accessToken?.invoke() | ||
?: pluginManager.getPluginOrNull(Auth)?.currentAccessTokenOrNull() ?: key | ||
} | ||
|
||
/** | ||
* Returns the access token used for requests. The token is resolved in the following order: | ||
* 1. [MainConfig.jwtToken] if not null | ||
* 2. [SupabaseClient.resolveAccessToken] if not null | ||
* 3. [Auth.currentAccessTokenOrNull] if the Auth plugin is installed | ||
* 4. [SupabaseClient.supabaseKey] if [keyAsFallback] is true | ||
*/ | ||
@SupabaseInternal | ||
suspend fun <C : MainConfig> SupabaseClient.resolveAccessToken( | ||
plugin: MainPlugin<C>, | ||
keyAsFallback: Boolean = true | ||
) = resolveAccessToken(plugin.config.jwtToken, keyAsFallback) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,73 @@ | ||
import io.github.jan.supabase.gotrue.Auth | ||
import io.github.jan.supabase.gotrue.auth | ||
import io.github.jan.supabase.gotrue.minimalSettings | ||
import io.github.jan.supabase.gotrue.resolveAccessToken | ||
import io.github.jan.supabase.testing.createMockedSupabaseClient | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
|
||
class AccessTokenTest { | ||
|
||
@Test | ||
fun testAccessTokenWithJwtToken() { | ||
runTest { | ||
val client = createMockedSupabaseClient( | ||
configuration = { | ||
install(Auth) { | ||
minimalSettings() | ||
} | ||
} | ||
) | ||
client.auth.importAuthToken("myAuth") //this should be ignored as per plugin tokens override the used access token | ||
assertEquals("myJwtToken", client.resolveAccessToken("myJwtToken")) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAccessTokenWithKeyAsFallback() { | ||
runTest { | ||
val client = createMockedSupabaseClient(supabaseKey = "myKey") | ||
assertEquals("myKey", client.resolveAccessToken()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAccessTokenWithoutKey() { | ||
runTest { | ||
val client = createMockedSupabaseClient() | ||
assertNull(client.resolveAccessToken(keyAsFallback = false)) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAccessTokenWithCustomAccessToken() { | ||
runTest { | ||
val client = createMockedSupabaseClient( | ||
configuration = { | ||
accessToken = { | ||
"myCustomToken" | ||
} | ||
} | ||
) | ||
assertEquals("myCustomToken", client.resolveAccessToken()) | ||
} | ||
} | ||
|
||
@Test | ||
fun testAccessTokenWithAuth() { | ||
runTest { | ||
val client = createMockedSupabaseClient( | ||
configuration = { | ||
install(Auth) { | ||
minimalSettings() | ||
} | ||
} | ||
) | ||
client.auth.importAuthToken("myAuth") | ||
assertEquals("myAuth", client.resolveAccessToken()) | ||
} | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
10 changes: 10 additions & 0 deletions
10
Supabase/src/commonMain/kotlin/io/github/jan/supabase/AccessTokenProvider.kt
This file contains 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,10 @@ | ||
package io.github.jan.supabase | ||
|
||
/** | ||
* Optional function for using a third-party authentication system with | ||
* Supabase. The function should return an access token or ID token (JWT) by | ||
* obtaining it from the third-party auth client library. Note that this | ||
* function may be called concurrently and many times. Use memoization and | ||
* locking techniques if this is not supported by the client libraries. | ||
*/ | ||
typealias AccessTokenProvider = suspend () -> String |
This file contains 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
This file contains 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
This file contains 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