-
Notifications
You must be signed in to change notification settings - Fork 23
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 #454 from ForgeRock/user-profile-sample
Enhance Sample App to support IDM related Callbacks.
- Loading branch information
Showing
11 changed files
with
444 additions
and
2 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
54 changes: 54 additions & 0 deletions
54
samples/app/src/main/java/com/example/app/callback/AttributeInput.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,54 @@ | ||
/* | ||
* 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 com.example.app.callback | ||
|
||
import org.forgerock.android.auth.callback.AttributeInputCallback | ||
|
||
val VALIDATION_ERROR = mutableMapOf( | ||
"VALID_USERNAME" to "Invalid username", | ||
"UNIQUE" to "Must be unique", | ||
"MIN_LENGTH" to "{prompt} Must be at least {minLength} characters long", | ||
"MAX_LENGTH" to "{prompt} Must be less than {maxLength} characters long", | ||
"AT_LEAST_X_CAPITAL_LETTERS" to "{prompt} Must have at least {numCaps} capital letter(s)", | ||
"AT_LEAST_X_NUMBERS" to "{prompt} Must have at least {numNums} number(s)", | ||
"VALID_EMAIL_ADDRESS_FORMAT" to "Invalid email format (example@example.com)", | ||
"CANNOT_CONTAIN_CHARACTERS" to "{prompt} Cannot contain characters: {forbiddenChars}", | ||
"CANNOT_CONTAIN_DUPLICATES" to "Cannot contain duplicates: {duplicateValue}", | ||
"REQUIRED" to "Cannot be blank", | ||
"MATCH_REGEXP" to "Has to match pattern: {regexp}", | ||
"VALID_TYPE" to "Must be one of the following types: {validTypes}. Cannot be {invalidType}", | ||
"VALID_NUMBER" to "{prompt} Invalid number", | ||
"MINIMUM_NUMBER_VALUE" to "{prompt} Less than minimum number value", | ||
"MAXIMUM_NUMBER_VALUE" to "{prompt} Greater than maximum number value", | ||
"VALID_NAME_FORMAT" to "Must have valid name characters", | ||
"VALID_PHONE_FORMAT" to "Must be a valid phone number", | ||
"CANNOT_CONTAIN_DUPLICATES" to "{prompt} must not contain duplicates {duplicateValue}", | ||
"CANNOT_CONTAIN_OTHERS" to "{prompt} Must not share characters with {disallowedFields}", | ||
"VALID_DATE" to "Must be a valid date", | ||
"IS_NEW" to "Must not be the same as the previous {historyLength} password(s)", | ||
"IS_NUMBER" to "Must be a number greater than or equal to zero.", | ||
"IS_NUMBER_GREATER_ZERO" to "Must be a number greater than or equal to 1.", | ||
"MIN_ITEMS" to "Must have at least {minItems} value(s)", | ||
"MUST_CONTAIN_LOWERCASE_CHARACTERS" to "Must have at least {minItems} lowercase letter(s)", | ||
"MUST_CONTAIN_SPECIAL_CHARACTERS" to "Must have at least {minItems} of these characters: {requiredChars}", | ||
"VALID_BOOLEAN" to "Value must be of type boolean.", | ||
"VALID_INT" to "Value must be of type integer.", | ||
) | ||
|
||
|
||
fun AttributeInputCallback.error(): String { | ||
return failedPolicies.joinToString("\n") { policy -> | ||
VALIDATION_ERROR[policy.policyRequirement]?.let { error -> | ||
policy.format(prompt, error) | ||
} ?: "" | ||
} | ||
} | ||
|
||
fun AttributeInputCallback.hasError(): Boolean { | ||
return failedPolicies.isNotEmpty() | ||
} |
46 changes: 46 additions & 0 deletions
46
samples/app/src/main/java/com/example/app/callback/BooleanAttributeInputCallback.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,46 @@ | ||
/* | ||
* 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 com.example.app.callback | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import org.forgerock.android.auth.callback.BooleanAttributeInputCallback | ||
|
||
@Composable | ||
fun BooleanAttributeInputCallback(callback: BooleanAttributeInputCallback) { | ||
|
||
var input by remember { | ||
mutableStateOf(callback.value) | ||
} | ||
|
||
Row(modifier = Modifier | ||
.padding(4.dp) | ||
.fillMaxWidth()) { | ||
Text(text = callback.prompt) | ||
Spacer(modifier = Modifier.weight(1f, true)) | ||
Switch( | ||
checked = input, | ||
onCheckedChange = { | ||
input = it | ||
callback.value = it | ||
} | ||
) | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
samples/app/src/main/java/com/example/app/callback/KbaCreateCallback.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,97 @@ | ||
/* | ||
* 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 com.example.app.callback | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.DropdownMenuItem | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ExposedDropdownMenuBox | ||
import androidx.compose.material3.ExposedDropdownMenuDefaults | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import org.forgerock.android.auth.callback.ChoiceCallback | ||
import org.forgerock.android.auth.callback.KbaCreateCallback | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun KbaCreateCallback(callback: KbaCreateCallback) { | ||
var expanded by remember { mutableStateOf(false) } | ||
var selectedItem by remember { | ||
mutableStateOf(callback.predefinedQuestions[0]) | ||
} | ||
|
||
var answer by remember { | ||
mutableStateOf("") | ||
} | ||
|
||
Column(modifier = Modifier | ||
.padding(8.dp) | ||
.fillMaxWidth()) { | ||
ExposedDropdownMenuBox( | ||
expanded = expanded, | ||
onExpandedChange = { | ||
expanded = !expanded | ||
} | ||
) { | ||
|
||
// text field | ||
TextField( | ||
modifier = Modifier.menuAnchor(), | ||
value = selectedItem, | ||
onValueChange = {}, | ||
readOnly = true, | ||
label = { Text(text = callback.prompt) }, | ||
trailingIcon = { | ||
ExposedDropdownMenuDefaults.TrailingIcon( | ||
expanded = expanded | ||
) | ||
}, | ||
colors = ExposedDropdownMenuDefaults.textFieldColors() | ||
) | ||
|
||
// menu | ||
ExposedDropdownMenu( | ||
expanded = expanded, | ||
onDismissRequest = { expanded = false } | ||
) { | ||
callback.predefinedQuestions.forEachIndexed { index, selectedOption -> | ||
// menu item | ||
DropdownMenuItem(text = { | ||
Text(text = selectedOption) | ||
}, onClick = { | ||
selectedItem = selectedOption | ||
expanded = false | ||
callback.setSelectedQuestion(callback.predefinedQuestions[index]) | ||
}) | ||
} | ||
} | ||
} | ||
OutlinedTextField( | ||
modifier = Modifier, | ||
value = answer, | ||
onValueChange = { value -> | ||
answer = value | ||
callback.setSelectedAnswer(answer) | ||
}, | ||
label = { Text(text = "Answer") }, | ||
) | ||
|
||
} | ||
|
||
} | ||
|
64 changes: 64 additions & 0 deletions
64
samples/app/src/main/java/com/example/app/callback/NumberAttributeInputCallback.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,64 @@ | ||
/* | ||
* 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 com.example.app.callback | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.core.text.isDigitsOnly | ||
import org.forgerock.android.auth.callback.NameCallback | ||
import org.forgerock.android.auth.callback.NumberAttributeInputCallback | ||
import org.json.JSONObject | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun NumberAttributeInputCallback(callback: NumberAttributeInputCallback) { | ||
|
||
var input by remember { | ||
mutableStateOf(callback.value?.toString() ?: "") | ||
} | ||
|
||
Row(modifier = Modifier | ||
.padding(4.dp) | ||
.fillMaxWidth()) { | ||
OutlinedTextField( | ||
modifier = Modifier, | ||
value = input, | ||
onValueChange = { value -> | ||
if (value.isDigitsOnly()) { | ||
input = value | ||
if (input.isNotEmpty()) { | ||
callback.value = input.toDouble() | ||
} | ||
} | ||
}, | ||
isError = callback.hasError(), | ||
supportingText = if (callback.hasError()) { | ||
@Composable { | ||
Text( | ||
text = callback.error(), | ||
style = MaterialTheme.typography.bodySmall | ||
) | ||
} | ||
} else null, | ||
label = { Text(callback.prompt) }, | ||
) | ||
} | ||
} |
Oops, something went wrong.