use of org.candlepin.auth.Principal in project candlepin by candlepin.
the class AuthenticationFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
log.debug("Authentication check for {}", requestContext.getUriInfo().getPath());
HttpRequest httpRequest = ResteasyProviderFactory.getContextData(HttpRequest.class);
ResourceInfo resourceInfo = ResteasyProviderFactory.getContextData(ResourceInfo.class);
Method method = resourceInfo.getResourceMethod();
SecurityHole hole = method.getAnnotation(SecurityHole.class);
Principal principal = null;
if (hole != null && hole.anon()) {
principal = new NoAuthPrincipal();
} else if (resourceInfo.getResourceClass().equals(ApiListingResource.class)) {
log.debug("Swagger API request made; no principal required.");
principal = new NoAuthPrincipal();
} else {
for (AuthProvider provider : providers) {
principal = provider.getPrincipal(httpRequest);
if (principal != null) {
log.debug("Establishing principal with {}", provider.getClass().getName());
break;
}
}
}
/* At this point, there is no provider that has given a valid principal,
* so we use the NoAuthPrincipal here if it is allowed. */
if (principal == null) {
if (hole != null && hole.noAuth()) {
log.debug("No auth allowed for resource; setting NoAuth principal");
principal = new NoAuthPrincipal();
} else if (!config.getBoolean(ConfigProperties.AUTH_OVER_HTTP) && !request.isSecure()) {
throw new BadRequestException("Please use SSL when accessing protected resources");
} else {
throw new NotAuthorizedException("Invalid credentials.");
}
}
SecurityContext securityContext = new CandlepinSecurityContext(principal);
requestContext.setSecurityContext(securityContext);
// Push the principal into the context for the PrincipalProvider to access directly
ResteasyProviderFactory.pushContext(Principal.class, principal);
}
use of org.candlepin.auth.Principal in project candlepin by candlepin.
the class ConsumerCheckInFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
ResourceInfo resourceInfo = ResteasyProviderFactory.getContextData(ResourceInfo.class);
Method method = resourceInfo.getResourceMethod();
Principal principal = ResteasyProviderFactory.getContextData(Principal.class);
if (principal instanceof ConsumerPrincipal && method.getAnnotation(UpdateConsumerCheckIn.class) != null) {
ConsumerPrincipal p = (ConsumerPrincipal) principal;
consumerCurator.updateLastCheckin(p.getConsumer());
}
}
use of org.candlepin.auth.Principal in project candlepin by candlepin.
the class DatabaseListener method onEvent.
@Override
public void onEvent(Event event) {
// We're outside of a web request here, need to create this event and satisfy the
// access control interceptor.
Principal systemPrincipal = new SystemPrincipal();
ResteasyProviderFactory.pushContext(Principal.class, systemPrincipal);
if (log.isDebugEnabled()) {
log.debug("Received event: " + event);
}
if (event != null) {
eventCurator.create(event);
}
}
use of org.candlepin.auth.Principal in project candlepin by candlepin.
the class PinsetterAsyncFilterTest method existingJobMapPrincipal.
@Test
public void existingJobMapPrincipal() {
List<Permission> permissions = Arrays.asList(new Permission[] { new OwnerPermission(new Owner("test_owner"), Access.ALL) });
Principal principal = new UserPrincipal("testing", permissions, false);
when(this.principalProvider.get()).thenReturn(principal);
JobDataMap map = new JobDataMap();
map.put("Temp", "something");
JobDetail detail = newJob(RefreshPoolsJob.class).usingJobData(map).build();
when(response.getEntity()).thenReturn(detail);
this.interceptor.postProcess(response);
Assert.assertSame(principal, detail.getJobDataMap().get(PinsetterJobListener.PRINCIPAL_KEY));
}
use of org.candlepin.auth.Principal in project candlepin by candlepin.
the class AuthenticationFilterTest method securityHoleWithNoAuth.
@Test
public void securityHoleWithNoAuth() throws Exception {
Method method = FakeResource.class.getMethod("noAuthMethod", String.class);
mockResourceMethod(method);
interceptor.filter(getContext());
Principal p = ResteasyProviderFactory.getContextData(Principal.class);
assertTrue(p instanceof NoAuthPrincipal);
}
Aggregations