Search in sources :

Example 1 with Principal

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);
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) ResourceInfo(javax.ws.rs.container.ResourceInfo) SecurityHole(org.candlepin.common.auth.SecurityHole) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) SecurityContext(javax.ws.rs.core.SecurityContext) BadRequestException(org.candlepin.common.exceptions.BadRequestException) AuthProvider(org.candlepin.auth.AuthProvider) Method(java.lang.reflect.Method) NotAuthorizedException(org.candlepin.common.exceptions.NotAuthorizedException) Principal(org.candlepin.auth.Principal) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal)

Example 2 with 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());
    }
}
Also used : ResourceInfo(javax.ws.rs.container.ResourceInfo) ConsumerPrincipal(org.candlepin.auth.ConsumerPrincipal) Method(java.lang.reflect.Method) ConsumerPrincipal(org.candlepin.auth.ConsumerPrincipal) Principal(org.candlepin.auth.Principal)

Example 3 with Principal

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);
    }
}
Also used : SystemPrincipal(org.candlepin.auth.SystemPrincipal) SystemPrincipal(org.candlepin.auth.SystemPrincipal) Principal(org.candlepin.auth.Principal)

Example 4 with Principal

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));
}
Also used : OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Owner(org.candlepin.model.Owner) JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) OwnerPermission(org.candlepin.auth.permissions.OwnerPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 5 with Principal

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);
}
Also used : NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) Method(java.lang.reflect.Method) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) Test(org.junit.Test)

Aggregations

Principal (org.candlepin.auth.Principal)74 Test (org.junit.Test)54 UserPrincipal (org.candlepin.auth.UserPrincipal)40 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)20 ConsumerPrincipal (org.candlepin.auth.ConsumerPrincipal)17 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)15 Consumer (org.candlepin.model.Consumer)15 Owner (org.candlepin.model.Owner)15 TrustedUserPrincipal (org.candlepin.auth.TrustedUserPrincipal)14 Date (java.util.Date)12 ConsumerType (org.candlepin.model.ConsumerType)11 HashSet (java.util.HashSet)10 Pool (org.candlepin.model.Pool)10 JobDetail (org.quartz.JobDetail)10 Method (java.lang.reflect.Method)9 Permission (org.candlepin.auth.permissions.Permission)9 JobDataMap (org.quartz.JobDataMap)9 CandlepinQuery (org.candlepin.model.CandlepinQuery)8 File (java.io.File)7 FileInputStream (java.io.FileInputStream)7