Skip to content

Commit c43cc4e

Browse files
committed
added functions to get messages for triggered token
1 parent 5ba3578 commit c43cc4e

4 files changed

Lines changed: 66 additions & 9 deletions

File tree

src/main/java/org/privacyidea/Endpoint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.charset.StandardCharsets;
2828
import java.security.KeyManagementException;
2929
import java.security.NoSuchAlgorithmException;
30+
import java.util.Arrays;
3031
import java.util.Collections;
3132
import java.util.LinkedHashMap;
3233
import java.util.List;
@@ -60,7 +61,7 @@
6061
class Endpoint {
6162

6263
private final PrivacyIDEA privacyIDEA;
63-
private List<String> logExcludedEndpointPrints = Collections.emptyList(); //Arrays.asList(PIConstants.ENDPOINT_AUTH, PIConstants.ENDPOINT_POLLTRANSACTION); //
64+
private List<String> logExcludedEndpointPrints = Arrays.asList(PIConstants.ENDPOINT_AUTH, PIConstants.ENDPOINT_POLLTRANSACTION); //Collections.emptyList(); //
6465
private final PIConfig piconfig;
6566
private final OkHttpClient client;
6667

src/main/java/org/privacyidea/PIResponse.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.gson.JsonSyntaxException;
2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.function.Predicate;
2728
import java.util.stream.Collectors;
2829

2930
import static org.privacyidea.PIConstants.ATTRIBUTES;
@@ -40,6 +41,7 @@
4041
import static org.privacyidea.PIConstants.SERIAL;
4142
import static org.privacyidea.PIConstants.SIGNATURE;
4243
import static org.privacyidea.PIConstants.STATUS;
44+
import static org.privacyidea.PIConstants.TOKEN_TYPE_PUSH;
4345
import static org.privacyidea.PIConstants.TOKEN_TYPE_WEBAUTHN;
4446
import static org.privacyidea.PIConstants.TRANSACTION_ID;
4547
import static org.privacyidea.PIConstants.TYPE;
@@ -193,6 +195,45 @@ public String getMessage() {
193195
return message;
194196
}
195197

198+
public boolean isPushAvailable() {
199+
return multichallenge.stream().anyMatch(c -> TOKEN_TYPE_PUSH.equals(c.getType()));
200+
}
201+
202+
/**
203+
* Get the messages of all triggered push challenges reduced to a string to show on the push UI.
204+
*
205+
* @return messages of all push challenges combined
206+
*/
207+
public String getPushMessage() {
208+
return reduceChallengeMessagesWhere(c -> TOKEN_TYPE_PUSH.equals(c.getType()));
209+
}
210+
211+
/**
212+
* Get the messages of all token that require an input field (HOTP, TOTP, SMS, Email...) reduced to a single string
213+
* to show with the input field.
214+
*
215+
* @return message string
216+
*/
217+
public String getOTPMessage() {
218+
// Any challenge that is not WebAuthn or Push is considered OTP
219+
return reduceChallengeMessagesWhere(c -> !(TOKEN_TYPE_WEBAUTHN.equals(c.getType())) && !(TOKEN_TYPE_PUSH.equals(c.getType())));
220+
}
221+
222+
private String reduceChallengeMessagesWhere(Predicate<Challenge> predicate) {
223+
StringBuilder sb = new StringBuilder();
224+
sb.append(multichallenge
225+
.stream()
226+
.filter(predicate)
227+
.map(Challenge::getMessage)
228+
.reduce("", (a, s) -> a + s + ", ").trim());
229+
230+
if (sb.length() > 0) {
231+
sb.deleteCharAt(sb.length() - 1);
232+
}
233+
234+
return sb.toString();
235+
}
236+
196237
/**
197238
* @return list of token types that were triggered or an empty list
198239
*/
@@ -214,6 +255,21 @@ public List<Challenge> getMultiChallenge() {
214255
return multichallenge;
215256
}
216257

258+
/**
259+
* Get all WebAuthn challenges from the multi_challenge.
260+
*
261+
* @return List of WebAuthn objects or empty list
262+
*/
263+
public List<WebAuthn> getWebAuthnSignRequests() {
264+
List<WebAuthn> ret = new ArrayList<>();
265+
multichallenge.stream().filter(c -> TOKEN_TYPE_WEBAUTHN.equals(c.getType())).collect(Collectors.toList()).forEach(c -> {
266+
if (c instanceof WebAuthn) {
267+
ret.add((WebAuthn) c);
268+
}
269+
});
270+
return ret;
271+
}
272+
217273
/**
218274
* @return the transaction id that was triggered or an empty string if nothing was triggered
219275
*/
@@ -268,7 +324,7 @@ public String getType() {
268324
return type;
269325
}
270326

271-
public int getOTPlength() {
327+
public int getOTPLength() {
272328
return otplen;
273329
}
274330

src/main/java/org/privacyidea/WebAuthn.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020

2121
public class WebAuthn extends Challenge {
2222

23-
private final List<String> attributes = new ArrayList<>();
2423
private final String signRequest;
2524

2625
public WebAuthn(String serial, String message, String transaction_id, String signRequest) {
2726
super(serial, message, transaction_id, PIConstants.TOKEN_TYPE_WEBAUTHN);
2827
this.signRequest = signRequest;
2928
}
3029

31-
public List<String> getAttributes() {
32-
return attributes;
33-
}
34-
35-
30+
/**
31+
* Returns the WebAuthnSignRequest in JSON format as a string, ready to use with pi-webauthn.js.
32+
* If this returns an empty string, it *might* indicate that the PIN of this token should be changed.
33+
*
34+
* @return sign request or empty string
35+
*/
3636
public String getSignRequest() {
3737
return signRequest;
3838
}

src/test/java/org/privacyidea/TestOTP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testOTPSuccess() {
7676
// Assert everything
7777
assertEquals("1", response.getID());
7878
assertEquals("matching 1 tokens", response.getMessage());
79-
assertEquals(6, response.getOTPlength());
79+
assertEquals(6, response.getOTPLength());
8080
assertEquals("PISP0001C673", response.getSerial());
8181
//assertEquals("140536383567616", response.getThreadID());
8282
//assertEquals("1589276995.4397042", response.getTime());

0 commit comments

Comments
 (0)