-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from auth0/ft-gson-singleton
Move to a JSON client singleton
- Loading branch information
Showing
26 changed files
with
1,090 additions
and
866 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
43 changes: 0 additions & 43 deletions
43
auth0/src/main/java/com/auth0/android/request/internal/GsonProvider.java
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
auth0/src/main/java/com/auth0/android/request/internal/GsonProvider.kt
This file contains 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.request.internal | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import com.auth0.android.result.Credentials | ||
import com.auth0.android.result.UserProfile | ||
import com.auth0.android.util.JsonRequiredTypeAdapterFactory | ||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.reflect.TypeToken | ||
import java.security.PublicKey | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
internal object GsonProvider { | ||
internal val gson: Gson | ||
private var sdf: SimpleDateFormat | ||
private const val DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" | ||
|
||
init { | ||
val jwksType = TypeToken.getParameterized( | ||
Map::class.java, | ||
String::class.java, | ||
PublicKey::class.java | ||
).type | ||
gson = GsonBuilder() | ||
.registerTypeAdapterFactory(JsonRequiredTypeAdapterFactory()) | ||
.registerTypeAdapter(UserProfile::class.java, UserProfileDeserializer()) | ||
.registerTypeAdapter(Credentials::class.java, CredentialsDeserializer()) | ||
.registerTypeAdapter(jwksType, JwksDeserializer()) | ||
.setDateFormat(DATE_FORMAT) | ||
.create() | ||
sdf = SimpleDateFormat(DATE_FORMAT, Locale.US) | ||
} | ||
|
||
@JvmStatic | ||
@VisibleForTesting | ||
fun formatDate(date: Date): String { | ||
return sdf.format(date) | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
auth0/src/main/java/com/auth0/android/request/internal/JwksDeserializer.java
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
auth0/src/main/java/com/auth0/android/request/internal/JwksDeserializer.kt
This file contains 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,81 @@ | ||
package com.auth0.android.request.internal | ||
|
||
import android.util.Base64 | ||
import android.util.Log | ||
import com.google.gson.JsonDeserializationContext | ||
import com.google.gson.JsonDeserializer | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonParseException | ||
import java.lang.reflect.Type | ||
import java.math.BigInteger | ||
import java.security.KeyFactory | ||
import java.security.NoSuchAlgorithmException | ||
import java.security.PublicKey | ||
import java.security.spec.InvalidKeySpecException | ||
import java.security.spec.RSAPublicKeySpec | ||
|
||
internal class JwksDeserializer : JsonDeserializer<Map<String, PublicKey>> { | ||
@Throws(JsonParseException::class) | ||
override fun deserialize( | ||
json: JsonElement, | ||
typeOfT: Type, | ||
context: JsonDeserializationContext | ||
): Map<String, PublicKey> { | ||
if (!json.isJsonObject || json.isJsonNull || json.asJsonObject.entrySet().isEmpty()) { | ||
throw JsonParseException("jwks json must be a valid and non-empty json object") | ||
} | ||
val jwks = mutableMapOf<String, PublicKey>() | ||
val keys = json.asJsonObject.getAsJsonArray("keys") | ||
for (k in keys) { | ||
val currentKey = k.asJsonObject | ||
val keyAlg = context.deserialize<String>(currentKey["alg"], String::class.java) | ||
val keyUse = context.deserialize<String>(currentKey["use"], String::class.java) | ||
if (RSA_ALGORITHM != keyAlg || USE_SIGNING != keyUse) { | ||
//Key not supported at this time | ||
continue | ||
} | ||
val keyType = context.deserialize<String>(currentKey["kty"], String::class.java) | ||
val keyId = context.deserialize<String>(currentKey["kid"], String::class.java) | ||
val keyModulus = context.deserialize<String>(currentKey["n"], String::class.java) | ||
val keyPublicExponent = context.deserialize<String>(currentKey["e"], String::class.java) | ||
try { | ||
val kf = KeyFactory.getInstance(keyType) | ||
val modulus = BigInteger( | ||
1, | ||
Base64.decode( | ||
keyModulus, | ||
Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP | ||
) | ||
) | ||
val exponent = BigInteger( | ||
1, | ||
Base64.decode( | ||
keyPublicExponent, | ||
Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP | ||
) | ||
) | ||
val pub = kf.generatePublic(RSAPublicKeySpec(modulus, exponent)) | ||
jwks[keyId] = pub | ||
} catch (e: NoSuchAlgorithmException) { | ||
Log.e( | ||
JwksDeserializer::class.java.simpleName, | ||
"Could not parse the JWK with ID $keyId", | ||
e | ||
) | ||
//Would result in an empty key set | ||
} catch (e: InvalidKeySpecException) { | ||
Log.e( | ||
JwksDeserializer::class.java.simpleName, | ||
"Could not parse the JWK with ID $keyId", | ||
e | ||
) | ||
} | ||
} | ||
return jwks.toMap() | ||
} | ||
|
||
companion object { | ||
private const val RSA_ALGORITHM = "RS256" | ||
private const val USE_SIGNING = "sig" | ||
} | ||
} |
This file contains 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 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 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.