-
Notifications
You must be signed in to change notification settings - Fork 169
feat :Add partial support for PAR auth flow #967
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
6 commits
Select commit
Hold shift + click to select a range
0cfcec4
feat :Add partial support for PAR auth flow
pmathew92 d538379
Updated examples file
pmathew92 b13e4f7
Minor doc change
pmathew92 b2b13fa
fixed the lint error
pmathew92 c2e5aa6
Addressed review feedbacks
pmathew92 7ce7580
Added doc for state validation
pmathew92 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
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
52 changes: 52 additions & 0 deletions
52
auth0/src/main/java/com/auth0/android/provider/AuthorizeResultParser.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,52 @@ | ||
| package com.auth0.android.provider | ||
|
|
||
| import com.auth0.android.authentication.AuthenticationException | ||
|
|
||
| /** | ||
| * Parses the result from an authorization redirect callback. | ||
| */ | ||
| internal object AuthorizeResultParser { | ||
|
|
||
| sealed class CodeResult { | ||
| data class Success(val code: String, val state: String?) : CodeResult() | ||
| data class Error(val exception: AuthenticationException) : CodeResult() | ||
| object Canceled : CodeResult() | ||
| object Invalid : CodeResult() | ||
| } | ||
|
|
||
| private const val KEY_CODE = "code" | ||
| private const val KEY_STATE = "state" | ||
| private const val KEY_ERROR = "error" | ||
| private const val KEY_ERROR_DESCRIPTION = "error_description" | ||
|
|
||
| fun parse(result: AuthorizeResult, requestCode: Int): CodeResult { | ||
| if (!result.isValid(requestCode)) { | ||
| return CodeResult.Invalid | ||
| } | ||
|
|
||
| if (result.isCanceled) { | ||
| return CodeResult.Canceled | ||
| } | ||
|
|
||
| val values = CallbackHelper.getValuesFromUri(result.intentData) | ||
| if (values.isEmpty()) { | ||
| return CodeResult.Invalid | ||
| } | ||
|
|
||
| val error = values[KEY_ERROR] | ||
| if (error != null) { | ||
| val description = values[KEY_ERROR_DESCRIPTION] ?: error | ||
| return CodeResult.Error(AuthenticationException(error, description)) | ||
| } | ||
|
|
||
| val code = values[KEY_CODE] | ||
| ?: return CodeResult.Error( | ||
| AuthenticationException( | ||
| "access_denied", | ||
| "No authorization code was received in the callback." | ||
| ) | ||
| ) | ||
|
|
||
| return CodeResult.Success(code = code, state = values[KEY_STATE]) | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
auth0/src/main/java/com/auth0/android/provider/PARCodeManager.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,88 @@ | ||
| package com.auth0.android.provider | ||
|
|
||
| import android.content.Context | ||
| import com.auth0.android.Auth0 | ||
| import com.auth0.android.authentication.AuthenticationException | ||
| import com.auth0.android.callback.Callback | ||
| import com.auth0.android.result.AuthorizationCode | ||
|
|
||
| /** | ||
| * Manager for handling PAR (Pushed Authorization Request) code-only flows. | ||
| * This manager handles opening the authorize URL with a request_uri and | ||
| * returns the authorization code to the caller for BFF token exchange. | ||
| */ | ||
| internal class PARCodeManager( | ||
| private val account: Auth0, | ||
| private val callback: Callback<AuthorizationCode, AuthenticationException>, | ||
| private val requestUri: String, | ||
| private val sessionTransferToken: String? = null, | ||
| private val ctOptions: CustomTabsOptions = CustomTabsOptions.newBuilder().build() | ||
| ) : ResumableManager() { | ||
|
|
||
| private var requestCode = 0 | ||
|
|
||
| fun startAuthentication(context: Context, requestCode: Int) { | ||
| this.requestCode = requestCode | ||
| val additionalParams = buildMap { | ||
| sessionTransferToken?.let { put("session_transfer_token", it) } | ||
| } | ||
| val uri = PARUtils.buildAuthorizeUri(account, requestUri, additionalParams) | ||
| AuthenticationActivity.authenticateUsingBrowser(context, uri, false, ctOptions) | ||
| } | ||
|
|
||
| override fun resume(result: AuthorizeResult): Boolean { | ||
|
utkrishtsahu marked this conversation as resolved.
|
||
| return when (val parsed = AuthorizeResultParser.parse(result, requestCode)) { | ||
| is AuthorizeResultParser.CodeResult.Success -> { | ||
| callback.onSuccess(AuthorizationCode(parsed.code, parsed.state)) | ||
| true | ||
| } | ||
| is AuthorizeResultParser.CodeResult.Error -> { | ||
| callback.onFailure(parsed.exception) | ||
| true | ||
| } | ||
| is AuthorizeResultParser.CodeResult.Canceled -> { | ||
| callback.onFailure( | ||
| AuthenticationException( | ||
| AuthenticationException.ERROR_VALUE_AUTHENTICATION_CANCELED, | ||
| "The user closed the browser app and the authentication was canceled." | ||
| ) | ||
| ) | ||
| true | ||
| } | ||
| AuthorizeResultParser.CodeResult.Invalid -> false | ||
| } | ||
| } | ||
|
|
||
| override fun failure(exception: AuthenticationException) { | ||
| callback.onFailure(exception) | ||
| } | ||
|
|
||
| internal fun toState(): PARCodeManagerState { | ||
| return PARCodeManagerState( | ||
| auth0 = account, | ||
| requestCode = requestCode, | ||
| requestUri = requestUri, | ||
| sessionTransferToken = sessionTransferToken, | ||
| ctOptions = ctOptions | ||
| ) | ||
| } | ||
|
|
||
| internal companion object { | ||
| private val TAG = PARCodeManager::class.java.simpleName | ||
|
|
||
| fun fromState( | ||
| state: PARCodeManagerState, | ||
| callback: Callback<AuthorizationCode, AuthenticationException> | ||
| ): PARCodeManager { | ||
| val manager = PARCodeManager( | ||
| account = state.auth0, | ||
| callback = callback, | ||
| requestUri = state.requestUri, | ||
| sessionTransferToken = state.sessionTransferToken, | ||
| ctOptions = state.ctOptions | ||
| ) | ||
| manager.requestCode = state.requestCode | ||
| return manager | ||
| } | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
auth0/src/main/java/com/auth0/android/provider/PARCodeManagerState.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,87 @@ | ||
| package com.auth0.android.provider | ||
|
|
||
| import android.os.Parcel | ||
| import android.os.Parcelable | ||
| import android.util.Base64 | ||
| import androidx.core.os.ParcelCompat | ||
| import com.auth0.android.Auth0 | ||
| import com.auth0.android.request.internal.GsonProvider | ||
| import com.google.gson.Gson | ||
|
|
||
| internal data class PARCodeManagerState( | ||
| val auth0: Auth0, | ||
| val requestCode: Int, | ||
| val requestUri: String, | ||
| val sessionTransferToken: String?, | ||
| val ctOptions: CustomTabsOptions | ||
| ) { | ||
|
|
||
| private class PARCodeManagerJson( | ||
| val auth0ClientId: String, | ||
| val auth0DomainUrl: String, | ||
| val auth0ConfigurationUrl: String?, | ||
| val requestCode: Int, | ||
| val requestUri: String, | ||
| val sessionTransferToken: String?, | ||
| val ctOptions: String | ||
| ) | ||
|
|
||
| fun serializeToJson(gson: Gson = GsonProvider.gson): String { | ||
| val parcel = Parcel.obtain() | ||
| try { | ||
| parcel.writeParcelable(ctOptions, Parcelable.PARCELABLE_WRITE_RETURN_VALUE) | ||
| val ctOptionsEncoded = Base64.encodeToString(parcel.marshall(), Base64.DEFAULT) | ||
|
|
||
| val json = PARCodeManagerJson( | ||
| auth0ClientId = auth0.clientId, | ||
| auth0DomainUrl = auth0.domain, | ||
| auth0ConfigurationUrl = auth0.configurationDomain, | ||
| requestCode = requestCode, | ||
| requestUri = requestUri, | ||
| sessionTransferToken = sessionTransferToken, | ||
| ctOptions = ctOptionsEncoded | ||
| ) | ||
| return gson.toJson(json) | ||
| } finally { | ||
| parcel.recycle() | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun deserializeState( | ||
| json: String, | ||
| gson: Gson = GsonProvider.gson | ||
| ): PARCodeManagerState { | ||
| val parcel = Parcel.obtain() | ||
| try { | ||
| val parsed = gson.fromJson(json, PARCodeManagerJson::class.java) | ||
|
|
||
| val decodedCtOptionsBytes = Base64.decode(parsed.ctOptions, Base64.DEFAULT) | ||
| parcel.unmarshall(decodedCtOptionsBytes, 0, decodedCtOptionsBytes.size) | ||
| parcel.setDataPosition(0) | ||
|
|
||
| val customTabsOptions = ParcelCompat.readParcelable( | ||
| parcel, | ||
| CustomTabsOptions::class.java.classLoader, | ||
| CustomTabsOptions::class.java | ||
| ) ?: error("Couldn't deserialize CustomTabsOptions from Parcel") | ||
|
|
||
| val auth0 = Auth0.getInstance( | ||
| clientId = parsed.auth0ClientId, | ||
| domain = parsed.auth0DomainUrl, | ||
| configurationDomain = parsed.auth0ConfigurationUrl | ||
| ) | ||
|
|
||
| return PARCodeManagerState( | ||
| auth0 = auth0, | ||
| requestCode = parsed.requestCode, | ||
| requestUri = parsed.requestUri, | ||
| sessionTransferToken = parsed.sessionTransferToken, | ||
| ctOptions = customTabsOptions | ||
| ) | ||
| } finally { | ||
| parcel.recycle() | ||
| } | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
auth0/src/main/java/com/auth0/android/provider/PARUtils.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,40 @@ | ||
| package com.auth0.android.provider | ||
|
|
||
| import android.net.Uri | ||
| import com.auth0.android.Auth0 | ||
| import com.auth0.android.authentication.AuthenticationException | ||
| import androidx.core.net.toUri | ||
|
|
||
| /** | ||
| * Shared utilities for PAR (Pushed Authorization Request) flows. | ||
| */ | ||
| internal object PARUtils { | ||
|
|
||
| internal const val REQUEST_URI_PREFIX = "urn:ietf:params:oauth:request_uri:" | ||
|
|
||
| /** | ||
| * Validates that the request_uri conforms to the expected format. | ||
| * @return true if valid, false otherwise. | ||
| */ | ||
| fun isValidRequestUri(requestUri: String): Boolean { | ||
| return requestUri.startsWith(REQUEST_URI_PREFIX) | ||
| } | ||
|
|
||
| /** | ||
| * Builds a minimal /authorize URI for PAR flows containing only client_id and request_uri, | ||
| * plus any additional query parameters. | ||
| */ | ||
| fun buildAuthorizeUri( | ||
| account: Auth0, | ||
| requestUri: String, | ||
| additionalParameters: Map<String, String> = emptyMap() | ||
| ): Uri { | ||
| val builder = account.authorizeUrl.toUri().buildUpon() | ||
| .appendQueryParameter("client_id", account.clientId) | ||
| .appendQueryParameter("request_uri", requestUri) | ||
| for ((key, value) in additionalParameters) { | ||
| builder.appendQueryParameter(key, value) | ||
| } | ||
| return builder.build() | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This manager doesn't survive Activity recreation. If the user rotates the device while the browser is open, managerInstance is lost and the callback silently fails.
OAuthManager handles this via toState() / onRestoreInstanceState(). PARCodeManager has no equivalent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added