Search in sources :

Example 1 with SimplePrincipal

use of org.apache.cxf.common.security.SimplePrincipal in project cxf by apache.

the class AbstractAuthFilter method createSecurityContext.

protected SecurityContext createSecurityContext(HttpServletRequest request, final OAuthInfo info) {
    // TODO:
    // This custom parameter is only needed by the "oauth"
    // demo shipped in the distribution; needs to be removed.
    request.setAttribute("oauth_authorities", info.getRoles());
    UserSubject subject = info.getToken().getSubject();
    final UserSubject theSubject = subject;
    return new SecurityContext() {

        public Principal getUserPrincipal() {
            String login = AbstractAuthFilter.this.useUserSubject ? (theSubject != null ? theSubject.getLogin() : null) : info.getToken().getClient().getLoginName();
            return new SimplePrincipal(login);
        }

        public boolean isUserInRole(String role) {
            List<String> roles = null;
            if (AbstractAuthFilter.this.useUserSubject && theSubject != null) {
                roles = theSubject.getRoles();
            } else {
                roles = info.getRoles();
            }
            return roles.contains(role);
        }
    };
}
Also used : UserSubject(org.apache.cxf.rs.security.oauth.data.UserSubject) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Example 2 with SimplePrincipal

use of org.apache.cxf.common.security.SimplePrincipal in project cxf by apache.

the class ClaimsAuthorizingInterceptorTest method prepareMessage.

private Message prepareMessage(Class<?> cls, String methodName, org.apache.cxf.rt.security.claims.Claim... claim) throws Exception {
    ClaimCollection claims = new ClaimCollection();
    claims.addAll(Arrays.asList(claim));
    Set<Principal> roles = SAMLUtils.parseRolesFromClaims(claims, SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT, SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
    SecurityContext sc = new SAMLSecurityContext(new SimplePrincipal("user"), roles, claims);
    Message m = new MessageImpl();
    m.setExchange(new ExchangeImpl());
    m.put(SecurityContext.class, sc);
    m.put("org.apache.cxf.resource.method", cls.getMethod(methodName, new Class[] {}));
    return m;
}
Also used : Message(org.apache.cxf.message.Message) SAMLSecurityContext(org.apache.cxf.rt.security.saml.claims.SAMLSecurityContext) SAMLSecurityContext(org.apache.cxf.rt.security.saml.claims.SAMLSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) MessageImpl(org.apache.cxf.message.MessageImpl) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 3 with SimplePrincipal

use of org.apache.cxf.common.security.SimplePrincipal in project tesb-rt-se by Talend.

the class SecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Message message = JAXRSUtils.getCurrentMessage();
    if (ui.getAbsolutePath().toString().endsWith(userRegistrationPath)) {
        return;
    }
    List<String> authValues = headers.getRequestHeader("Authorization");
    if (authValues.size() != 1) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] values = authValues.get(0).split(" ");
    if (values.length != 2 || !"Basic".equals(values[0])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String decodedValue = null;
    try {
        decodedValue = new String(Base64Utility.decode(values[1]));
    } catch (Base64Exception ex) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] namePassword = decodedValue.split(":");
    if (namePassword.length != 2) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final UserAccount account = accounts.getAccount(namePassword[0]);
    if (account == null || !account.getPassword().equals(namePassword[1])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final SecurityContext sc = new SecurityContext() {

        public Principal getUserPrincipal() {
            return new SimplePrincipal(account.getName());
        }

        public boolean isUserInRole(String arg0) {
            return false;
        }
    };
    message.put(SecurityContext.class, sc);
}
Also used : Message(org.apache.cxf.message.Message) Base64Exception(org.apache.cxf.common.util.Base64Exception) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Example 4 with SimplePrincipal

use of org.apache.cxf.common.security.SimplePrincipal in project tesb-rt-se by Talend.

the class SecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    List<String> authValues = headers.getRequestHeader("Authorization");
    if (authValues.size() != 1) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] values = authValues.get(0).split(" ");
    if (values.length != 2 || !"Basic".equals(values[0])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String decodedValue = null;
    try {
        decodedValue = new String(Base64Utility.decode(values[1]));
    } catch (Base64Exception ex) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final String[] namePassword = decodedValue.split(":");
    if (namePassword.length != 2) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String password = users.get(namePassword[0]);
    if (password == null || !password.equals(namePassword[1])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final SecurityContext sc = new SecurityContext() {

        public Principal getUserPrincipal() {
            return new SimplePrincipal(namePassword[0]);
        }

        public boolean isUserInRole(String arg0) {
            return false;
        }
    };
    JAXRSUtils.getCurrentMessage().put(SecurityContext.class, sc);
}
Also used : Base64Exception(org.apache.cxf.common.util.Base64Exception) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Example 5 with SimplePrincipal

use of org.apache.cxf.common.security.SimplePrincipal in project teiid by teiid.

the class TestLocalConnections method testPassThroughDifferentUsers.

@Test
public void testPassThroughDifferentUsers() throws Throwable {
    MockSecurityHelper securityHelper = new MockSecurityHelper();
    SecurityHelper current = server.getSessionService().getSecurityHelper();
    server.getClientServiceRegistry().setSecurityHelper(securityHelper);
    server.getSessionService().setSecurityHelper(securityHelper);
    try {
        final Connection c = server.createConnection("jdbc:teiid:PartsSupplier;PassthroughAuthentication=true");
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery("select session_id()");
        Subject o = currentContext;
        currentContext = null;
        s.cancel();
        currentContext = o;
        rs.next();
        String id = rs.getString(1);
        rs.close();
        assertEquals(4, securityHelper.calls);
        server.getSessionService().pingServer(id);
        currentContext = new Subject();
        currentContext.getPrincipals().add(new SimplePrincipal("x"));
        rs = s.executeQuery("select session_id()");
        rs.next();
        String id1 = rs.getString(1);
        rs.close();
        assertFalse(id.equals(id1));
        try {
            server.getSessionService().pingServer(id);
            // should have logged off
            fail();
        } catch (InvalidSessionException e) {
        }
    } finally {
        server.getClientServiceRegistry().setSecurityHelper(current);
        server.getSessionService().setSecurityHelper(current);
    }
}
Also used : InvalidSessionException(org.teiid.client.security.InvalidSessionException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Subject(javax.security.auth.Subject) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) SecurityHelper(org.teiid.security.SecurityHelper) Test(org.junit.Test)

Aggregations

SimplePrincipal (org.apache.cxf.common.security.SimplePrincipal)33 Principal (java.security.Principal)19 Subject (javax.security.auth.Subject)19 Test (org.junit.Test)13 SimpleGroup (org.apache.cxf.common.security.SimpleGroup)10 SecurityContext (org.apache.cxf.security.SecurityContext)9 GroupPrincipal (org.apache.cxf.common.security.GroupPrincipal)6 Message (org.apache.cxf.message.Message)6 LoginSecurityContext (org.apache.cxf.security.LoginSecurityContext)4 IOException (java.io.IOException)3 Callback (javax.security.auth.callback.Callback)3 NameCallback (javax.security.auth.callback.NameCallback)3 PasswordCallback (javax.security.auth.callback.PasswordCallback)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 LoginException (javax.security.auth.login.LoginException)3 Base64Exception (org.apache.cxf.common.util.Base64Exception)3 ClaimCollection (org.apache.cxf.rt.security.claims.ClaimCollection)3 HashSet (java.util.HashSet)2 CallbackHandler (javax.security.auth.callback.CallbackHandler)2 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)2