-
Notifications
You must be signed in to change notification settings - Fork 23
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
SDKS-3297 Support MetadataCallback used by Marketplace PingOne Protect #436
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
147 changes: 147 additions & 0 deletions
147
forgerock-auth/src/main/java/org/forgerock/android/auth/callback/AbstractProtectCallback.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,147 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth.callback | ||
|
||
import androidx.annotation.Keep | ||
import org.forgerock.android.auth.Node | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
const val _ACTION = "_action" | ||
const val _TYPE = "_type" | ||
const val CLIENT_ERROR = "clientError" | ||
const val PING_ONE_RISK_EVALUATION_SIGNALS = "pingone_risk_evaluation_signals" | ||
const val PING_ONE_PROTECT = "PingOneProtect" | ||
const val PROTECT_INITIALIZE = "protect_initialize" | ||
const val PROTECT_RISK_EVALUATION = "protect_risk_evaluation" | ||
const val PING_ONE_PROTECT_INITIALIZE_CALLBACK = "PingOneProtectInitializeCallback" | ||
const val PING_ONE_PROTECT_EVALUATION_CALLBACK = "PingOneProtectEvaluationCallback" | ||
|
||
/** | ||
* Abstract Protect Callback that provides the raw content of the Callback, and common methods | ||
* for sub classes to access. | ||
*/ | ||
abstract class AbstractProtectCallback: NodeAware, AbstractCallback { | ||
|
||
/** | ||
* Indicates if this callback is derived from a [MetadataCallback] | ||
*/ | ||
protected var derivedCallback: Boolean = false | ||
|
||
/** | ||
* Constructor for [AbstractProtectCallback]. Capable of parsing the [MetadataCallback] used for Protect. | ||
*/ | ||
@Keep | ||
constructor(jsonObject: JSONObject, index: Int) : super(jsonObject, index) { | ||
val type = jsonObject.optString("type") | ||
if (type == "MetadataCallback") { | ||
derivedCallback = true | ||
jsonObject.optJSONArray("output")?.let { output -> | ||
output.getJSONObject(0)?.let { nameValuePair -> | ||
if (nameValuePair.optString("name") == "data") { | ||
nameValuePair.optJSONObject("value")?.let { value -> | ||
value.keys().forEach { attr -> | ||
setAttribute(attr, value.get(attr)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Keep | ||
constructor() : super() | ||
|
||
/** | ||
* The [Node] that associate with this Callback | ||
*/ | ||
private lateinit var node: Node | ||
|
||
override fun setNode(node: Node) { | ||
this.node = node | ||
} | ||
|
||
/** | ||
* Get the [Node] that associate with this Callback | ||
* | ||
* @return The [Node] that associate with this Callback | ||
*/ | ||
protected fun getNode(): Node { | ||
return node | ||
} | ||
|
||
/** | ||
* Input the Client Error to the server | ||
* | ||
* @param value Protect ErrorType | ||
* @param index The index of the error | ||
*/ | ||
fun setClientError(value: String, index: Int) { | ||
if (derivedCallback) { | ||
setClientErrorInHiddenCallback(value); | ||
} else { | ||
super.setValue(value, index) | ||
} | ||
} | ||
|
||
/** | ||
* Set the client error to the [HiddenValueCallback] which associated with the Protect | ||
* Callback. | ||
* | ||
* @param value The Value to set to the [HiddenValueCallback] | ||
*/ | ||
private fun setClientErrorInHiddenCallback(value: String) { | ||
for (callback in node.callbacks) { | ||
if (callback is HiddenValueCallback) { | ||
if (callback.id.contains(CLIENT_ERROR)) { | ||
callback.value = value | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
/** | ||
* Check if this callback is [PingOneProtectInitializeCallback] Type | ||
* | ||
* @param value The callback raw data json. | ||
* @return True if this is a [PingOneProtectInitializeCallback] Type, else false | ||
*/ | ||
@JvmStatic | ||
fun isPingOneProtectInitializeCallback(value: JSONObject): Boolean { | ||
return try { | ||
value.has(_TYPE) && value.getString(_TYPE) == PING_ONE_PROTECT && (value.has( | ||
_ACTION | ||
) && value.getString(_ACTION) == PROTECT_INITIALIZE) | ||
} catch (e: JSONException) { | ||
false | ||
} | ||
} | ||
|
||
/** | ||
* Check if this callback is [PingOneProtectEvaluationCallback] Type | ||
* | ||
* @param value The callback raw data json. | ||
* @return True if this is a [PingOneProtectEvaluationCallback] Type, else false | ||
*/ | ||
@JvmStatic | ||
fun isPingOneProtectEvaluationCallback(value: JSONObject): Boolean { | ||
return try { | ||
value.has(_TYPE) && value.getString(_TYPE) == PING_ONE_PROTECT && (value.has( | ||
_ACTION | ||
) && value.getString(_ACTION) == PROTECT_RISK_EVALUATION) | ||
} catch (e: JSONException) { | ||
false | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"authId": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoSW5kZXhWYWx1ZSI6IndlYmF1dGhuX3JlZyIsIm90ayI6Ijluc2E1YWVva2E3YXBuaW84bHBycXAxc242IiwiYXV0aEluZGV4VHlwZSI6InNlcnZpY2UiLCJyZWFsbSI6Ii8iLCJzZXNzaW9uSWQiOiIqQUFKVFNRQUNNREVBQkhSNWNHVUFDRXBYV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0RWaWJWZHZTMFowTkRoZlgyeEVWbTFZUkVSblIydHlWR3hMZFRoTlNVazFNWGszUWxKVFFrZEdkV2N4VUhwaVVuazRVRGx3TlZKaFJtVXRORU5GVEhGdFNsOUdMV2RDZG1ob1RUWldibUZaVVdoTGJ6Y3laR05rV2s1a1pUaEllazVCYm1WTVQzTkNVRE5aTkRscVdHVkNaR2gwVmpaalNsOXBMWHBxYjFSVVowSktVVkJXVUdoSVNtTnhkamhzTFRaa05sZGhlRWgzWnpaSmJUUlNhek4zTFVNMFlVVlBNREI0U1U5Nk0zVTJRVU5HVFRGakxVaE5jMUZsZVV0c1NtOUtOVFZuT0ZSMFJubE9WVFpVVm5OemRYVmlSRzVTTlVoYWJWazVVRGhuZEZSQmJ6VlBUakk0V1c5WVgxcDRZVk5aUXpGek1UUktTV3hoYzJ4NE1uTmFOelUzVWpVMGVFNUlWM3BtVDNkTlJtazVWWHA0VjFjNVpVVnVjSGw2YUhsT1NqRnlNelJNYnk1R1UyTnNabk10YkdSalpWSmhNVjh5VmtWWGRqaEIuSElHZFprUVNwbzR4U0dUVmlabEdSN3psRExvV09uOHhNQmt3MVRVZ05qVSIsImV4cCI6MTYxMzY5NTI1MywiaWF0IjoxNjEzNjk0OTUzfQ.BOdMUcaZbRNGkYAQu-Z_sBZMMHW4hdKDrXnBCbZhrxA", | ||
"callbacks": [ | ||
{ | ||
"type": "MetadataCallback", | ||
"output": [ | ||
{ | ||
"name": "data", | ||
"value": { | ||
"_type": "PingOneProtect", | ||
"_action": "protect_initialize", | ||
"envId": "some_id", | ||
"consoleLogEnabled": true, | ||
"deviceAttributesToIgnore": [], | ||
"customHost": "", | ||
"lazyMetadata": true, | ||
"behavioralDataCollection": true, | ||
"disableHub": true, | ||
"deviceKeyRsyncIntervals": 10, | ||
"enableTrust": true, | ||
"disableTags": true | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "HiddenValueCallback", | ||
"output": [ | ||
{ | ||
"name": "value", | ||
"value": "" | ||
}, | ||
{ | ||
"name": "id", | ||
"value": "clientError" | ||
} | ||
], | ||
"input": [ | ||
{ | ||
"name": "IDToken2", | ||
"value": "clientError" | ||
} | ||
] | ||
} | ||
] | ||
} |
57 changes: 57 additions & 0 deletions
57
forgerock-auth/src/test/resources/protect_risk_evaluation.json
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,57 @@ | ||
{ | ||
"authId": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoSW5kZXhWYWx1ZSI6IndlYmF1dGhuX3JlZyIsIm90ayI6Ijluc2E1YWVva2E3YXBuaW84bHBycXAxc242IiwiYXV0aEluZGV4VHlwZSI6InNlcnZpY2UiLCJyZWFsbSI6Ii8iLCJzZXNzaW9uSWQiOiIqQUFKVFNRQUNNREVBQkhSNWNHVUFDRXBYVkY5QlZWUklBQUpUTVFBQSpleUowZVhBaU9pSktWMVFpTENKamRIa2lPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LlpYbEtNR1ZZUVdsUGFVcExWakZSYVV4RFNteGliVTFwVDJsS1FrMVVTVFJSTUVwRVRGVm9WRTFxVlRKSmFYZHBXVmQ0YmtscWIybGFSMng1U1c0d0xpNWpVSHBGZG1OVFpIRk9UbHB4UVZsaExWaEhhMWhSTG1wMVVscEZkRFZ4Y2tsRlpWQTNOMDh4TUhSbk5IbGlVRTV2UW5acmIzUjBiM2RXWDA5dGFYaDVjWE5uWm1OelFXVlJTR1pWVVRrd2NsaHRiRXBFVTNkNFZVUjZibEJrZUVWWVJXSllkRUZYUlhWeVdXWjVOMnhwT0Vaa1ZHcDNaSGxDT1VjM2Nrd3ROVlJsZG1VNFlsazRUM1ZSU0VOclkweDBSbEZYT1ZkdmNtRlpNSFpvWlVWMGJWZFhTM3BaWlZsT2FXSTJPSEV0Um5SUGFVc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k10YkdSalpWSmhNVjh5VmtWWGRqaEIuSElHZFprUVNwbzR4U0dUVmlabEdSN3psRExvV09uOHhNQmt3MVRVZ05qVSIsImV4cCI6MTYxMzY5NTI1MywiaWF0IjoxNjEzNjk0OTUzfQ.BOdMUcaZbRNGkYAQu-Z_sBZMMHW4hdKDrXnBCbZhrxA", | ||
"callbacks": [ | ||
{ | ||
"type": "MetadataCallback", | ||
"output": [ | ||
{ | ||
"name": "data", | ||
"value": { | ||
"_type": "PingOneProtect", | ||
"_action": "protect_risk_evaluation", | ||
"envId" : "some_id", | ||
"pauseBehavioralData" : true | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "HiddenValueCallback", | ||
"output": [ | ||
{ | ||
"name": "value", | ||
"value": "" | ||
}, | ||
{ | ||
"name": "id", | ||
"value": "pingone_risk_evaluation_signals" | ||
} | ||
], | ||
"input": [ | ||
{ | ||
"name": "IDToken1", | ||
"value": "pingone_risk_evaluation_signals" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "HiddenValueCallback", | ||
"output": [ | ||
{ | ||
"name": "value", | ||
"value": "" | ||
}, | ||
{ | ||
"name": "id", | ||
"value": "clientError" | ||
} | ||
], | ||
"input": [ | ||
{ | ||
"name": "IDToken1", | ||
"value": "clientError" | ||
} | ||
] | ||
} | ||
] | ||
} |
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 |
---|---|---|
|
@@ -8,21 +8,24 @@ package org.forgerock.android.auth | |
|
||
import android.content.Context | ||
import androidx.annotation.Keep | ||
import org.forgerock.android.auth.callback.AbstractCallback | ||
import org.forgerock.android.auth.callback.AbstractProtectCallback | ||
witrisna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import org.forgerock.android.auth.callback.HiddenValueCallback | ||
import org.forgerock.android.auth.callback.PING_ONE_RISK_EVALUATION_SIGNALS | ||
import org.json.JSONObject | ||
|
||
private val TAG = PingOneProtectEvaluationCallback::class.java.simpleName | ||
|
||
/** | ||
* Callback to evaluate Ping One Protect | ||
*/ | ||
open class PingOneProtectEvaluationCallback : AbstractCallback { | ||
open class PingOneProtectEvaluationCallback : AbstractProtectCallback { | ||
|
||
@Keep | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. lets keep both constructors and PingOneProtectEvaluationCallback: AbstractProtectCallback |
||
constructor(jsonObject: JSONObject, index: Int) : super(jsonObject, index) | ||
|
||
@Keep | ||
constructor() : super() | ||
|
||
/** | ||
* The pauseBehavioralData received from server | ||
*/ | ||
|
@@ -43,15 +46,26 @@ open class PingOneProtectEvaluationCallback : AbstractCallback { | |
* @param value The JWS value. | ||
*/ | ||
fun setSignals(value: String) { | ||
super.setValue(value, 0) | ||
if (derivedCallback) { | ||
setSignalsInHiddenCallback(value) | ||
} else { | ||
super.setValue(value, 0) | ||
} | ||
} | ||
|
||
/** | ||
* Input the Client Error to the server | ||
* @param value Protect ErrorType . | ||
* Set the signals to the [HiddenValueCallback] which associated with the callback. | ||
* | ||
* @param value The Value to set to the [HiddenValueCallback]. | ||
*/ | ||
fun setClientError(value: String) { | ||
super.setValue(value, 1) | ||
private fun setSignalsInHiddenCallback(value: String) { | ||
for (callback in getNode().callbacks) { | ||
if (callback is HiddenValueCallback) { | ||
if (callback.id.contains(PING_ONE_RISK_EVALUATION_SIGNALS)) { | ||
callback.value = value | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -69,10 +83,11 @@ open class PingOneProtectEvaluationCallback : AbstractCallback { | |
} | ||
catch (e: Exception) { | ||
Logger.error(TAG, t = e, message = e.message) | ||
setClientError(e.message ?: "clientError") | ||
setClientError(e.message ?: "clientError", 1) | ||
throw e | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
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.
Here we can keep two constructors
abstract class AbstractProtectCallback: NodeAware, AbstractCallback {
}