Commit dc963bfc authored by Kelly Domico's avatar Kelly Domico
Browse files

Added tests for AuthUtils

parent 53fa1378
......@@ -34,6 +34,8 @@ import java.util.Map;
public class Slf4jMDCFilter implements Filter {
@Autowired
private DpsHeaders dpsHeaders;
@Autowired
private AuthUtils authUtils;
/**
* Filter logic.
......@@ -76,7 +78,7 @@ public class Slf4jMDCFilter implements Filter {
* @return the user ID
*/
private String getUserId() {
JWTClaimsSet claimsSet = AuthUtils.getClaimsFromJwtToken(dpsHeaders.getAuthorization());
JWTClaimsSet claimsSet = authUtils.getClaimsFromJwtToken(dpsHeaders.getAuthorization());
return claimsSet == null ? null : claimsSet.getSubject();
}
}
package org.opengroup.osdu.azure.util;
import com.nimbusds.jwt.JWTClaimsSet;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.util.Base64;
......@@ -8,20 +9,15 @@ import java.util.Base64;
/**
* Utils for auth.
*/
@Component
public final class AuthUtils {
/**
* Private constructor.
*/
private AuthUtils() {
}
/**
* Get claims set from JWT token.
*
* @param jwtToken the JWT token string
* @return the JWTClaimsSet object
*/
public static JWTClaimsSet getClaimsFromJwtToken(final String jwtToken) {
public JWTClaimsSet getClaimsFromJwtToken(final String jwtToken) {
try {
String[] jwtTokenParts = jwtToken.split("\\.");
Base64.Decoder decoder = Base64.getDecoder();
......@@ -38,7 +34,7 @@ public final class AuthUtils {
* @param jwtToken the JWT token string
* @return the OID
*/
public static String getOidFromJwtToken(final String jwtToken) {
public String getOidFromJwtToken(final String jwtToken) {
try {
JWTClaimsSet claimsSet = getClaimsFromJwtToken(jwtToken);
return claimsSet == null ? null : claimsSet.getStringClaim("oid");
......
package org.opengroup.osdu.azure.util;
import com.nimbusds.jwt.JWTClaimsSet;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(MockitoExtension.class)
public class AuthUtilsTest {
// Mock JWT Token: {"aud": "audience","appid": "application-id","oid": "my-oid","sub": "my-sub","tid": "my-tenant"}
private static final String jwtToken = "part1.eyJhdWQiOiJhdWRpZW5jZSIsImFwcGlkIjoiYXBwbGljYXRpb24taWQiLCJvaWQiOiJteS1vaWQiLCJzdWIiOiJteS1zdWIiLCJ0aWQiOiJteS10ZW5hbnQifQ.part3";
private static final String invalidJwtToken = "invalidjwttoken";
private static final String invalidJwtTokenClaims = "part1.invalidclaims.part2";
private static final String emptyJwtTokenClaims = "part1.e30.part3";
@InjectMocks
private AuthUtils authUtils;
@Test
public void ShouldSuccessfullyGetClaims() throws Exception {
JWTClaimsSet claims = authUtils.getClaimsFromJwtToken(jwtToken);
assertEquals(5, claims.getClaims().size());
assertEquals(1, claims.getAudience().size());
assertEquals("audience", claims.getAudience().get(0));
assertEquals("my-sub", claims.getSubject());
assertEquals("application-id", claims.getStringClaim("appid"));
assertEquals("my-oid", claims.getStringClaim("oid"));
assertEquals("my-tenant", claims.getStringClaim("tid"));
}
@Test
public void ShouldReturnNullForInvalidToken() throws Exception {
JWTClaimsSet claims = authUtils.getClaimsFromJwtToken(invalidJwtToken);
assertEquals(null, claims);
}
@Test
public void ShouldReturnNullForInvalidTokenClaims() throws Exception {
JWTClaimsSet claims = authUtils.getClaimsFromJwtToken(invalidJwtTokenClaims);
assertEquals(null, claims);
}
@Test
public void ShouldReturnEmptySetForEmptyTokenClaims() throws Exception {
JWTClaimsSet claims = authUtils.getClaimsFromJwtToken(emptyJwtTokenClaims);
assertEquals(0, claims.getClaims().size());
}
@Test
public void ShouldReturnOidForValidTokenWithOid() throws Exception {
String oid = authUtils.getOidFromJwtToken(jwtToken);
assertEquals("my-oid", oid);
}
@Test
public void ShouldReturnNullOidForInvalidToken() throws Exception {
String oid = authUtils.getOidFromJwtToken(invalidJwtToken);
assertEquals(null, oid);
}
@Test
public void ShouldReturnNullOidForEmptyToken() throws Exception {
String oid = authUtils.getOidFromJwtToken(emptyJwtTokenClaims);
assertEquals(null, oid);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment