Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
added support for JACC authetnication to allow delegation to applicat…
Browse files Browse the repository at this point in the history
…ion server security mechanism
  • Loading branch information
mswiderski authored and manstis committed Apr 10, 2013
1 parent add5192 commit a389801
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 13 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Expand Up @@ -60,7 +60,7 @@
<gwt-dnd.version>3.1.2</gwt-dnd.version> <gwt-dnd.version>3.1.2</gwt-dnd.version>
<hamcrest.version>1.3</hamcrest.version> <hamcrest.version>1.3</hamcrest.version>
<jasypt.version>1.9.0</jasypt.version> <jasypt.version>1.9.0</jasypt.version>
<jacc.version>1.4.0</jacc.version> <jacc.version>1.4</jacc.version>
</properties> </properties>


<scm> <scm>
Expand Down Expand Up @@ -725,6 +725,13 @@
<version>${jasypt.version}</version> <version>${jasypt.version}</version>
</dependency> </dependency>


<dependency>
<groupId>javax.security.jacc</groupId>
<artifactId>javax.security.jacc-api</artifactId>
<version>${jacc.version}</version>
<scope>provided</scope>
</dependency>

</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>


Expand Down
@@ -0,0 +1,15 @@
package org.uberfire.security.impl.auth;

import org.uberfire.security.auth.Credential;

public class UserNameCredential implements Credential {
private final String userName;

public UserNameCredential(final String userName) {
this.userName = userName;
}

public String getUserName() {
return userName;
}
}
Expand Up @@ -18,20 +18,15 @@


import org.uberfire.security.auth.Credential; import org.uberfire.security.auth.Credential;


