1818
1919import com .google .gson .*;
2020
21- import java .util .ArrayList ;
22- import java .util .LinkedHashMap ;
23- import java .util .List ;
24- import java .util .Map ;
21+ import java .util .*;
2522
2623import static org .privacyidea .PIConstants .*;
2724
@@ -66,9 +63,9 @@ public String formatJson(String json)
6663 * Extract the auth token from the response of the server.
6764 *
6865 * @param serverResponse response of the server
69- * @return the auth token or null if error
66+ * @return the AuthToken obj or null if error
7067 */
71- String extractAuthToken (String serverResponse )
68+ LinkedHashMap < String , String > extractAuthToken (String serverResponse )
7269 {
7370 if (serverResponse != null && !serverResponse .isEmpty ())
7471 {
@@ -78,11 +75,21 @@ String extractAuthToken(String serverResponse)
7875 try
7976 {
8077 JsonObject obj = root .getAsJsonObject ();
81- return obj .getAsJsonObject (RESULT ).getAsJsonObject (VALUE ).getAsJsonPrimitive (TOKEN ).getAsString ();
78+ String authToken = obj .getAsJsonObject (RESULT ).getAsJsonObject (VALUE ).getAsJsonPrimitive (TOKEN ).getAsString ();
79+ var parts = authToken .split ("\\ ." );
80+ String dec = new String (Base64 .getDecoder ().decode (parts [1 ]));
81+
82+ // Extract the expiration date from the token
83+ int respDate = obj .getAsJsonPrimitive (TIME ).getAsInt ();
84+ int expDate = JsonParser .parseString (dec ).getAsJsonObject ().getAsJsonPrimitive (EXP ).getAsInt ();
85+ int difference = expDate - respDate ;
86+ privacyIDEA .log ("JWT Validity: " + difference / 60 + " minutes. Token expires at: " + new Date (expDate * 1000L ));
87+
88+ return new LinkedHashMap <>(Map .of (AUTH_TOKEN , authToken , AUTH_TOKEN_EXP , String .valueOf (expDate )));
8289 }
8390 catch (Exception e )
8491 {
85- privacyIDEA .error ("Response did not contain an authorization token : " + formatJson ( serverResponse ) );
92+ privacyIDEA .error ("Auth token extraction failed : " + e );
8693 }
8794 }
8895 }
@@ -232,7 +239,7 @@ else if ("interactive".equals(modeFromResponse))
232239
233240 if (TOKEN_TYPE_WEBAUTHN .equals (type ))
234241 {
235- String webauthnSignRequest = getItemFromAttributes (WEBAUTHN_SIGN_REQUEST , challenge );
242+ String webauthnSignRequest = getItemFromAttributes (challenge );
236243 response .multiChallenge .add (new WebAuthn (serial , message , clientMode , image , transactionID , webauthnSignRequest ));
237244 }
238245 else
@@ -263,13 +270,13 @@ static String mergeWebAuthnSignRequest(WebAuthn webauthn, List<String> arr) thro
263270 return signRequest .toString ();
264271 }
265272
266- private String getItemFromAttributes (String item , JsonObject jsonObject )
273+ private String getItemFromAttributes (JsonObject jsonObject )
267274 {
268275 String ret = "" ;
269276 JsonElement attributeElement = jsonObject .get (ATTRIBUTES );
270277 if (attributeElement != null && !attributeElement .isJsonNull ())
271278 {
272- JsonElement requestElement = attributeElement .getAsJsonObject ().get (item );
279+ JsonElement requestElement = attributeElement .getAsJsonObject ().get (PIConstants . WEBAUTHN_SIGN_REQUEST );
273280 if (requestElement != null && !requestElement .isJsonNull ())
274281 {
275282 ret = requestElement .toString ();
0 commit comments