Search in sources :

Example 61 with Principal

use of java.security.Principal in project robovm by robovm.

the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener.

public void test_SSLSocket_HandshakeCompletedListener() throws Exception {
    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    final boolean[] handshakeCompletedListenerCalled = new boolean[1];
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent event) {
            try {
                SSLSession session = event.getSession();
                String cipherSuite = event.getCipherSuite();
                Certificate[] localCertificates = event.getLocalCertificates();
                Certificate[] peerCertificates = event.getPeerCertificates();
                javax.security.cert.X509Certificate[] peerCertificateChain = event.getPeerCertificateChain();
                Principal peerPrincipal = event.getPeerPrincipal();
                Principal localPrincipal = event.getLocalPrincipal();
                Socket socket = event.getSocket();
                if (false) {
                    System.out.println("Session=" + session);
                    System.out.println("CipherSuite=" + cipherSuite);
                    System.out.println("LocalCertificates=" + Arrays.toString(localCertificates));
                    System.out.println("PeerCertificates=" + Arrays.toString(peerCertificates));
                    System.out.println("PeerCertificateChain=" + Arrays.toString(peerCertificateChain));
                    System.out.println("PeerPrincipal=" + peerPrincipal);
                    System.out.println("LocalPrincipal=" + localPrincipal);
                    System.out.println("Socket=" + socket);
                }
                assertNotNull(session);
                byte[] id = session.getId();
                assertNotNull(id);
                assertEquals(32, id.length);
                assertNotNull(c.clientContext.getClientSessionContext().getSession(id));
                assertNotNull(cipherSuite);
                assertTrue(Arrays.asList(client.getEnabledCipherSuites()).contains(cipherSuite));
                assertTrue(Arrays.asList(c.serverSocket.getEnabledCipherSuites()).contains(cipherSuite));
                assertNull(localCertificates);
                assertNotNull(peerCertificates);
                TestKeyStore.assertChainLength(peerCertificates);
                assertNotNull(peerCertificates[0]);
                TestSSLContext.assertServerCertificateChain(c.clientTrustManager, peerCertificates);
                TestSSLContext.assertCertificateInKeyStore(peerCertificates[0], c.serverKeyStore);
                assertNotNull(peerCertificateChain);
                TestKeyStore.assertChainLength(peerCertificateChain);
                assertNotNull(peerCertificateChain[0]);
                TestSSLContext.assertCertificateInKeyStore(peerCertificateChain[0].getSubjectDN(), c.serverKeyStore);
                assertNotNull(peerPrincipal);
                TestSSLContext.assertCertificateInKeyStore(peerPrincipal, c.serverKeyStore);
                assertNull(localPrincipal);
                assertNotNull(socket);
                assertSame(client, socket);
                synchronized (handshakeCompletedListenerCalled) {
                    handshakeCompletedListenerCalled[0] = true;
                    handshakeCompletedListenerCalled.notify();
                }
                handshakeCompletedListenerCalled[0] = true;
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    client.startHandshake();
    future.get();
    if (!TestSSLContext.sslServerSocketSupportsSessionTickets()) {
        assertNotNull(c.serverContext.getServerSessionContext().getSession(client.getSession().getId()));
    }
    synchronized (handshakeCompletedListenerCalled) {
        while (!handshakeCompletedListenerCalled[0]) {
            handshakeCompletedListenerCalled.wait();
        }
    }
    client.close();
    server.close();
    c.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSession(javax.net.ssl.SSLSession) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) ExecutorService(java.util.concurrent.ExecutorService) Principal(java.security.Principal) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 62 with Principal

use of java.security.Principal in project spring-boot by spring-projects.

the class MvcEndpointSecurityInterceptorTests method sensitiveEndpointIfRoleIsNotCorrectShouldNotAllowAccess.

@Test
public void sensitiveEndpointIfRoleIsNotCorrectShouldNotAllowAccess() throws Exception {
    Principal principal = mock(Principal.class);
    this.request.setUserPrincipal(principal);
    this.servletContext.declareRoles("HERO");
    assertThat(this.securityInterceptor.preHandle(this.request, this.response, this.handlerMethod)).isFalse();
    verify(this.response).sendError(HttpStatus.FORBIDDEN.value(), "Access is denied. User must have one of the these roles: SUPER_HERO");
}
Also used : Principal(java.security.Principal) Test(org.junit.Test)

Example 63 with Principal

use of java.security.Principal in project spring-boot by spring-projects.

the class NoSpringSecurityHealthMvcEndpointIntegrationTests method getRequestPostProcessor.

private RequestPostProcessor getRequestPostProcessor() {
    return new RequestPostProcessor() {

        @Override
        public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
            Principal principal = mock(Principal.class);
            request.setUserPrincipal(principal);
            return request;
        }
    };
}
Also used : RequestPostProcessor(org.springframework.test.web.servlet.request.RequestPostProcessor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Principal(java.security.Principal)

Example 64 with Principal

use of java.security.Principal in project spring-boot by spring-projects.

the class NoSpringSecurityMvcEndpointSecurityInterceptorTests method sensitiveEndpointIfRoleNotPresentShouldNotValidateAuthorities.

@Test
public void sensitiveEndpointIfRoleNotPresentShouldNotValidateAuthorities() throws Exception {
    Principal principal = mock(Principal.class);
    this.request.setUserPrincipal(principal);
    this.servletContext.declareRoles("HERO");
    assertThat(this.securityInterceptor.preHandle(this.request, this.response, this.handlerMethod)).isFalse();
}
Also used : Principal(java.security.Principal) Test(org.junit.Test)

Example 65 with Principal

use of java.security.Principal in project spring-framework by spring-projects.

the class RxNettyRequestUpgradeStrategy method getHandshakeInfo.

private HandshakeInfo getHandshakeInfo(ServerWebExchange exchange, Optional<String> protocol) {
    ServerHttpRequest request = exchange.getRequest();
    Mono<Principal> principal = exchange.getPrincipal();
    return new HandshakeInfo(request.getURI(), request.getHeaders(), principal, protocol);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

Principal (java.security.Principal)931 Test (org.junit.Test)243 Subject (javax.security.auth.Subject)114 EveryonePrincipal (org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal)114 HashSet (java.util.HashSet)89 User (org.apache.jackrabbit.api.security.user.User)75 Group (org.apache.jackrabbit.api.security.user.Group)74 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)58 Privilege (javax.jcr.security.Privilege)57 RepositoryException (javax.jcr.RepositoryException)51 IOException (java.io.IOException)50 ArrayList (java.util.ArrayList)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 TestPrincipal (org.apache.jackrabbit.core.security.TestPrincipal)45 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)43 EveryonePrincipal (org.apache.jackrabbit.core.security.principal.EveryonePrincipal)42 PrincipalIterator (org.apache.jackrabbit.api.security.principal.PrincipalIterator)40 HashMap (java.util.HashMap)39 PrincipalImpl (org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl)39 X500Principal (javax.security.auth.x500.X500Principal)38