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);
}
};
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations