18
18
19
19
import com .google .gson .*;
20
20
21
- import java .util .ArrayList ;
22
- import java .util .LinkedHashMap ;
23
- import java .util .List ;
24
- import java .util .Map ;
21
+ import java .util .*;
25
22
26
23
import static org .privacyidea .PIConstants .*;
27
24
@@ -66,9 +63,9 @@ public String formatJson(String json)
66
63
* Extract the auth token from the response of the server.
67
64
*
68
65
* @param serverResponse response of the server
69
- * @return the auth token or null if error
66
+ * @return the AuthToken obj or null if error
70
67
*/
71
- String extractAuthToken (String serverResponse )
68
+ LinkedHashMap < String , String > extractAuthToken (String serverResponse )
72
69
{
73
70
if (serverResponse != null && !serverResponse .isEmpty ())
74
71
{
@@ -78,11 +75,21 @@ String extractAuthToken(String serverResponse)
78
75
try
79
76
{
80
77
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 )));
82
89
}
83
90
catch (Exception e )
84
91
{
85
- privacyIDEA .error ("Response did not contain an authorization token : " + formatJson ( serverResponse ) );
92
+ privacyIDEA .error ("Auth token extraction failed : " + e );
86
93
}
87
94
}
88
95
}
@@ -232,7 +239,7 @@ else if ("interactive".equals(modeFromResponse))
232
239
233
240
if (TOKEN_TYPE_WEBAUTHN .equals (type ))
234
241
{
235
- String webauthnSignRequest = getItemFromAttributes (WEBAUTHN_SIGN_REQUEST , challenge );
242
+ String webauthnSignRequest = getItemFromAttributes (challenge );
236
243
response .multiChallenge .add (new WebAuthn (serial , message , clientMode , image , transactionID , webauthnSignRequest ));
237
244
}
238
245
else
@@ -263,13 +270,13 @@ static String mergeWebAuthnSignRequest(WebAuthn webauthn, List<String> arr) thro
263
270
return signRequest .toString ();
264
271
}
265
272
266
- private String getItemFromAttributes (String item , JsonObject jsonObject )
273
+ private String getItemFromAttributes (JsonObject jsonObject )
267
274
{
268
275
String ret = "" ;
269
276
JsonElement attributeElement = jsonObject .get (ATTRIBUTES );
270
277
if (attributeElement != null && !attributeElement .isJsonNull ())
271
278
{
272
- JsonElement requestElement = attributeElement .getAsJsonObject ().get (item );
279
+ JsonElement requestElement = attributeElement .getAsJsonObject ().get (PIConstants . WEBAUTHN_SIGN_REQUEST );
273
280
if (requestElement != null && !requestElement .isJsonNull ())
274
281
{
275
282
ret = requestElement .toString ();
0 commit comments