-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance JWT verification logs with detailed claim data (#207)
* Enhance JWT verification logs with detailed claim data Added detailed claim data to logs on JWT expiration and not-before time checks. This improves the ability to diagnose issues by providing comprehensive context in error messages. * Add Mockito dependencies and JWTClaimsSetVerifierWithLogsTest This commit adds Mockito dependencies to the pom.xml file to facilitate mocking in unit tests. It also introduces the JWTClaimsSetVerifierWithLogsTest class to test JWT claim set verification, ensuring proper handling of expired and not-before JWT conditions. --------- Co-authored-by: marcelmeyer <[email protected]>
- Loading branch information
1 parent
feb9bb1
commit 66f0543
Showing
3 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
sts-token-auth/src/test/java/de/adorsys/sts/tokenauth/JWTClaimsSetVerifierWithLogsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.adorsys.sts.tokenauth; | ||
|
||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.proc.BadJWTException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class JWTClaimsSetVerifierWithLogsTest { | ||
|
||
@org.junit.jupiter.api.Test | ||
void verify() { | ||
JWTClaimsSetVerifierWithLogs jwtClaimsSetVerifierWithLogs = new JWTClaimsSetVerifierWithLogs(null); | ||
assertThrows(NullPointerException.class, () -> jwtClaimsSetVerifierWithLogs.verify(null, null)); | ||
} | ||
|
||
@Mock | ||
private Clock clock; | ||
|
||
private JWTClaimsSetVerifierWithLogs underTest; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
underTest = new JWTClaimsSetVerifierWithLogs(clock); | ||
} | ||
|
||
@Test | ||
public void testVerify_throwsBadJWTException_whenJWTIsExpired() { | ||
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().expirationTime(new Date(System.currentTimeMillis() - 60000)).build(); | ||
when(clock.instant()).thenReturn(Instant.now()); | ||
assertThrows(BadJWTException.class, () -> { | ||
underTest.verify(claimsSet, null); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testVerify_throwsBadJWTException_whenJWTIsNotBeforeNow() { | ||
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().notBeforeTime(new Date(System.currentTimeMillis() + 60000)).build(); | ||
when(clock.instant()).thenReturn(Instant.now()); | ||
assertThrows(BadJWTException.class, () -> { | ||
underTest.verify(claimsSet, null); | ||
}); | ||
} | ||
|
||
} |