2424import com .google .gson .JsonSyntaxException ;
2525import java .util .ArrayList ;
2626import java .util .List ;
27+ import java .util .function .Predicate ;
2728import java .util .stream .Collectors ;
2829
2930import static org .privacyidea .PIConstants .ATTRIBUTES ;
4041import static org .privacyidea .PIConstants .SERIAL ;
4142import static org .privacyidea .PIConstants .SIGNATURE ;
4243import static org .privacyidea .PIConstants .STATUS ;
44+ import static org .privacyidea .PIConstants .TOKEN_TYPE_PUSH ;
4345import static org .privacyidea .PIConstants .TOKEN_TYPE_WEBAUTHN ;
4446import static org .privacyidea .PIConstants .TRANSACTION_ID ;
4547import 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
0 commit comments