-
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.
refactor auth service by introducing jwt helper class. return user id…
… for isauthorisedUserAccess function
- Loading branch information
1 parent
080149c
commit 3337b5e
Showing
6 changed files
with
96 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.quemistry.auth_ms.Util; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
|
||
public class JwtHelper { | ||
private static final String EMAIL = "email"; | ||
private static final String USERID = "sub"; | ||
private static final String USER_GROUP ="cognito:groups"; | ||
|
||
private final Map<String, Object> KeyValue; | ||
public JwtHelper(String jwtString){ | ||
this.KeyValue = JwtKeyValue(jwtString); | ||
} | ||
|
||
public Boolean getValid(){ | ||
return this.KeyValue != null; | ||
} | ||
|
||
public String getEmail(){ | ||
return (String)this.KeyValue.get(JwtHelper.EMAIL); | ||
} | ||
|
||
public String getUserId(){ | ||
return (String)this.KeyValue.get(JwtHelper.USERID); | ||
} | ||
|
||
public String[] getUserGroup(){ | ||
var useRoles = (ArrayList<Object>)this.KeyValue.get(JwtHelper.USER_GROUP); | ||
if(useRoles != null) { | ||
return useRoles.toArray(new String[useRoles.size()]); | ||
} | ||
return new String[0]; | ||
} | ||
private static Map<String, Object> JwtKeyValue(String jwtString) { | ||
String[] chunks = (jwtString == null) ? null : jwtString.split("\\."); | ||
if (chunks == null || chunks.length < 3) { | ||
return null; | ||
} | ||
try { | ||
Base64.Decoder decoder = Base64.getUrlDecoder(); | ||
String payload = new String(decoder.decode(chunks[1])); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
return mapper.readValue(payload, new TypeReference<>() {}); | ||
} catch (Exception ex) { | ||
return null; | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -51,11 +51,10 @@ public class AuthenticationServiceImplTest { | |
@InjectMocks | ||
private AuthenticationServiceImpl authenticationService; | ||
|
||
|
||
private TokenResponse tokenResponse; | ||
private UserProfile user; | ||
|
||
private String idToken; | ||
private final String UserId = "c9aad54c-60e1-7045-e712-9ad1da73f87a"; | ||
@BeforeEach | ||
void init() throws NoSuchFieldException, IllegalAccessException { | ||
|
||
|
@@ -65,11 +64,12 @@ void init() throws NoSuchFieldException, IllegalAccessException { | |
user.setSessionId(UUID.randomUUID().toString()); | ||
|
||
//idtoken with email set as [email protected] | ||
idToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdF9oYXNoIjoidThRdnNKdFY1TXRETFdodF9xQmFpZyIsInN1YiI6ImM5YWFkNTRjLTYwZTEtNzA0NS1lNzEyLTlhZDFkYTczZjg3YSIsImNvZ25pdG86Z3JvdXBzIjpbInR1dG9yIiwiYWRtaW4iXSwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJpc3MiOiJodHRwczovL2NvZ25pdG8taWRwLmFwLXNvdXRoZWFzdC0xLmFt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QwMjk0OTE2Njc5NjE1ODg4NDAiLCJvcmlnaW5fanRpIjoiYTY1M2ExMDEtNTUyMi00ZDIyLTk1NzctZGZkZjA4ZDM5NDc4IiwiYXVkIjoiMXEzMHZtZDB2Y2VlNmsxbHJwMmluMTA2MjMiLCJpZGVudGl0aWVzIjpbeyJkYXRlQ3JlYXRlZCI6IjE3MTcyNTI0Mzk3MDMiLCJ1c2VySWQiOiIxMDQwMjk0OTE2Njc5NjE1ODg4NDAiLCJwcm92aWRlck5hbWUiOiJHb29nbGUiLCJwcm92aWRlclR5cGUiOiJHb29nbGUiLCJpc3N1ZXIiOm51bGwsInByaW1hcnkiOiJ0cnVlIn1dLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTcyMTE0MDE3NywiZXhwIjoxNzIxMTQxMDc3LCJpYXQiOjE3MjExNDAxNzcsImp0aSI6IjU5YjlkZmZlLWFkMjItNDMyZC05ZWIxLTRiZmVhYjFhOGY4MyIsImVtYWlsIjoidGVzdFVzZXJAZW1haWwuY29tIn0.HabZEsulPCsu-IYRE_G42RUWo0k5jMJqYSxJx_QgtuY"; | ||
|
||
tokenResponse = new TokenResponse(); | ||
tokenResponse.setIdToken(idToken); | ||
tokenResponse.setAccessToken("testAccessToken"); | ||
tokenResponse.setAccessToken("testRefreshToken"); | ||
tokenResponse.setAccessToken(idToken); | ||
tokenResponse.setRefreshToken("testRefreshToken"); | ||
tokenResponse.setExpiresIn(120); | ||
} | ||
|
||
|
@@ -101,10 +101,9 @@ void givenGetAccessToken_Success(){ | |
|
||
Mockito.when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
|
||
var result = authenticationService.getAccessToken(tokenRequest); | ||
tokenResponse.setEmail("[email protected]"); | ||
var result = authenticationService.getAccessToken(tokenRequest); | ||
|
||
Assertions.assertEquals(user.getEmail(), result.getEmail() ); | ||
Assertions.assertEquals(user.getEmail(), result.getEmail() ); | ||
Assertions.assertEquals(user.getRoles().length, result.getRoles().length ); | ||
|
||
} | ||
|
@@ -182,11 +181,13 @@ void givencheckUserSessionAccess_Success(){ | |
Mockito.when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_profile")) | ||
.thenReturn(user); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_tokens")) | ||
.thenReturn(tokenResponse); | ||
|
||
Mockito.when(roleRepository.findByNames(user.getRoles())).thenReturn(roles); | ||
var result = authenticationService.checkUserSessionAccess(user.getSessionId(), "/questions", "GET"); | ||
|
||
Assertions.assertEquals(result, true); | ||
Assertions.assertEquals(result, UserId); | ||
} | ||
|
||
@Test | ||
|
@@ -207,11 +208,13 @@ void givencheckUserSessionAccess_InValidSession(){ | |
Mockito.when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_profile")) | ||
.thenReturn(null); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_tokens")) | ||
.thenReturn(null); | ||
|
||
Mockito.when(roleRepository.findByNames(user.getRoles())).thenReturn(roles); | ||
var result = authenticationService.checkUserSessionAccess(user.getSessionId(), "/questions", "GET"); | ||
|
||
Assertions.assertEquals(result, false); | ||
Assertions.assertEquals(result, ""); | ||
} | ||
|
||
@Test | ||
|
@@ -231,11 +234,13 @@ void givencheckUserSessionAccess_NoAccess(){ | |
|
||
Mockito.when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_profile")) | ||
.thenReturn(null); | ||
.thenReturn(user); | ||
Mockito.when(redisTemplate.opsForValue().get(user.getSessionId()+"_tokens")) | ||
.thenReturn(tokenResponse); | ||
|
||
Mockito.when(roleRepository.findByNames(user.getRoles())).thenReturn(roles); | ||
var result = authenticationService.checkUserSessionAccess(user.getSessionId(), "/questions", "POST"); | ||
|
||
Assertions.assertEquals(result, false); | ||
Assertions.assertEquals(result, ""); | ||
} | ||
} |