use of org.sdase.commons.server.auth.JwtPrincipal in project sda-dropwizard-commons by SDA-SE.
the class SecureEndPoint method returnClaims.
@GET
@Produces(APPLICATION_JSON)
public Response returnClaims() {
JwtPrincipal jwtPrincipal = (JwtPrincipal) securityContext.getUserPrincipal();
Map<String, Claim> claims = jwtPrincipal.getClaims();
Map<String, String> claimsAsString = claims.entrySet().stream().collect(toMap(Map.Entry::getKey, e -> e.getValue().asString()));
return Response.ok(claimsAsString).build();
}
use of org.sdase.commons.server.auth.JwtPrincipal in project sda-dropwizard-commons by SDA-SE.
the class JwtAuthFilterTest method shouldAcceptOnAnonymousPayload.
@Test
public void shouldAcceptOnAnonymousPayload() throws AuthenticationException {
// given
MultivaluedStringMap headers = new MultivaluedStringMap();
headers.add(HttpHeaders.AUTHORIZATION, "Bearer MY_JWT");
when(requestContext.getHeaders()).thenReturn(headers);
when(authenticator.authenticate(credentialsCaptor.capture())).thenReturn(Optional.of(JwtPrincipal.emptyPrincipal()));
JwtAuthFilter authFilter = new Builder<JwtPrincipal>().setAcceptAnonymous(true).setAuthenticator(authenticator).buildAuthFilter();
// when
authFilter.filter(requestContext);
// then
assertThat(credentialsCaptor.getValue()).contains("MY_JWT");
}
use of org.sdase.commons.server.auth.JwtPrincipal in project sda-dropwizard-commons by SDA-SE.
the class OpaAuthFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) {
Span span = tracer.buildSpan("authorizeUsingOpa").withTag("opa.allow", false).withTag(COMPONENT, "OpaAuthFilter").start();
try (Scope ignored = tracer.scopeManager().activate(span)) {
// collect input parameters for Opa request
UriInfo uriInfo = requestContext.getUriInfo();
String method = requestContext.getMethod();
String trace = requestContext.getHeaderString(RequestTracing.TOKEN_HEADER);
String jwt = null;
// if security context already exist and if it is a jwt security context,
// we include the jwt in the request
SecurityContext securityContext = requestContext.getSecurityContext();
Map<String, Claim> claims = null;
if (null != securityContext) {
JwtPrincipal jwtPrincipal = getJwtPrincipal(requestContext.getSecurityContext());
if (jwtPrincipal != null) {
// JWT principal found, this means that JWT has been validated by
// auth bundle
// and can be used within this bundle
jwt = jwtPrincipal.getJwt();
claims = jwtPrincipal.getClaims();
}
}
JsonNode constraints = null;
if (!isDisabled && !isExcluded(uriInfo)) {
// process the actual request to the open policy agent server
String[] path = uriInfo.getPathSegments().stream().map(PathSegment::getPath).toArray(String[]::new);
OpaInput opaInput = new OpaInput(jwt, path, method, trace);
ObjectNode objectNode = om.convertValue(opaInput, ObjectNode.class);
// append the input extensions to the input object
inputExtensions.forEach((namespace, extension) -> objectNode.set(namespace, om.valueToTree(extension.createAdditionalInputContent(requestContext))));
OpaRequest request = OpaRequest.request(objectNode);
constraints = authorizeWithOpa(request, span);
}
OpaJwtPrincipal principal = OpaJwtPrincipal.create(jwt, claims, constraints, om);
replaceSecurityContext(requestContext, securityContext, principal);
} finally {
span.finish();
}
}
use of org.sdase.commons.server.auth.JwtPrincipal in project sda-dropwizard-commons by SDA-SE.
the class JwtAuthFilterTest method shouldAcceptOnAnonymousEmpty.
@Test
public void shouldAcceptOnAnonymousEmpty() throws AuthenticationException {
// given
MultivaluedStringMap headers = new MultivaluedStringMap();
when(requestContext.getHeaders()).thenReturn(headers);
when(authenticator.authenticate(credentialsCaptor.capture())).thenReturn(Optional.empty());
JwtAuthFilter authFilter = new Builder<JwtPrincipal>().setAcceptAnonymous(true).setAuthenticator(authenticator).buildAuthFilter();
// when
authFilter.filter(requestContext);
// then
assertThat(credentialsCaptor.getValue()).isEmpty();
}
use of org.sdase.commons.server.auth.JwtPrincipal in project sda-dropwizard-commons by SDA-SE.
the class JwtAuthFilterTest method throwsOnDefaultEmpty.
@Test(expected = JwtAuthException.class)
public void throwsOnDefaultEmpty() throws AuthenticationException {
// given
MultivaluedStringMap headers = new MultivaluedStringMap();
when(requestContext.getHeaders()).thenReturn(headers);
when(authenticator.authenticate(credentialsCaptor.capture())).thenReturn(Optional.empty());
JwtAuthFilter<JwtPrincipal> authFilter = new Builder<JwtPrincipal>().setAuthenticator(authenticator).buildAuthFilter();
// when
authFilter.filter(requestContext);
}
Aggregations