public class UsernamePasswordCredential implements Credential { public class UsernamePasswordCredential extends UserNameCredential {


private final String userName;
private final Object passwd; private final Object passwd;


public UsernamePasswordCredential(final String userName, final Object passwd) { public UsernamePasswordCredential(final String userName, final Object passwd) {
this.userName = userName; super(userName);
this.passwd = passwd; this.passwd = passwd;
} }


public String getUserName() {
return userName;
}

public Object getPassword() { public Object getPassword() {
return passwd; return passwd;
} }
Expand Down
5 changes: 5 additions & 0 deletions uberfire-security/uberfire-security-server/pom.xml
Expand Up @@ -69,6 +69,11 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>


<dependency>
<groupId>javax.security.jacc</groupId>
<artifactId>javax.security.jacc-api</artifactId>
</dependency>

</dependencies> </dependencies>


</project> </project>
Expand Up @@ -57,4 +57,7 @@ public interface SecurityConstants {
final String LOGOUT_URI = "/uf_logout"; final String LOGOUT_URI = "/uf_logout";


final String SUBJECT_ON_SESSION_KEY = "org.uf.subject"; final String SUBJECT_ON_SESSION_KEY = "org.uf.subject";

final String ROLES_IN_CONTEXT_KEY = "org.uf.context.roles";

} }
Expand Up @@ -188,15 +188,18 @@ private AuthenticationManager getAuthenticationManager( final Map<String, String


private AuthenticationScheme getAuthenticationScheme( final Map<String, String> options ) { private AuthenticationScheme getAuthenticationScheme( final Map<String, String> options ) {
final String authScheme = options.get( AUTH_SCHEME_KEY ); final String authScheme = options.get( AUTH_SCHEME_KEY );
AuthenticationScheme scheme = null;
if ( authScheme == null || authScheme.isEmpty() ) { if ( authScheme == null || authScheme.isEmpty() ) {
return new FormAuthenticationScheme(); return new FormAuthenticationScheme();
} else {
scheme = loadConfigClazz( authScheme, AuthenticationScheme.class );
} }


if ( authScheme.equalsIgnoreCase( FORM ) ) { if ( scheme == null && authScheme.equalsIgnoreCase( FORM ) ) {
return new FormAuthenticationScheme(); return new FormAuthenticationScheme();
} }


return null; return scheme;
} }


@Override @Override
Expand Down Expand Up @@ -296,7 +299,7 @@ private <T> T loadConfigClazz( final String clazzName,
try { try {
final Class<?> clazz = Class.forName( clazzName ); final Class<?> clazz = Class.forName( clazzName );


if ( !clazz.isAssignableFrom( typeOf ) ) { if ( !typeOf.isAssignableFrom( clazz ) ) {
LOG.error( "Invalid class type '" + typeOf.getName() + "'" ); LOG.error( "Invalid class type '" + typeOf.getName() + "'" );
return null; return null;
} }
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.uberfire.security.auth.AuthenticationStatus; import org.uberfire.security.auth.AuthenticationStatus;
import org.uberfire.security.auth.Credential; import org.uberfire.security.auth.Credential;
import org.uberfire.security.auth.Principal; import org.uberfire.security.auth.Principal;
import org.uberfire.security.impl.auth.UserNameCredential;
import org.uberfire.security.impl.auth.UsernamePasswordCredential; import org.uberfire.security.impl.auth.UsernamePasswordCredential;


import static java.util.Collections.*; import static java.util.Collections.*;
Expand Down Expand Up @@ -74,7 +75,7 @@ public Principal getPrincipal() {
}; };
} }


final UsernamePasswordCredential realCredential = UsernamePasswordCredential.class.cast(credential); final UserNameCredential realCredential = UserNameCredential.class.cast(credential);


if (!authenticationSource.authenticate(realCredential)) { if (!authenticationSource.authenticate(realCredential)) {
return new AuthenticationResult() { return new AuthenticationResult() {
Expand Down
Expand Up @@ -163,7 +163,7 @@ public String getName() {
} }


final String originalRequest = requestCache.remove(httpContext.getRequest().getSession().getId()); final String originalRequest = requestCache.remove(httpContext.getRequest().getSession().getId());
if (originalRequest != null && !originalRequest.isEmpty()) { if (originalRequest != null && !originalRequest.isEmpty() && !httpContext.getResponse().isCommitted()) {
try { try {
httpContext.getResponse().sendRedirect(originalRequest); httpContext.getResponse().sendRedirect(originalRequest);
} catch (IOException e) { } catch (IOException e) {
Expand All @@ -179,5 +179,7 @@ public void logout(final SecurityContext context) throws AuthenticationException
for (final AuthenticatedStorageProvider storeProvider : authStorageProviders) { for (final AuthenticatedStorageProvider storeProvider : authStorageProviders) {
storeProvider.cleanup(context); storeProvider.cleanup(context);
} }
final HttpSecurityContext httpContext = checkInstanceOf("context", context, HttpSecurityContext.class);
httpContext.getRequest().getSession().invalidate();
} }
} }
@@ -0,0 +1,26 @@
package org.uberfire.security.server.auth;

import org.uberfire.security.SecurityContext;
import org.uberfire.security.auth.AuthenticationScheme;
import org.uberfire.security.auth.Credential;
import org.uberfire.security.impl.auth.UserNameCredential;
import org.uberfire.security.server.HttpSecurityContext;

import static org.kie.commons.validation.Preconditions.checkInstanceOf;

public class JACCAuthenticationScheme extends FormAuthenticationScheme implements AuthenticationScheme {

@Override
public void challengeClient(SecurityContext context) {

}

@Override
public Credential buildCredential(SecurityContext context) {

final HttpSecurityContext httpSecurityContext = checkInstanceOf("context", context, HttpSecurityContext.class);

final String userName = httpSecurityContext.getRequest().getUserPrincipal().getName();
return new UserNameCredential(userName);
}
}
@@ -0,0 +1,100 @@
package org.uberfire.security.server.auth.source;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.jacc.PolicyContext;

import org.uberfire.security.Role;
import org.uberfire.security.auth.AuthenticationSource;
import org.uberfire.security.auth.Credential;
import org.uberfire.security.auth.Principal;
import org.uberfire.security.auth.RoleProvider;
import org.uberfire.security.impl.auth.UserNameCredential;

import static org.kie.commons.validation.Preconditions.checkInstanceOf;
import static org.uberfire.security.server.SecurityConstants.*;

public class JACCAuthenticationSource implements AuthenticationSource, RoleProvider{

public static final String DEFAULT_ROLE_PRINCIPLE_NAME = "Roles";
private String rolePrincipleName = DEFAULT_ROLE_PRINCIPLE_NAME;

@Override
public void initialize(Map<String, ?> options) {
if (options.containsKey(ROLES_IN_CONTEXT_KEY)) {
rolePrincipleName = (String) options.get(ROLES_IN_CONTEXT_KEY);
}
}

@Override public boolean supportsCredential(Credential credential) {
if (credential == null) {
return false;
}
return credential instanceof UserNameCredential;
}

@Override public boolean authenticate(Credential credential) {
final UserNameCredential userNameCredential = checkInstanceOf("credential", credential, UserNameCredential.class);
try {
Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");

if (subject != null) {
Set<java.security.Principal> principals = subject.getPrincipals();

if (principals != null) {
for (java.security.Principal p : principals) {
if (p.getName().equals(userNameCredential.getUserName())) {
return true;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

@Override
public List<Role> loadRoles(Principal principal) {
List<Role> roles = null;
try {
Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");

if (subject != null) {
Set<java.security.Principal> principals = subject.getPrincipals();

if (principals != null) {
roles = new ArrayList<Role>();
for (java.security.Principal p : principals) {
if (p instanceof Group && rolePrincipleName.equalsIgnoreCase(p.getName())) {
Enumeration<? extends java.security.Principal> groups = ((Group) p).members();

while (groups.hasMoreElements()) {
final java.security.Principal groupPrincipal = (java.security.Principal) groups.nextElement();
roles.add(new Role() {
@Override
public String getName() {
return groupPrincipal.getName();
}
});

}
break;

}

}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return roles;
}
}

0 comments on commit a389801

Please sign in to comment.