@@ -305,18 +305,34 @@ object MappedConsentProvider extends ConsentProvider with code.util.Helper.MdcLo
305305 MappedConsent .find(By (MappedConsent .mConsentId, consentId)) match {
306306 case Full (consent) =>
307307 val payload = JwtUtil .getSignedPayloadAsJson(jwt).openOr(null )
308+ // Parse JWT payload to denormalise exp and consent items
309+ val consentJWTParsed : Option [ConsentJWT ] = if (payload != null ) {
310+ try {
311+ import net .liftweb .json ._
312+ implicit val formats : DefaultFormats .type = DefaultFormats
313+ Some (parse(payload).extract[ConsentJWT ])
314+ } catch {
315+ case e : Exception =>
316+ logger.error(s " setJsonWebToken says: Failed to parse JWT payload for consent $consentId: ${e.getMessage}" )
317+ None
318+ }
319+ } else None
320+
321+ // Set jwt_expires_at from the JWT exp claim
322+ consentJWTParsed.foreach { jwt =>
323+ consent.mJwtExpiresAt(new Date (jwt.exp * 1000L ))
324+ }
325+
308326 val result = tryo(consent
309327 .mJsonWebToken(jwt)
310328 .mJsonWebTokenPayload(payload)
311329 .saveMe())
330+
312331 // Denormalise bank_id, account_id, view_id and role_name from the JWT into consent_items
313332 // so that bank-scoped queries can use an indexed SQL join instead of extracting every JWT.
314333 result.foreach { savedConsent =>
315334 try {
316- if (payload != null ) {
317- import net .liftweb .json ._
318- implicit val formats : DefaultFormats .type = DefaultFormats
319- val consentJWT = parse(payload).extract[ConsentJWT ]
335+ consentJWTParsed.foreach { consentJWT =>
320336 DoobieConsentQueries .insertConsentItems(savedConsent.id.get, consentJWT)
321337 }
322338 } catch {
@@ -455,6 +471,10 @@ class MappedConsent extends ConsentTrait with LongKeyedMapper[MappedConsent] wit
455471 object mStatusUpdateDateTime extends MappedDateTime (this )
456472 object mNote extends MappedText (this )
457473 object mJsonWebTokenPayload extends MappedText (this )
474+ // Denormalised from the JWT exp claim so we can query expiry without parsing the JWT.
475+ object mJwtExpiresAt extends MappedDateTime (this ) {
476+ override def dbColumnName = " jwt_expires_at"
477+ }
458478
459479 override def consentId : String = mConsentId.get
460480 override def userId : String = mUserId.get
0 commit comments