Search in sources :

Example 56 with User

use of org.opencastproject.security.api.User in project opencast by opencast.

the class RemoteUserAndOrganizationFilter method doFilter.

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
 *      javax.servlet.FilterChain)
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    // Keep the original organization and user
    final Organization originalOrganization = securityService.getOrganization();
    final User originalUser = securityService.getUser();
    // Organization and user as specified by the request
    Organization requestedOrganization = originalOrganization;
    User requestedUser = originalUser;
    try {
        // See if there is an organization provided in the request
        String organizationHeader = httpRequest.getHeader(ORGANIZATION_HEADER);
        if (StringUtils.isNotBlank(organizationHeader)) {
            // Organization switching is only allowed if the request is coming in with the global admin role enabled
            if (!originalUser.hasRole(GLOBAL_ADMIN_ROLE)) {
                logger.warn("An unauthorized request is trying to switch from organization '{}' to '{}'", originalOrganization.getId(), organizationHeader);
                ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            try {
                requestedOrganization = organizationDirectory.getOrganization(organizationHeader);
                securityService.setOrganization(requestedOrganization);
                logger.trace("Switching to organization '{}' from request header {}", requestedOrganization.getId(), ORGANIZATION_HEADER);
            } catch (NotFoundException e) {
                logger.warn("Non-existing organization '{}' specified in request header {}", organizationHeader, ORGANIZATION_HEADER);
                ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
        } else {
            logger.trace("Request organization remains '{}'", originalOrganization.getId());
        }
        // See if there is a user provided in the request
        String userHeader = httpRequest.getHeader(USER_HEADER);
        if (StringUtils.isBlank(userHeader)) {
            userHeader = httpRequest.getHeader(RUN_AS_USER_HEADER);
        }
        if (StringUtils.isNotBlank(userHeader)) {
            // User switching is only allowed if the request is coming in with the global sudo role enabled
            if (!originalUser.hasRole(GLOBAL_SUDO_ROLE)) {
                logger.warn("An unauthorized request is trying to switch from user '{}' to '{}'", originalUser.getUsername(), userHeader);
                ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            if (SecurityConstants.GLOBAL_ANONYMOUS_USERNAME.equals(userHeader)) {
                requestedUser = SecurityUtil.createAnonymousUser(requestedOrganization);
                logger.trace("Request user is switched to '{}'", requestedUser.getUsername());
            } else {
                requestedUser = userDirectory.loadUser(userHeader);
                // Does the target user exist?
                if (requestedUser == null) {
                    logger.warn("Unable to switch to non-existing user '{}' as specified in request header {}", userHeader, USER_HEADER);
                    ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
                if (!originalUser.hasRole(GLOBAL_ADMIN_ROLE)) {
                    // if the original user did not have system privileges, the target user must not gain those, either.
                    for (String systemRole : GLOBAL_SYSTEM_ROLES) {
                        if (requestedUser.hasRole(systemRole)) {
                            logger.warn("An unauthorized request is trying to switch to an admin user, from '{}' to '{}'", originalUser.getUsername(), userHeader);
                            ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                            return;
                        }
                    }
                    // make sure the user does not gain organization administrator privileges
                    String organizationAdminRole = requestedOrganization.getAdminRole();
                    if (requestedUser.hasRole(organizationAdminRole)) {
                        logger.warn("An unauthorized request is trying to switch to an admin user, from '{}' to '{}'", originalUser.getUsername(), userHeader);
                        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                        return;
                    }
                }
            }
            logger.trace("Switching from user '{}' to user '{}' from request header '{}'", originalUser.getUsername(), requestedUser.getUsername(), USER_HEADER);
            securityService.setUser(requestedUser);
        }
        // See if there are roles provided in the request
        String rolesHeader = httpRequest.getHeader(ROLES_HEADER);
        if (StringUtils.isBlank(rolesHeader)) {
            rolesHeader = httpRequest.getHeader(RUN_WITH_ROLES);
        }
        if (StringUtils.isNotBlank(rolesHeader)) {
            // Role switching is only allowed if the request is coming in with the global sudo role enabled
            if (!originalUser.hasRole(GLOBAL_SUDO_ROLE)) {
                logger.warn("An unauthorized request is trying to switch roles from '{}' to '{}'", requestedUser.getRoles(), rolesHeader);
                ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
            Collection<String> requestedRoles = Arrays.asList(StringUtils.split(rolesHeader, ","));
            if (!originalUser.hasRole(GLOBAL_ADMIN_ROLE)) {
                // Role switching is only allowed to non-system roles
                for (String systemRole : GLOBAL_SYSTEM_ROLES) {
                    if (requestedRoles.contains(systemRole)) {
                        logger.warn("An unauthorized request by user '{}' is trying to gain admin role '{}'", originalUser.getUsername(), systemRole);
                        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                        return;
                    }
                }
                // Role switching is only allowed to non-organization administrator roles
                String organizationAdminRole = requestedOrganization.getAdminRole();
                if (requestedRoles.contains(organizationAdminRole)) {
                    logger.warn("An unauthorized request by user '{}' is trying to gain admin role '{}'", originalUser.getUsername(), organizationAdminRole);
                    ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            }
            // If no user has been provider by the request create a virtual user
            if (StringUtils.isBlank(userHeader)) {
                requestedUser = SecurityUtil.createAnonymousUser(requestedOrganization);
            }
            // Set roles to requested user
            requestedUser = new JaxbUser(requestedUser.getUsername(), requestedUser.getPassword(), requestedUser.getName(), requestedUser.getEmail(), requestedUser.getProvider(), requestedUser.canLogin(), JaxbOrganization.fromOrganization(requestedUser.getOrganization()), Stream.$(requestedRoles).map(toJaxbRole._2(requestedOrganization)).toSet());
            logger.trace("Request roles '{}' are amended to user '{}'", rolesHeader, requestedUser.getUsername());
            securityService.setUser(requestedUser);
        }
        // Execute the rest of the filter chain
        logger.trace("Executing the filter chain with user '{}@{}'", requestedUser.getUsername(), requestedOrganization.getId());
        chain.doFilter(httpRequest, response);
    } finally {
        securityService.setOrganization(originalOrganization);
        securityService.setUser(originalUser);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Organization(org.opencastproject.security.api.Organization) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) HttpServletResponse(javax.servlet.http.HttpServletResponse) NotFoundException(org.opencastproject.util.NotFoundException) JaxbUser(org.opencastproject.security.api.JaxbUser)

Example 57 with User

use of org.opencastproject.security.api.User in project opencast by opencast.

the class RemoteUserAndOrganizationFilterTest method testUserSwitchingToAdminForbidden.

@Test
public void testUserSwitchingToAdminForbidden() throws IOException {
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    filter.setSecurityService(securityService);
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.expect(securityService.getUser()).andAnswer(userResponder).anyTimes();
    EasyMock.replay(securityService);
    User defaultUser = new JaxbUser("admin", "test", new DefaultOrganization(), new JaxbRole(SecurityConstants.GLOBAL_SUDO_ROLE, new DefaultOrganization()));
    userResponder.setResponse(defaultUser);
    switchingUserResponder.setResponse(defaultUser);
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request.getHeader(SecurityConstants.USER_HEADER)).andReturn("admin").anyTimes();
    EasyMock.replay(request);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    response.sendError(EasyMock.anyInt());
    EasyMock.expectLastCall().times(1);
    EasyMock.replay(response);
    try {
        filter.doFilter(request, response, chain);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    EasyMock.verify(response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) HttpServletResponse(javax.servlet.http.HttpServletResponse) JaxbUser(org.opencastproject.security.api.JaxbUser) IOException(java.io.IOException) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Test(org.junit.Test)

Example 58 with User

use of org.opencastproject.security.api.User in project opencast by opencast.

the class RemoteUserAndOrganizationFilterTest method testUserSwitching.

@Test
public void testUserSwitching() throws IOException {
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    filter.setSecurityService(securityService);
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.expect(securityService.getUser()).andAnswer(userResponder).anyTimes();
    securityService.setUser(EasyMock.anyObject(User.class));
    EasyMock.expectLastCall().times(2);
    EasyMock.replay(securityService);
    User defaultUser = new JaxbUser("admin", "test", new DefaultOrganization(), new JaxbRole(SecurityConstants.GLOBAL_SUDO_ROLE, new DefaultOrganization()));
    userResponder.setResponse(defaultUser);
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request.getHeader(SecurityConstants.USER_HEADER)).andReturn("joe").anyTimes();
    EasyMock.replay(request);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(response);
    try {
        filter.doFilter(request, response, chain);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    EasyMock.verify(securityService);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) HttpServletResponse(javax.servlet.http.HttpServletResponse) JaxbUser(org.opencastproject.security.api.JaxbUser) IOException(java.io.IOException) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Test(org.junit.Test)

Example 59 with User

use of org.opencastproject.security.api.User in project opencast by opencast.

the class ThemesServiceDatabaseTest method setUp.

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    // Mock up a security service
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    User user = new JaxbUser("admin", "test", new DefaultOrganization(), new JaxbRole(SecurityConstants.GLOBAL_ADMIN_ROLE, new DefaultOrganization()));
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.replay(securityService);
    UserDirectoryService userDirectoryService = EasyMock.createNiceMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser(EasyMock.anyString())).andReturn(user).anyTimes();
    EasyMock.replay(userDirectoryService);
    MessageSender messageSender = EasyMock.createNiceMock(MessageSender.class);
    messageSender.sendObjectMessage(EasyMock.anyObject(String.class), EasyMock.anyObject(MessageSender.DestinationType.class), EasyMock.anyObject(Serializable.class));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(messageSender);
    themesDatabase = new ThemesServiceDatabaseImpl();
    themesDatabase.setEntityManagerFactory(newTestEntityManagerFactory(ThemesServiceDatabaseImpl.PERSISTENCE_UNIT));
    themesDatabase.setSecurityService(securityService);
    themesDatabase.setUserDirectoryService(userDirectoryService);
    themesDatabase.setMessageSender(messageSender);
    themesDatabase.activate(null);
}
Also used : ThemesServiceDatabaseImpl(org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl) Serializable(java.io.Serializable) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) MessageSender(org.opencastproject.message.broker.api.MessageSender) JaxbUser(org.opencastproject.security.api.JaxbUser) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) Before(org.junit.Before)

Example 60 with User

use of org.opencastproject.security.api.User in project opencast by opencast.

the class CaptureAgentStateServiceImplTest method testAgentVisibility.

@Test
public void testAgentVisibility() throws Exception {
    // Create a new capture agent called "visibility"
    String agentName = "visibility";
    service.setAgentState(agentName, IDLE);
    // Ensure we can see it
    assertEquals(1, service.getKnownAgents().size());
    // Set the roles allowed to use this agent
    Set<String> roles = new HashSet<>();
    roles.add("a_role_we_do_not_have");
    AgentImpl agent = (AgentImpl) service.getAgent(agentName);
    agent.setSchedulerRoles(roles);
    service.updateAgentInDatabase(agent);
    // Since we are an organizational admin, we should still see the agent
    assertEquals(1, service.getKnownAgents().size());
    // Use a security service that identifies us as a non-administrative user
    DefaultOrganization organization = new DefaultOrganization();
    HashSet<JaxbRole> roleSet = new HashSet<>();
    roleSet.add(new JaxbRole("ROLE_NOT_ADMIN", organization, ""));
    User user = new JaxbUser("testuser", "test", organization, roleSet);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.replay(securityService);
    service.setSecurityService(securityService);
    // Ensure we can no longer see the agent, since we don't have an administrative role
    assertEquals(0, service.getKnownAgents().size());
// TODO: Do we need to enforce access strictly? If someone asks for an agent by name, but they do not have the
// appropriate scheduler role, should we throw UnauthorizedException?
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) HashSet(java.util.HashSet) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Test(org.junit.Test)

Aggregations

User (org.opencastproject.security.api.User)156 Organization (org.opencastproject.security.api.Organization)61 JaxbUser (org.opencastproject.security.api.JaxbUser)60 JaxbRole (org.opencastproject.security.api.JaxbRole)49 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)44 SecurityService (org.opencastproject.security.api.SecurityService)43 NotFoundException (org.opencastproject.util.NotFoundException)32 Before (org.junit.Before)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)26 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)24 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)23 AccessControlList (org.opencastproject.security.api.AccessControlList)21 Role (org.opencastproject.security.api.Role)21 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)21 HashSet (java.util.HashSet)20 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)17 IOException (java.io.IOException)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)16