|
| 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.request.internal.GsonProvider |
| 9 | +import com.google.gson.Gson |
| 10 | + |
| 11 | +internal data class PARCodeManagerState( |
| 12 | + val auth0: Auth0, |
| 13 | + val requestCode: Int, |
| 14 | + val requestUri: String, |
| 15 | + val sessionTransferToken: String?, |
| 16 | + val ctOptions: CustomTabsOptions |
| 17 | +) { |
| 18 | + |
| 19 | + private class PARCodeManagerJson( |
| 20 | + val auth0ClientId: String, |
| 21 | + val auth0DomainUrl: String, |
| 22 | + val auth0ConfigurationUrl: String?, |
| 23 | + val requestCode: Int, |
| 24 | + val requestUri: String, |
| 25 | + val sessionTransferToken: String?, |
| 26 | + val ctOptions: String |
| 27 | + ) |
| 28 | + |
| 29 | + fun serializeToJson(gson: Gson = GsonProvider.gson): String { |
| 30 | + val parcel = Parcel.obtain() |
| 31 | + try { |
| 32 | + parcel.writeParcelable(ctOptions, Parcelable.PARCELABLE_WRITE_RETURN_VALUE) |
| 33 | + val ctOptionsEncoded = Base64.encodeToString(parcel.marshall(), Base64.DEFAULT) |
| 34 | + |
| 35 | + val json = PARCodeManagerJson( |
| 36 | + auth0ClientId = auth0.clientId, |
| 37 | + auth0DomainUrl = auth0.domain, |
| 38 | + auth0ConfigurationUrl = auth0.configurationDomain, |
| 39 | + requestCode = requestCode, |
| 40 | + requestUri = requestUri, |
| 41 | + sessionTransferToken = sessionTransferToken, |
| 42 | + ctOptions = ctOptionsEncoded |
| 43 | + ) |
| 44 | + return gson.toJson(json) |
| 45 | + } finally { |
| 46 | + parcel.recycle() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + companion object { |
| 51 | + fun deserializeState( |
| 52 | + json: String, |
| 53 | + gson: Gson = GsonProvider.gson |
| 54 | + ): PARCodeManagerState { |
| 55 | + val parcel = Parcel.obtain() |
| 56 | + try { |
| 57 | + val parsed = gson.fromJson(json, PARCodeManagerJson::class.java) |
| 58 | + |
| 59 | + val decodedCtOptionsBytes = Base64.decode(parsed.ctOptions, Base64.DEFAULT) |
| 60 | + parcel.unmarshall(decodedCtOptionsBytes, 0, decodedCtOptionsBytes.size) |
| 61 | + parcel.setDataPosition(0) |
| 62 | + |
| 63 | + val customTabsOptions = ParcelCompat.readParcelable( |
| 64 | + parcel, |
| 65 | + CustomTabsOptions::class.java.classLoader, |
| 66 | + CustomTabsOptions::class.java |
| 67 | + ) ?: error("Couldn't deserialize CustomTabsOptions from Parcel") |
| 68 | + |
| 69 | + val auth0 = Auth0.getInstance( |
| 70 | + clientId = parsed.auth0ClientId, |
| 71 | + domain = parsed.auth0DomainUrl, |
| 72 | + configurationDomain = parsed.auth0ConfigurationUrl |
| 73 | + ) |
| 74 | + |
| 75 | + return PARCodeManagerState( |
| 76 | + auth0 = auth0, |
| 77 | + requestCode = parsed.requestCode, |
| 78 | + requestUri = parsed.requestUri, |
| 79 | + sessionTransferToken = parsed.sessionTransferToken, |
| 80 | + ctOptions = customTabsOptions |
| 81 | + ) |
| 82 | + } finally { |
| 83 | + parcel.recycle() |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments