Skip to content
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-3227_Skipping type 4 TextOutputCallbacks #434

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.X.0]
#### Added
- Skip Type 4 TextOutputCallback [SDKS-3227]

## [4.5.0]
#### Added
- Added SDK support for deleting registered WebAuthn devices from the server. [SDKS-1710]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 - 2023 ForgeRock. All rights reserved.
* Copyright (c) 2019 - 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.
Expand All @@ -11,7 +11,9 @@

import androidx.annotation.VisibleForTesting;

import org.forgerock.android.auth.callback.AdditionalParameterCallback;
import org.forgerock.android.auth.callback.Callback;
import org.forgerock.android.auth.callback.TextOutputCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -34,6 +36,7 @@ public class Node implements Serializable {
private final String description;
private final String authServiceId;
private final List<Callback> callbacks;
private static final String TAG = "Node";

@VisibleForTesting
public Node(String authId, String stage, String header, String description, String authServiceId, List<Callback> callbacks) {
Expand All @@ -59,7 +62,10 @@ JSONObject toJsonObject() throws JSONException {
}
JSONArray array = new JSONArray();
for (Callback cb : callbacks) {
array.put(new JSONObject(cb.getContent()));
String content = cb.getContent();
if (content != null) {
array.put(new JSONObject(content));
}
}
jsonObject.put("callbacks", array);
return jsonObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 ForgeRock. All rights reserved.
* Copyright (c) 2019 - 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.
Expand Down Expand Up @@ -72,4 +72,13 @@ public String getType() {
return "TextOutputCallback";
}

@Override
public String getContent() {
if (messageType == 4) {
return null;
} else {
return super.getContent();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 ForgeRock. All rights reserved.
* Copyright (c) 2023 - 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.
Expand Down Expand Up @@ -36,4 +36,24 @@ class TextOutputCallbackTest {
assertEquals("This is a Message Node", callback.message)
assertEquals(1, callback.messageType.toLong())
}

@Test
@Throws(JSONException::class)
fun testType4Message() {
val raw = JSONObject("""{
"type": "TextOutputCallback",
"output": [
{
"name": "message",
"value": "Javascript"
},
{
"name": "messageType",
"value": "4"
}
]
}""")
val callback = TextOutputCallback(raw, 0)
assertNull(callback.getContent())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;

import org.forgerock.android.auth.AndroidBaseTest;
import org.forgerock.android.auth.FRSession;
import org.forgerock.android.auth.Node;
import org.forgerock.android.auth.NodeListener;
import org.forgerock.android.auth.NodeListenerFuture;
import org.forgerock.android.auth.UsernamePasswordNodeListener;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class TextOutputCallbackTest extends AndroidBaseTest {
protected final static String TREE = "TextOutputCallbackTest";

@After
public void logoutSession() {
if (FRSession.getCurrentSession() != null) {
FRSession.getCurrentSession().logout();
}
}

@Test
public void testTextOutputCallback() throws ExecutionException, InterruptedException {
final int[] textOutputCallbacksReceived = {0};
NodeListenerFuture<FRSession> nodeListenerFuture = new UsernamePasswordNodeListener(context) {
final NodeListener<FRSession> nodeListener = this;

@Override
public void onCallbackReceived(Node node) {
if (node.getCallback(TextOutputCallback.class) != null) {
textOutputCallbacksReceived[0]++;
// The TextOutputCallbackProducer script sends 4 TextOutput callbacks (of all types)
List<Callback> callbacks = node.getCallbacks();
assertThat(callbacks.size() == 4);

for (int i = 0; i < callbacks.size(); i++) {
TextOutputCallback callback = (TextOutputCallback) callbacks.get(i);
assert( callback.getMessage().equals("TextOutput Type 0 (INFO)") ||
callback.getMessage().equals("TextOutput Type 1 (WARNING)") ||
callback.getMessage().equals("TextOutput Type 2 (ERROR)") ||
callback.getMessage().equals("TextOutput Type 4 (SCRIPT)"));

assert( callback.getMessageType() == 0 ||
callback.getMessageType() == 1 ||
callback.getMessageType() == 2 ||
callback.getMessageType() == 4);
}
node.next(context, nodeListener);
return;
}
super.onCallbackReceived(node);
}
};

FRSession.authenticate(context, TREE, nodeListenerFuture);
Assert.assertNotNull(nodeListenerFuture.get());
assertThat(textOutputCallbacksReceived[0]).isEqualTo(1);

// Ensure that the journey finishes with success
// Note that the SDK should NOT send TextOutput of type 4 to AM (SDKS-3227)
// If it does the journey will fail (see the `TextOutputCallbackProducer` script...)
Assert.assertNotNull(FRSession.getCurrentSession());
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Error
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
Expand All @@ -40,7 +41,7 @@ fun TextOutputCallback(callback: TextOutputCallback) {
INFORMATION -> Icon(Icons.Filled.Info, null)
WARNING -> Icon(Icons.Filled.Warning, null)
ERROR -> Icon(Icons.Filled.Error, null)
else -> Icon(Icons.Filled.Info, null)
else -> Icon(Icons.Filled.Settings, null)
}
Spacer(Modifier.width(8.dp))
Text(text = callback.message,
Expand Down
3 changes: 3 additions & 0 deletions samples/app/src/main/java/com/example/app/journey/Journey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package com.example.app.journey
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -82,6 +84,7 @@ fun Journey(state: JourneyState,
onFailure: ((Exception) -> Unit)?) {

Column(modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(8.dp)
.fillMaxWidth()) {
state.session?.apply {
Expand Down
Loading