forked from thingsboard/thingsboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request thingsboard#10884 from thingsboard/fix/token-factory
Fix token sessionId calculation
- Loading branch information
Showing
6 changed files
with
64 additions
and
64 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,16 +73,7 @@ public void beforeEach() { | |
|
||
@Test | ||
public void testCreateAndParseAccessJwtToken() { | ||
SecurityUser securityUser = new SecurityUser(); | ||
securityUser.setId(new UserId(UUID.randomUUID())); | ||
securityUser.setEmail("[email protected]"); | ||
securityUser.setAuthority(Authority.TENANT_ADMIN); | ||
securityUser.setTenantId(new TenantId(UUID.randomUUID())); | ||
securityUser.setEnabled(true); | ||
securityUser.setFirstName("A"); | ||
securityUser.setLastName("B"); | ||
securityUser.setUserPrincipal(new UserPrincipal(UserPrincipal.Type.USER_NAME, securityUser.getEmail())); | ||
securityUser.setCustomerId(new CustomerId(UUID.randomUUID())); | ||
SecurityUser securityUser = createSecurityUser(); | ||
|
||
testCreateAndParseAccessJwtToken(securityUser); | ||
|
||
|
@@ -111,18 +102,12 @@ public void testCreateAndParseAccessJwtToken(SecurityUser securityUser) { | |
assertThat(parsedSecurityUser.getCustomerId()).isEqualTo(securityUser.getCustomerId()); | ||
assertThat(parsedSecurityUser.getFirstName()).isEqualTo(securityUser.getFirstName()); | ||
assertThat(parsedSecurityUser.getLastName()).isEqualTo(securityUser.getLastName()); | ||
assertThat(parsedSecurityUser.getSessionId()).isNotNull().isEqualTo(securityUser.getSessionId()); | ||
} | ||
|
||
@Test | ||
public void testCreateAndParseRefreshJwtToken() { | ||
SecurityUser securityUser = new SecurityUser(); | ||
securityUser.setId(new UserId(UUID.randomUUID())); | ||
securityUser.setEmail("[email protected]"); | ||
securityUser.setAuthority(Authority.TENANT_ADMIN); | ||
securityUser.setUserPrincipal(new UserPrincipal(UserPrincipal.Type.USER_NAME, securityUser.getEmail())); | ||
securityUser.setEnabled(true); | ||
securityUser.setTenantId(new TenantId(UUID.randomUUID())); | ||
securityUser.setCustomerId(new CustomerId(UUID.randomUUID())); | ||
SecurityUser securityUser = createSecurityUser(); | ||
|
||
JwtToken refreshToken = tokenFactory.createRefreshToken(securityUser); | ||
checkExpirationTime(refreshToken, jwtSettings.getRefreshTokenExpTime()); | ||
|
@@ -138,15 +123,7 @@ public void testCreateAndParseRefreshJwtToken() { | |
|
||
@Test | ||
public void testCreateAndParsePreVerificationJwtToken() { | ||
SecurityUser securityUser = new SecurityUser(); | ||
securityUser.setId(new UserId(UUID.randomUUID())); | ||
securityUser.setEmail("[email protected]"); | ||
securityUser.setAuthority(Authority.TENANT_ADMIN); | ||
securityUser.setUserPrincipal(new UserPrincipal(UserPrincipal.Type.USER_NAME, securityUser.getEmail())); | ||
securityUser.setEnabled(true); | ||
securityUser.setTenantId(new TenantId(UUID.randomUUID())); | ||
securityUser.setCustomerId(new CustomerId(UUID.randomUUID())); | ||
|
||
SecurityUser securityUser = createSecurityUser(); | ||
int tokenLifetime = (int) TimeUnit.MINUTES.toSeconds(30); | ||
JwtToken preVerificationToken = tokenFactory.createPreVerificationToken(securityUser, tokenLifetime); | ||
checkExpirationTime(preVerificationToken, tokenLifetime); | ||
|
@@ -162,6 +139,34 @@ public void testCreateAndParsePreVerificationJwtToken() { | |
}); | ||
} | ||
|
||
@Test | ||
public void testSessionId() { | ||
SecurityUser securityUser = createSecurityUser(); | ||
String sessionId = securityUser.getSessionId(); | ||
|
||
String accessToken = tokenFactory.createAccessJwtToken(securityUser).getToken(); | ||
securityUser = tokenFactory.parseAccessJwtToken(accessToken); | ||
assertThat(securityUser.getSessionId()).isNotNull().isEqualTo(sessionId); | ||
|
||
String newAccessToken = tokenFactory.createTokenPair(securityUser).getToken(); | ||
securityUser = tokenFactory.parseAccessJwtToken(newAccessToken); | ||
assertThat(securityUser.getSessionId()).isNotNull().isNotEqualTo(sessionId); | ||
} | ||
|
||
private SecurityUser createSecurityUser() { | ||
SecurityUser securityUser = new SecurityUser(); | ||
securityUser.setId(new UserId(UUID.randomUUID())); | ||
securityUser.setEmail("[email protected]"); | ||
securityUser.setAuthority(Authority.TENANT_ADMIN); | ||
securityUser.setTenantId(new TenantId(UUID.randomUUID())); | ||
securityUser.setEnabled(true); | ||
securityUser.setFirstName("A"); | ||
securityUser.setLastName("B"); | ||
securityUser.setUserPrincipal(new UserPrincipal(UserPrincipal.Type.USER_NAME, securityUser.getEmail())); | ||
securityUser.setCustomerId(new CustomerId(UUID.randomUUID())); | ||
return securityUser; | ||
} | ||
|
||
private void mockJwtSettings(JwtSettings settings) { | ||
AdminSettings adminJwtSettings = new AdminSettings(); | ||
adminJwtSettings.setJsonValue(JacksonUtil.valueToTree(settings)); | ||
|
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