-
-
Notifications
You must be signed in to change notification settings - Fork 54
Add support for third party auth and add Slack OIDC provider #688
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
94359ce
Add support for third party auth and add Slack OIDC provider
jan-tennert dded345
Move error to Auth instance creation
jan-tennert 3cf95ce
Use init block
jan-tennert 9ae5323
Add a test and make AccessTokenProvider suspending
jan-tennert b285021
Remove unused code in test
jan-tennert be4ac3f
Add test for using Auth with an access token
jan-tennert e6bcb14
Clean up code and add realtime & storage support
jan-tennert 81faa9a
Improve docs
jan-tennert b217156
Merge branch 'master' into third-party-auth
jan-tennert 9a7916f
Rename methods
jan-tennert 0459c0d
improve docs
jan-tennert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,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 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
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
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
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,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 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
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
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
10 changes: 10 additions & 0 deletions
10
Supabase/src/commonMain/kotlin/io/github/jan/supabase/AccessTokenProvider.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,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 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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.