package com.mercateo.spring.security.jwt.verifier;

import java.util.Optional;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@AllArgsConstructor
@Slf4j
public class WrappedJWTVerifier {

    public static final String WRAPPED_TOKEN_KEY = "jwt";

    private final Optional<JWTVerifier> verifier;

    public void verifyIfPresent(String tokenString) {
        if (!verifier.isPresent()) {
            return;
        }

        DecodedJWT token = JWT.decode(tokenString);

        while (true) {
            if (token.getAlgorithm() != null) {
                verifyIfPresent(token);
            }

            final Claim wrappedTokenClaim = token.getClaim(WRAPPED_TOKEN_KEY);
            if (wrappedTokenClaim.isNull()) {
                break;
            } else {
                token = JWT.decode(wrappedTokenClaim.asString());
            }
        }
    }

    private void verifyIfPresent(DecodedJWT token) {
        verifier.ifPresent(verifier -> {
            log.info("verify token");
            verifier.verify(token.getToken());
        });
    }
}

@@ -235,82 +235,28 @@
 bok.
-AllArgsConstructor;%0Aimport lombok.extern.slf4j.Slf4j;%0A%0A@AllArgsConstructor
+extern.slf4j.Slf4j;%0A
 %0A@Sl
@@ -404,16 +404,169 @@
 ifier;%0A%0A
+    public WrappedJWTVerifier(Optional%3CJWTVerifier%3E verifier) %7B%0A        this.verifier = verifier;%0A%0A        log.info(%22use verifier %7B%7D%22, verifier);%0A    %7D%0A%0A
     publ
@@ -1234,20 +1234,21 @@
     log.
-info
+debug
 (%22verify
@@ -1253,17 +1253,43 @@
 fy token
-%22
+ with id %7B%7D%22, token.getId()
 );%0A     

clean up log output