-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add Pubky Ring auth callbacks #917
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
5 commits
Select commit
Hold shift + click to select a range
c0de815
fix: add pubky ring callbacks
ben-kaufman b805610
fix: ignore stale pubky callbacks
ovitrif 46b372d
fix: guard pubky auth completion
ben-kaufman 20dcbc3
fix: reuse pubky nonce constant
ben-kaufman 2b8fcf8
Merge branch 'master' into codex-pubky-ring-callbacks-913
ovitrif 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
70 changes: 70 additions & 0 deletions
70
app/src/main/java/to/bitkit/models/PubkyRingAuthCallback.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,70 @@ | ||
| package to.bitkit.models | ||
|
|
||
| import android.net.Uri | ||
|
|
||
| private const val NONCE_PARAM = "nonce" | ||
|
|
||
| sealed interface PubkyRingAuthCallback { | ||
| companion object { | ||
| private const val BITKIT_SCHEME = "bitkit" | ||
| private const val PUBKY_AUTH_HOST = "pubky-auth" | ||
| private const val SUCCESS_PATH = "/success" | ||
| private const val CANCEL_PATH = "/cancel" | ||
| private const val ERROR_PATH = "/error" | ||
| private const val ERROR_MESSAGE_PARAM = "errorMessage" | ||
|
|
||
| fun parse(uri: Uri): PubkyRingAuthCallback? { | ||
| if (uri.scheme != BITKIT_SCHEME || uri.host != PUBKY_AUTH_HOST) return null | ||
|
|
||
| val nonce = uri.getQueryParameter(NONCE_PARAM)?.takeIf { it.isNotBlank() } | ||
| return when (uri.path) { | ||
| SUCCESS_PATH -> Success(nonce) | ||
| CANCEL_PATH -> Cancel(nonce) | ||
| ERROR_PATH -> Error(uri.getQueryParameter(ERROR_MESSAGE_PARAM), nonce) | ||
| else -> null | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val nonce: String? | ||
|
|
||
| data class Success(override val nonce: String?) : PubkyRingAuthCallback | ||
| data class Cancel(override val nonce: String?) : PubkyRingAuthCallback | ||
| data class Error(val message: String?, override val nonce: String?) : PubkyRingAuthCallback | ||
| } | ||
|
|
||
| sealed interface PubkyRingAuthCallbackHandlingResult { | ||
| data object Ignored : PubkyRingAuthCallbackHandlingResult | ||
| data object Handled : PubkyRingAuthCallbackHandlingResult | ||
| data class TrustedError(val message: String?) : PubkyRingAuthCallbackHandlingResult | ||
| } | ||
|
ben-kaufman marked this conversation as resolved.
|
||
|
|
||
| object PubkyRingAuthUrlBuilder { | ||
| const val SUCCESS_CALLBACK = "bitkit://pubky-auth/success" | ||
| const val CANCEL_CALLBACK = "bitkit://pubky-auth/cancel" | ||
| const val ERROR_CALLBACK = "bitkit://pubky-auth/error" | ||
| const val SOURCE = "Bitkit" | ||
|
|
||
| fun addCallbacks(authUrl: String, nonce: String? = null): String? { | ||
| val uri = Uri.parse(authUrl) | ||
|
ovitrif marked this conversation as resolved.
|
||
| if (uri.scheme.isNullOrBlank()) return null | ||
|
|
||
| return uri.buildUpon() | ||
| .appendQueryParameter("x-success", callbackUrl(SUCCESS_CALLBACK, nonce)) | ||
| .appendQueryParameter("x-cancel", callbackUrl(CANCEL_CALLBACK, nonce)) | ||
| .appendQueryParameter("x-error", callbackUrl(ERROR_CALLBACK, nonce)) | ||
| .appendQueryParameter("x-source", SOURCE) | ||
| .build() | ||
| .toString() | ||
| } | ||
|
|
||
| private fun callbackUrl(baseUrl: String, nonce: String?): String { | ||
| if (nonce.isNullOrBlank()) return baseUrl | ||
|
|
||
| return Uri.parse(baseUrl) | ||
| .buildUpon() | ||
| .appendQueryParameter(NONCE_PARAM, nonce) | ||
| .build() | ||
|
ben-kaufman marked this conversation as resolved.
|
||
| .toString() | ||
| } | ||
| } | ||
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.
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.