|
| 1 | +package com.auth0.android.provider |
| 2 | + |
| 3 | +import android.os.Parcel |
| 4 | +import android.os.Parcelable |
| 5 | +import android.util.Base64 |
| 6 | +import androidx.core.os.ParcelCompat |
| 7 | +import com.auth0.android.Auth0 |
| 8 | +import com.auth0.android.authentication.AuthenticationAPIClient |
| 9 | +import com.auth0.android.request.internal.GsonProvider |
| 10 | +import com.google.gson.Gson |
| 11 | + |
| 12 | +internal data class OAuthManagerState( |
| 13 | + val auth0: Auth0, |
| 14 | + val parameters: Map<String, String>, |
| 15 | + val headers: Map<String, String>, |
| 16 | + val requestCode: Int = 0, |
| 17 | + val ctOptions: CustomTabsOptions, |
| 18 | + val pkce: PKCE?, |
| 19 | + val idTokenVerificationLeeway: Int?, |
| 20 | + val idTokenVerificationIssuer: String? |
| 21 | +) { |
| 22 | + |
| 23 | + private class OAuthManagerJson( |
| 24 | + val auth0ClientId: String, |
| 25 | + val auth0DomainUrl: String, |
| 26 | + val auth0ConfigurationUrl: String?, |
| 27 | + val parameters: Map<String, String>, |
| 28 | + val headers: Map<String, String>, |
| 29 | + val requestCode: Int = 0, |
| 30 | + val ctOptions: String, |
| 31 | + val redirectUri: String, |
| 32 | + val codeChallenge: String, |
| 33 | + val codeVerifier: String, |
| 34 | + val idTokenVerificationLeeway: Int?, |
| 35 | + val idTokenVerificationIssuer: String? |
| 36 | + ) |
| 37 | + |
| 38 | + fun serializeToJson( |
| 39 | + gson: Gson = GsonProvider.gson, |
| 40 | + ): String { |
| 41 | + val parcel = Parcel.obtain() |
| 42 | + try { |
| 43 | + parcel.writeParcelable(ctOptions, Parcelable.PARCELABLE_WRITE_RETURN_VALUE) |
| 44 | + val ctOptionsEncoded = Base64.encodeToString(parcel.marshall(), Base64.DEFAULT) |
| 45 | + |
| 46 | + val json = OAuthManagerJson( |
| 47 | + auth0ClientId = auth0.clientId, |
| 48 | + auth0ConfigurationUrl = auth0.configurationDomain, |
| 49 | + auth0DomainUrl = auth0.domain, |
| 50 | + parameters = parameters, |
| 51 | + headers = headers, |
| 52 | + requestCode = requestCode, |
| 53 | + ctOptions = ctOptionsEncoded, |
| 54 | + redirectUri = pkce?.redirectUri.orEmpty(), |
| 55 | + codeVerifier = pkce?.codeVerifier.orEmpty(), |
| 56 | + codeChallenge = pkce?.codeChallenge.orEmpty(), |
| 57 | + idTokenVerificationIssuer = idTokenVerificationIssuer, |
| 58 | + idTokenVerificationLeeway = idTokenVerificationLeeway, |
| 59 | + ) |
| 60 | + return gson.toJson(json) |
| 61 | + } finally { |
| 62 | + parcel.recycle() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + companion object { |
| 67 | + fun deserializeState( |
| 68 | + json: String, |
| 69 | + gson: Gson = GsonProvider.gson, |
| 70 | + ): OAuthManagerState { |
| 71 | + val parcel = Parcel.obtain() |
| 72 | + try { |
| 73 | + val oauthManagerJson = gson.fromJson(json, OAuthManagerJson::class.java) |
| 74 | + |
| 75 | + val decodedCtOptionsBytes = Base64.decode(oauthManagerJson.ctOptions, Base64.DEFAULT) |
| 76 | + parcel.unmarshall(decodedCtOptionsBytes, 0, decodedCtOptionsBytes.size) |
| 77 | + parcel.setDataPosition(0) |
| 78 | + |
| 79 | + val customTabsOptions = ParcelCompat.readParcelable( |
| 80 | + parcel, |
| 81 | + CustomTabsOptions::class.java.classLoader, |
| 82 | + CustomTabsOptions::class.java |
| 83 | + ) ?: error("Couldn't deserialize from Parcel") |
| 84 | + |
| 85 | + val auth0 = Auth0.getInstance( |
| 86 | + clientId = oauthManagerJson.auth0ClientId, |
| 87 | + domain = oauthManagerJson.auth0DomainUrl, |
| 88 | + configurationDomain = oauthManagerJson.auth0ConfigurationUrl, |
| 89 | + ) |
| 90 | + |
| 91 | + return OAuthManagerState( |
| 92 | + auth0 = auth0, |
| 93 | + parameters = oauthManagerJson.parameters, |
| 94 | + headers = oauthManagerJson.headers, |
| 95 | + requestCode = oauthManagerJson.requestCode, |
| 96 | + ctOptions = customTabsOptions, |
| 97 | + pkce = PKCE( |
| 98 | + AuthenticationAPIClient(auth0), |
| 99 | + oauthManagerJson.codeVerifier, |
| 100 | + oauthManagerJson.redirectUri, |
| 101 | + oauthManagerJson.codeChallenge, |
| 102 | + oauthManagerJson.headers, |
| 103 | + ), |
| 104 | + idTokenVerificationIssuer = oauthManagerJson.idTokenVerificationIssuer, |
| 105 | + idTokenVerificationLeeway = oauthManagerJson.idTokenVerificationLeeway, |
| 106 | + ) |
| 107 | + } finally { |
| 108 | + parcel.recycle() |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments