Commit a215bea3 authored by Kishore Battula's avatar Kishore Battula
Browse files

Merge branch 'user-id-mdc' into 'master'

Add user ID to MDC map in Slf4jMDCFilter

See merge request !60
parents 9b62d500 b1d46379
Pipeline #21014 passed with stages
in 10 minutes and 53 seconds
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.51</version>
<version>0.0.52</version>
<name>core-lib-azure</name>
<properties>
......
package org.opengroup.osdu.azure.filters;
import com.nimbusds.jwt.JWTClaimsSet;
import org.opengroup.osdu.azure.util.AuthUtils;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -59,6 +61,22 @@ public class Slf4jMDCFilter implements Filter {
final Map<String, String> contextMap = new HashMap<>();
contextMap.put(DpsHeaders.CORRELATION_ID, dpsHeaders.getCorrelationId());
contextMap.put(DpsHeaders.DATA_PARTITION_ID, dpsHeaders.getPartitionId());
String userId = getUserId();
if (userId != null) {
contextMap.put("user-id", userId);
}
return contextMap;
}
/**
* Get user ID from Authorization payload (JWT token).
*
* @return the user ID
*/
private String getUserId() {
JWTClaimsSet claimsSet = AuthUtils.getClaimsFromJwtToken(dpsHeaders.getAuthorization());
return claimsSet == null ? null : claimsSet.getSubject();
}
}
package org.opengroup.osdu.azure.util;
import com.nimbusds.jwt.JWTClaimsSet;
import java.text.ParseException;
import java.util.Base64;
/**
* Utils for auth.
*/
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) {
try {
String[] jwtTokenParts = jwtToken.split("\\.");
Base64.Decoder decoder = Base64.getDecoder();
String infoString = new String(decoder.decode(jwtTokenParts[1]));
return JWTClaimsSet.parse(infoString);
} catch (Exception e) {
return null;
}
}
/**
* Get OID from JWT token.
*
* @param jwtToken the JWT token string
* @return the OID
*/
public static String getOidFromJwtToken(final String jwtToken) {
try {
JWTClaimsSet claimsSet = getClaimsFromJwtToken(jwtToken);
return claimsSet == null ? null : claimsSet.getStringClaim("oid");
} catch (ParseException e) {
return null;
}
}
}
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