1616 */
1717package org .privacyidea ;
1818
19+ import com .google .gson .Gson ;
20+ import com .google .gson .GsonBuilder ;
1921import com .google .gson .JsonSyntaxException ;
2022import java .util .ArrayList ;
2123import java .util .List ;
@@ -55,6 +57,9 @@ public class PIResponse
5557 public String username = "" ;
5658 public String enrollmentLink = "" ;
5759
60+ public String webAuthnSignRequest = "" ;
61+ public String webAuthnTransactionId = "" ;
62+
5863 public boolean authenticationSuccessful ()
5964 {
6065 if (authentication == AuthenticationStatus .ACCEPT && (multiChallenge == null || multiChallenge .isEmpty ()))
@@ -87,6 +92,29 @@ public String pushMessage()
8792 return reduceChallengeMessagesWhere (c -> TOKEN_TYPE_PUSH .equals (c .getType ()));
8893 }
8994
95+ public String otpTransactionId ()
96+ {
97+ for (Challenge challenge : multiChallenge )
98+ {
99+ if (!TOKEN_TYPE_PUSH .equals (challenge .getType ()) && !TOKEN_TYPE_WEBAUTHN .equals (challenge .getType ()))
100+ {
101+ return challenge .transactionID ;
102+ }
103+ }
104+ return null ;
105+ }
106+
107+ public String pushTransactionId () {
108+ for (Challenge challenge : multiChallenge )
109+ {
110+ if (TOKEN_TYPE_PUSH .equals (challenge .getType ()))
111+ {
112+ return challenge .transactionID ;
113+ }
114+ }
115+ return null ;
116+ }
117+
90118 /**
91119 * Get the messages of all token that require an input field (HOTP, TOTP, SMS, Email...) reduced to a single string.
92120 *
@@ -115,25 +143,12 @@ private String reduceChallengeMessagesWhere(Predicate<Challenge> predicate)
115143 */
116144 public List <String > triggeredTokenTypes ()
117145 {
118- return multiChallenge .stream ().map (Challenge ::getType ).distinct ().collect (Collectors .toList ());
119- }
120-
121- /**
122- * Get all WebAuthn challenges from the multi_challenge.
123- *
124- * @return List of WebAuthn objects or empty list
125- */
126- public List <WebAuthn > webAuthnSignRequests ()
127- {
128- List <WebAuthn > ret = new ArrayList <>();
129- multiChallenge .stream ().filter (c -> TOKEN_TYPE_WEBAUTHN .equals (c .getType ())).collect (Collectors .toList ()).forEach (c ->
130- {
131- if (c instanceof WebAuthn )
132- {
133- ret .add ((WebAuthn ) c );
134- }
135- });
136- return ret ;
146+ List <String > types = multiChallenge .stream ().map (Challenge ::getType ).distinct ().collect (Collectors .toList ());
147+ if (this .webAuthnSignRequest != null && !this .webAuthnSignRequest .isEmpty ())
148+ {
149+ types .add (TOKEN_TYPE_WEBAUTHN );
150+ }
151+ return types ;
137152 }
138153
139154 /**
@@ -146,27 +161,24 @@ public List<WebAuthn> webAuthnSignRequests()
146161 */
147162 public String mergedSignRequest ()
148163 {
149- List <WebAuthn > webauthnSignRequests = webAuthnSignRequests ();
150- if (webauthnSignRequests .isEmpty ())
164+ if (this .webAuthnSignRequest == null || this .webAuthnSignRequest .isEmpty ())
151165 {
152166 return "" ;
153167 }
154- if (webauthnSignRequests .size () == 1 )
155- {
156- return webauthnSignRequests .get (0 ).signRequest ();
157- }
168+ return this .webAuthnSignRequest ;
169+ }
158170
159- WebAuthn webauthn = webauthnSignRequests .get (0 );
160- List <String > stringSignRequests = webauthnSignRequests .stream ().map (WebAuthn ::signRequest ).collect (Collectors .toList ());
171+ public String toJSON ()
172+ {
173+ GsonBuilder builder = new GsonBuilder ();
174+ builder .setPrettyPrinting ();
175+ Gson gson = builder .create ();
176+ return gson .toJson (this );
177+ }
161178
162- try
163- {
164- return JSONParser .mergeWebAuthnSignRequest (webauthn , stringSignRequests );
165- }
166- catch (JsonSyntaxException e )
167- {
168- return "" ;
169- }
179+ public static PIResponse fromJSON (String json )
180+ {
181+ return new Gson ().fromJson (json , PIResponse .class );
170182 }
171183
172184 @ Override
0 commit comments