[Minor] code cleanup

This commit is contained in:
Robert von Burg 2012-11-19 22:50:32 +01:00
parent 8b780368d7
commit 59e25a8e75
12 changed files with 278 additions and 317 deletions

332
pom.xml
View File

@ -1,201 +1,163 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.privilege</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>ch.eitchnet.privilege</name>
<url>https://github.com/eitch/ch.eitchnet.privilege</url>
<properties> <groupId>ch.eitchnet</groupId>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <artifactId>ch.eitchnet.privilege</artifactId>
</properties> <packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>ch.eitchnet.privilege</name>
<url>https://github.com/eitch/ch.eitchnet.privilege</url>
<!-- POM Reference: http://maven.apache.org/pom.html#Licenses --> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<inceptionYear>2011</inceptionYear> <!-- POM Reference: http://maven.apache.org/pom.html#Licenses -->
<licenses>
<license>
<name>GNU Lesser General Public License</name>
<url>http://www.gnu.org/licenses/lgpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<organization>
<name>eitchnet.ch</name>
<url>http://blog.eitchnet.ch</url>
</organization>
<developers>
<developer>
<id>eitch</id>
<name>Robert von Vurg</name>
<email>eitch@eitchnet.ch</email>
<url>http://blog.eitchnet.ch</url>
<organization>eitchnet.ch</organization>
<organizationUrl>http://blog.eitchnet.ch</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+1</timezone>
<properties>
<picUrl>http://localhost</picUrl>
</properties>
</developer>
</developers>
<issueManagement> <inceptionYear>2011</inceptionYear>
<system>Github Issues</system> <licenses>
<url>https://github.com/eitch/ch.eitchnet.privilege/issues</url> <license>
</issueManagement> <name>GNU Lesser General Public License</name>
<url>http://www.gnu.org/licenses/lgpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<organization>
<name>eitchnet.ch</name>
<url>http://blog.eitchnet.ch</url>
</organization>
<developers>
<developer>
<id>eitch</id>
<name>Robert von Vurg</name>
<email>eitch@eitchnet.ch</email>
<url>http://blog.eitchnet.ch</url>
<organization>eitchnet.ch</organization>
<organizationUrl>http://blog.eitchnet.ch</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+1</timezone>
<properties>
<picUrl>http://localhost</picUrl>
</properties>
</developer>
</developers>
<!-- <issueManagement>
<ciManagement> <system>Github Issues</system>
<system>continuum</system> <url>https://github.com/eitch/ch.eitchnet.privilege/issues</url>
<url>http://127.0.0.1:8080/continuum</url> </issueManagement>
<notifiers>
<notifier>
<type>mail</type>
<sendOnError>true</sendOnError>
<sendOnFailure>true</sendOnFailure>
<sendOnSuccess>false</sendOnSuccess>
<sendOnWarning>false</sendOnWarning>
<configuration><address>continuum@127.0.0.1</address></configuration>
</notifier>
</notifiers>
</ciManagement>
<mailingLists> <!-- <ciManagement> <system>continuum</system> <url>http://127.0.0.1:8080/continuum</url>
<mailingList> <notifiers> <notifier> <type>mail</type> <sendOnError>true</sendOnError>
<name>User List</name> <sendOnFailure>true</sendOnFailure> <sendOnSuccess>false</sendOnSuccess>
<subscribe>user-subscribe@127.0.0.1</subscribe> <sendOnWarning>false</sendOnWarning> <configuration><address>continuum@127.0.0.1</address></configuration>
<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe> </notifier> </notifiers> </ciManagement> <mailingLists> <mailingList> <name>User
<post>user@127.0.0.1</post> List</name> <subscribe>user-subscribe@127.0.0.1</subscribe> <unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
<archive>http://127.0.0.1/user/</archive> <post>user@127.0.0.1</post> <archive>http://127.0.0.1/user/</archive> <otherArchives>
<otherArchives> <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive> </otherArchives>
<otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive> </mailingList> </mailingLists> -->
</otherArchives>
</mailingList>
</mailingLists>
-->
<scm> <scm>
<connection>scm:git:https://github.com/eitch/ch.eitchnet.privilege.git</connection> <connection>scm:git:https://github.com/eitch/ch.eitchnet.privilege.git</connection>
<developerConnection>scm:git:git@github.com:eitch/ch.eitchnet.privilege.git</developerConnection> <developerConnection>scm:git:git@github.com:eitch/ch.eitchnet.privilege.git</developerConnection>
<url>https://github.com/eitch/ch.eitchnet.privilege</url> <url>https://github.com/eitch/ch.eitchnet.privilege</url>
</scm> </scm>
<!-- <!-- <repositories> <repository> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy>
<repositories> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled>
<repository> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy>
<releases> </snapshots> <id>codehausSnapshots</id> <name>Codehaus Snapshots</name> <url>http://snapshots.maven.codehaus.org/maven2</url>
<enabled>false</enabled> <layout>default</layout> </repository> </repositories> <pluginRepositories>
<updatePolicy>always</updatePolicy> ... </pluginRepositories> -->
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
-->
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.10</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.17</version> <version>1.2.17</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>maven</groupId> <groupId>maven</groupId>
<artifactId>dom4j</artifactId> <artifactId>dom4j</artifactId>
<version>1.7-20060614</version> <version>1.7-20060614</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>ch.eitchnet</groupId> <groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.utils</artifactId> <artifactId>ch.eitchnet.utils</artifactId>
<version>0.1.0-SNAPSHOT</version> <version>0.1.0-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId>
<artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version>
<version>2.9</version> <configuration>
<configuration> <downloadJavadocs>true</downloadJavadocs>
<downloadJavadocs>true</downloadJavadocs> <downloadSources>true</downloadSources>
<downloadSources>true</downloadSources> </configuration>
</configuration> </plugin>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>3.0</version>
<configuration> <configuration>
<source>1.6</source> <source>1.6</source>
<target>1.6</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>2.4</version> <version>2.1.2</version>
<configuration> <executions>
<archive> <execution>
<manifest> <id>attach-sources</id>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries> <phase>verify</phase>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> <goals>
<!--mainClass>ch.eitchnet.App</mainClass--> <goal>jar-no-fork</goal>
</manifest> </goals>
</archive> </execution>
</configuration> </executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.3</version> <version>2.4</version>
<configuration> <configuration>
<outputEncoding>UTF-8</outputEncoding> <archive>
</configuration> <manifest>
</plugin> <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<!--mainClass>ch.eitchnet.App</mainClass -->
</manifest>
</archive>
</configuration>
</plugin>
</plugins> <plugin>
</build> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.3</version>
<configuration>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -124,7 +124,7 @@ public class DefaultEncryptionHandler implements EncryptionHandler {
// test hash algorithm // test hash algorithm
try { try {
convertToHash("test"); convertToHash("test");
logger.info("Using hashing algorithm " + this.hashAlgorithm); DefaultEncryptionHandler.logger.info("Using hashing algorithm " + this.hashAlgorithm);
} catch (Exception e) { } catch (Exception e) {
throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter " throw new PrivilegeException("[" + EncryptionHandler.class.getName() + "] Defined parameter "
+ XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid because of underlying exception: " + XmlConstants.XML_PARAM_HASH_ALGORITHM + " is invalid because of underlying exception: "

View File

@ -403,7 +403,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// ignore if user already has role // ignore if user already has role
Set<String> currentRoles = user.getRoles(); Set<String> currentRoles = user.getRoles();
if (currentRoles.contains(roleName)) { if (currentRoles.contains(roleName)) {
logger.error("User " + username + " already has role " + roleName); DefaultPrivilegeHandler.logger.error("User " + username + " already has role " + roleName);
return; return;
} }
@ -496,7 +496,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// ignore if user does not have role // ignore if user does not have role
Set<String> currentRoles = user.getRoles(); Set<String> currentRoles = user.getRoles();
if (!currentRoles.contains(roleName)) { if (!currentRoles.contains(roleName)) {
logger.error("User " + user + " does not have role " + roleName); DefaultPrivilegeHandler.logger.error("User " + user + " does not have role " + roleName);
return; return;
} }
@ -714,10 +714,10 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
this.sessionMap.put(sessionId, new CertificateSessionPair(session, certificate)); this.sessionMap.put(sessionId, new CertificateSessionPair(session, certificate));
// log // log
logger.info("User " + username + " authenticated: " + session); DefaultPrivilegeHandler.logger.info("User " + username + " authenticated: " + session);
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.error("User " + username + " Failed to authenticate: " + e.getLocalizedMessage()); DefaultPrivilegeHandler.logger.error("User " + username + " Failed to authenticate: " + e.getLocalizedMessage());
throw e; throw e;
} finally { } finally {
clearPassword(password); clearPassword(password);
@ -742,9 +742,9 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
// return true if object was really removed // return true if object was really removed
boolean loggedOut = certificateSessionPair != null; boolean loggedOut = certificateSessionPair != null;
if (loggedOut) if (loggedOut)
logger.info("User " + certificate.getUsername() + " logged out."); DefaultPrivilegeHandler.logger.info("User " + certificate.getUsername() + " logged out.");
else else
logger.warn("User already logged out!"); DefaultPrivilegeHandler.logger.warn("User already logged out!");
return loggedOut; return loggedOut;
} }
@ -785,7 +785,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
Role role = this.persistenceHandler.getRole(roleName); Role role = this.persistenceHandler.getRole(roleName);
if (role == null) { if (role == null) {
logger.error("No role is defined with name " + roleName + " which is configured for user " + user); DefaultPrivilegeHandler.logger.error("No role is defined with name " + roleName + " which is configured for user " + user);
continue; continue;
} }
@ -861,7 +861,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
} }
// otherwise delegate checking to the policy configured for this privilege // otherwise delegate checking to the policy configured for this privilege
PrivilegePolicy policy = this.getPolicy(privilege.getPolicy()); PrivilegePolicy policy = getPolicy(privilege.getPolicy());
if (policy == null) { if (policy == null) {
throw new PrivilegeException("PrivilegePolicy " + privilege.getPolicy() + " does not exist for Privilege " throw new PrivilegeException("PrivilegePolicy " + privilege.getPolicy() + " does not exist for Privilege "
+ privilegeName); + privilegeName);
@ -921,7 +921,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
public void validateIsPrivilegeAdmin(Certificate certificate) throws PrivilegeException { public void validateIsPrivilegeAdmin(Certificate certificate) throws PrivilegeException {
// validate certificate // validate certificate
this.isCertificateValid(certificate); isCertificateValid(certificate);
// get user object // get user object
User user = this.persistenceHandler.getUser(certificate.getUsername()); User user = this.persistenceHandler.getUser(certificate.getUsername());
@ -1139,7 +1139,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
private Certificate getSystemUserCertificate(String systemUsername) { private Certificate getSystemUserCertificate(String systemUsername) {
// see if a certificate has already been created for this system user // see if a certificate has already been created for this system user
Certificate systemUserCertificate = systemUserCertificateMap.get(systemUsername); Certificate systemUserCertificate = this.systemUserCertificateMap.get(systemUsername);
if (systemUserCertificate != null) if (systemUserCertificate != null)
return systemUserCertificate; return systemUserCertificate;
@ -1187,7 +1187,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
this.sessionMap.put(sessionId, new CertificateSessionPair(session, systemUserCertificate)); this.sessionMap.put(sessionId, new CertificateSessionPair(session, systemUserCertificate));
// log // log
logger.info("The system user " + systemUsername + " is logged in with session " + session); DefaultPrivilegeHandler.logger.info("The system user " + systemUsername + " is logged in with session " + session);
return systemUserCertificate; return systemUserCertificate;
} }

View File

@ -153,7 +153,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
File modelFile = new File(this.modelPath); File modelFile = new File(this.modelPath);
boolean modelFileUnchanged = modelFile.exists() && modelFile.lastModified() == this.modelsFileDate; boolean modelFileUnchanged = modelFile.exists() && modelFile.lastModified() == this.modelsFileDate;
if (!(modelFileUnchanged && this.roleMapDirty && this.userMapDirty)) { if (!(modelFileUnchanged && this.roleMapDirty && this.userMapDirty)) {
logger.warn("Not persisting as current file is unchanged and model data is not dirty"); XmlPersistenceHandler.logger.warn("Not persisting as current file is unchanged and model data is not dirty");
return false; return false;
} }
@ -164,7 +164,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
// USERS // USERS
// build XML DOM of users // build XML DOM of users
List<Element> users = toDomUsers(this.userMap); List<Element> users = XmlPersistenceHandler.toDomUsers(this.userMap);
Element usersElement = docFactory.createElement(XmlConstants.XML_USERS); Element usersElement = docFactory.createElement(XmlConstants.XML_USERS);
for (Element userElement : users) { for (Element userElement : users) {
usersElement.add(userElement); usersElement.add(userElement);
@ -173,7 +173,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
// ROLES // ROLES
// build XML DOM of roles // build XML DOM of roles
List<Element> roles = toDomRoles(this.roleMap); List<Element> roles = XmlPersistenceHandler.toDomRoles(this.roleMap);
Element rolesElement = docFactory.createElement(XmlConstants.XML_ROLES); Element rolesElement = docFactory.createElement(XmlConstants.XML_ROLES);
for (Element roleElement : roles) { for (Element roleElement : roles) {
rolesElement.add(roleElement); rolesElement.add(roleElement);
@ -233,8 +233,8 @@ public class XmlPersistenceHandler implements PersistenceHandler {
this.userMapDirty = false; this.userMapDirty = false;
this.roleMapDirty = false; this.roleMapDirty = false;
logger.info("Read " + this.userMap.size() + " Users"); XmlPersistenceHandler.logger.info("Read " + this.userMap.size() + " Users");
logger.info("Read " + this.roleMap.size() + " Roles"); XmlPersistenceHandler.logger.info("Read " + this.roleMap.size() + " Roles");
// validate we have a user with PrivilegeAdmin access // validate we have a user with PrivilegeAdmin access
boolean privilegeAdminExists = false; boolean privilegeAdminExists = false;
@ -247,7 +247,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
} }
if (!privilegeAdminExists) { if (!privilegeAdminExists) {
logger.warn("No User with role '" + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE XmlPersistenceHandler.logger.warn("No User with role '" + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE
+ "' exists. Privilege modifications will not be possible!"); + "' exists. Privilege modifications will not be possible!");
} }
@ -279,7 +279,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
this.modelPath = basePath + "/" + modelFileName; this.modelPath = basePath + "/" + modelFileName;
if (reload()) if (reload())
logger.info("Privilege Data loaded."); XmlPersistenceHandler.logger.info("Privilege Data loaded.");
} }
/** /**
@ -320,9 +320,9 @@ public class XmlPersistenceHandler implements PersistenceHandler {
for (Element roleElement : rolesElementList) { for (Element roleElement : rolesElementList) {
String roleName = roleElement.getTextTrim(); String roleName = roleElement.getTextTrim();
if (roleName.isEmpty()) { if (roleName.isEmpty()) {
logger.error("User " + username + " has a role defined with no name, Skipped."); XmlPersistenceHandler.logger.error("User " + username + " has a role defined with no name, Skipped.");
} else if (!this.roleMap.containsKey(roleName)) { } else if (!this.roleMap.containsKey(roleName)) {
logger.error("User " + username + " has a inexistant role " + roleName + ", Skipped."); XmlPersistenceHandler.logger.error("User " + username + " has a inexistant role " + roleName + ", Skipped.");
} else { } else {
roles.add(roleName); roles.add(roleName);
} }
@ -330,14 +330,14 @@ public class XmlPersistenceHandler implements PersistenceHandler {
// read properties // read properties
Element propertiesElement = userElement.element(XmlConstants.XML_PROPERTIES); Element propertiesElement = userElement.element(XmlConstants.XML_PROPERTIES);
Map<String, String> propertyMap = convertToPropertyMap(propertiesElement); Map<String, String> propertyMap = XmlPersistenceHandler.convertToPropertyMap(propertiesElement);
// create user // create user
User user = new User(userId, username, password, firstname, surname, userState, roles, locale, propertyMap); User user = new User(userId, username, password, firstname, surname, userState, roles, locale, propertyMap);
// put user in map // put user in map
userMap.put(username, user); userMap.put(username, user);
logger.info("Loaded user " + user); XmlPersistenceHandler.logger.info("Loaded user " + user);
} }
return userMap; return userMap;
@ -508,7 +508,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
roleElement.addAttribute(XmlConstants.XML_ATTR_NAME, role.getName()); roleElement.addAttribute(XmlConstants.XML_ATTR_NAME, role.getName());
// add all the privileges // add all the privileges
toDomPrivileges(roleElement, role.getPrivilegeMap()); XmlPersistenceHandler.toDomPrivileges(roleElement, role.getPrivilegeMap());
// add element to return list // add element to return list
rolesAsElements.add(roleElement); rolesAsElements.add(roleElement);

View File

@ -75,12 +75,12 @@ public class BootstrapConfigurationHelper {
Logger.getRootLogger().setLevel(Level.INFO); Logger.getRootLogger().setLevel(Level.INFO);
// get current directory // get current directory
path = System.getProperty("user.dir") + "/newConfig"; BootstrapConfigurationHelper.path = System.getProperty("user.dir") + "/newConfig";
// TODO ask user where to save configuration, default is pwd/newConfig/.... // TODO ask user where to save configuration, default is pwd/newConfig/....
// see if path already exists // see if path already exists
File pathF = new File(path); File pathF = new File(BootstrapConfigurationHelper.path);
if (pathF.exists()) { if (pathF.exists()) {
throw new RuntimeException("Path already exists: " + pathF.getAbsolutePath()); throw new RuntimeException("Path already exists: " + pathF.getAbsolutePath());
} }
@ -92,9 +92,9 @@ public class BootstrapConfigurationHelper {
// TODO ask other questions... // TODO ask other questions...
// now perform work: // now perform work:
createXmlPrivilegeContainer(); BootstrapConfigurationHelper.createXmlPrivilegeContainer();
createPolicyConfiguration(); BootstrapConfigurationHelper.createPolicyConfiguration();
createModel(); BootstrapConfigurationHelper.createModel();
} }
/** /**
@ -133,34 +133,34 @@ public class BootstrapConfigurationHelper {
// create PersistenceHandler // create PersistenceHandler
Element persistenceHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_PERSISTENCE); Element persistenceHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_PERSISTENCE);
containerElement.add(persistenceHandlerElem); containerElement.add(persistenceHandlerElem);
persistenceHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultPersistenceHandler); persistenceHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, BootstrapConfigurationHelper.defaultPersistenceHandler);
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS); parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
persistenceHandlerElem.add(parametersElement); persistenceHandlerElem.add(parametersElement);
// Parameter basePath // Parameter basePath
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER); parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_BASE_PATH); parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_BASE_PATH);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, basePath); parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.basePath);
parametersElement.add(parameterElement); parametersElement.add(parameterElement);
// Parameter modelXmlFile // Parameter modelXmlFile
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER); parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_MODEL_FILE); parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_MODEL_FILE);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, modelFileName); parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.modelFileName);
parametersElement.add(parameterElement); parametersElement.add(parameterElement);
// create EncryptionHandler // create EncryptionHandler
Element encryptionHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_ENCRYPTION); Element encryptionHandlerElem = factory.createElement(XmlConstants.XML_HANDLER_ENCRYPTION);
containerElement.add(encryptionHandlerElem); containerElement.add(encryptionHandlerElem);
encryptionHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, defaultEncryptionHandler); encryptionHandlerElem.addAttribute(XmlConstants.XML_ATTR_CLASS, BootstrapConfigurationHelper.defaultEncryptionHandler);
parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS); parametersElement = factory.createElement(XmlConstants.XML_PARAMETERS);
encryptionHandlerElem.add(parametersElement); encryptionHandlerElem.add(parametersElement);
// Parameter hashAlgorithm // Parameter hashAlgorithm
parameterElement = factory.createElement(XmlConstants.XML_PARAMETER); parameterElement = factory.createElement(XmlConstants.XML_PARAMETER);
parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_HASH_ALGORITHM); parameterElement.addAttribute(XmlConstants.XML_ATTR_NAME, XmlConstants.XML_PARAM_HASH_ALGORITHM);
parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, hashAlgorithm); parameterElement.addAttribute(XmlConstants.XML_ATTR_VALUE, BootstrapConfigurationHelper.hashAlgorithm);
parametersElement.add(parameterElement); parametersElement.add(parameterElement);
// write the container file to disk // write the container file to disk
File privilegeContainerFile = new File(path + "/" + defaultPrivilegeContainerXmlFile); File privilegeContainerFile = new File(BootstrapConfigurationHelper.path + "/" + BootstrapConfigurationHelper.defaultPrivilegeContainerXmlFile);
XmlHelper.writeDocument(doc, privilegeContainerFile); XmlHelper.writeDocument(doc, privilegeContainerFile);
} }
} }

View File

@ -33,14 +33,14 @@ public class CertificateThreadLocal extends ThreadLocal<Certificate> {
} }
public static CertificateThreadLocal getInstance() { public static CertificateThreadLocal getInstance() {
return instance; return CertificateThreadLocal.instance;
} }
public static Certificate getCert() { public static Certificate getCert() {
return instance.get(); return CertificateThreadLocal.instance.get();
} }
public static void setCert(Certificate certificate) { public static void setCert(Certificate certificate) {
instance.set(certificate); CertificateThreadLocal.instance.set(certificate);
} }
} }

View File

@ -54,7 +54,7 @@ public class HashHelper {
*/ */
public static String stringToHash(String hashAlgorithm, String string) throws NoSuchAlgorithmException, public static String stringToHash(String hashAlgorithm, String string) throws NoSuchAlgorithmException,
UnsupportedEncodingException { UnsupportedEncodingException {
return stringToHash(hashAlgorithm, string.getBytes()); return HashHelper.stringToHash(hashAlgorithm, string.getBytes());
} }
/** /**
@ -83,8 +83,8 @@ public class HashHelper {
for (byte b : hashArray) { for (byte b : hashArray) {
int v = b & 0xFF; int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4]; hex[index++] = HashHelper.HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF]; hex[index++] = HashHelper.HEX_CHAR_TABLE[v & 0xF];
} }
return new String(hex, "ASCII"); return new String(hex, "ASCII");

View File

@ -35,7 +35,6 @@ import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.i18n.PrivilegeException; import ch.eitchnet.privilege.i18n.PrivilegeException;
import ch.eitchnet.privilege.policy.PrivilegePolicy; import ch.eitchnet.privilege.policy.PrivilegePolicy;
import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.helper.SystemHelper;
/** /**
* This class implements the initializing of the {@link PrivilegeHandler} by loading an XML file containing the * This class implements the initializing of the {@link PrivilegeHandler} by loading an XML file containing the
@ -81,19 +80,19 @@ public class InitializationHelper {
// get policies // get policies
Element policiesElement = rootElement.element(XmlConstants.XML_POLICIES); Element policiesElement = rootElement.element(XmlConstants.XML_POLICIES);
Map<String, Class<PrivilegePolicy>> policyMap = convertToPolicyMap(policiesElement); Map<String, Class<PrivilegePolicy>> policyMap = InitializationHelper.convertToPolicyMap(policiesElement);
try { try {
// get parameters // get parameters
Element parameterElement = encryptionHandlerElement.element(XmlConstants.XML_PARAMETERS); Element parameterElement = encryptionHandlerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = convertToParameterMap(parameterElement); Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize encryption handler // initialize encryption handler
encryptionHandler.initialize(parameterMap); encryptionHandler.initialize(parameterMap);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, e); InitializationHelper.logger.error(e, e);
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
+ " could not be initialized"); + " could not be initialized");
} }
@ -102,13 +101,13 @@ public class InitializationHelper {
// get parameters // get parameters
Element parameterElement = persistenceHandlerElement.element(XmlConstants.XML_PARAMETERS); Element parameterElement = persistenceHandlerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = convertToParameterMap(parameterElement); Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize persistence handler // initialize persistence handler
persistenceHandler.initialize(parameterMap); persistenceHandler.initialize(parameterMap);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, e); InitializationHelper.logger.error(e, e);
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerElement throw new PrivilegeException("PersistenceHandler " + persistenceHandlerElement
+ " could not be initialized"); + " could not be initialized");
} }
@ -117,13 +116,13 @@ public class InitializationHelper {
// get parameters // get parameters
Element parameterElement = containerElement.element(XmlConstants.XML_PARAMETERS); Element parameterElement = containerElement.element(XmlConstants.XML_PARAMETERS);
Map<String, String> parameterMap = convertToParameterMap(parameterElement); Map<String, String> parameterMap = InitializationHelper.convertToParameterMap(parameterElement);
// initialize privilege handler // initialize privilege handler
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap); privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, e); InitializationHelper.logger.error(e, e);
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName() throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
+ " could not be initialized"); + " could not be initialized");
} }

View File

@ -69,7 +69,7 @@ public class XmlHelper {
SAXReader reader = new SAXReader(); SAXReader reader = new SAXReader();
Document document = reader.read(inStream); Document document = reader.read(inStream);
logger.info("Read XML document " + document.getRootElement().getName()); XmlHelper.logger.info("Read XML document " + document.getRootElement().getName());
return document; return document;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -89,7 +89,7 @@ public class XmlHelper {
*/ */
public static void writeDocument(Document document, File file) { public static void writeDocument(Document document, File file) {
logger.info("Exporting document element " + document.getName() + " to " + file.getAbsolutePath()); XmlHelper.logger.info("Exporting document element " + document.getName() + " to " + file.getAbsolutePath());
OutputStream fileOutputStream = null; OutputStream fileOutputStream = null;
@ -99,7 +99,7 @@ public class XmlHelper {
String aEncodingScheme = document.getXMLEncoding(); String aEncodingScheme = document.getXMLEncoding();
if (aEncodingScheme == null || aEncodingScheme.isEmpty()) { if (aEncodingScheme == null || aEncodingScheme.isEmpty()) {
aEncodingScheme = DEFAULT_ENCODING; aEncodingScheme = XmlHelper.DEFAULT_ENCODING;
} }
OutputFormat outformat = OutputFormat.createPrettyPrint(); OutputFormat outformat = OutputFormat.createPrettyPrint();
outformat.setEncoding(aEncodingScheme); outformat.setEncoding(aEncodingScheme);
@ -117,7 +117,7 @@ public class XmlHelper {
try { try {
fileOutputStream.close(); fileOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
logger.error("Could not close file output stream: " + e, e); XmlHelper.logger.error("Could not close file output stream: " + e, e);
} }
} }
} }
@ -133,10 +133,10 @@ public class XmlHelper {
*/ */
public static void writeElement(Element rootElement, File file) { public static void writeElement(Element rootElement, File file) {
Document document = DocumentFactory.getInstance().createDocument(DEFAULT_ENCODING); Document document = DocumentFactory.getInstance().createDocument(XmlHelper.DEFAULT_ENCODING);
document.setRootElement(rootElement); document.setRootElement(rootElement);
document.setName(rootElement.getName()); document.setName(rootElement.getName());
writeDocument(document, file); XmlHelper.writeDocument(document, file);
} }
} }

View File

@ -87,9 +87,9 @@ public class PrivilegeTest {
// initialize container // initialize container
String pwd = System.getProperty("user.dir"); String pwd = System.getProperty("user.dir");
File privilegeContainerXmlFile = new File(pwd + "/config/Privilege.xml"); File privilegeContainerXmlFile = new File(pwd + "/config/Privilege.xml");
privilegeHandler = InitializationHelper.initializeFromXml(privilegeContainerXmlFile); PrivilegeTest.privilegeHandler = InitializationHelper.initializeFromXml(privilegeContainerXmlFile);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, e); PrivilegeTest.logger.error(e, e);
throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e); throw new RuntimeException("Initialization failed: " + e.getLocalizedMessage(), e);
} }
@ -102,9 +102,9 @@ public class PrivilegeTest {
@Test @Test
public void testAuthenticationOk() throws Exception { public void testAuthenticationOk() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
private byte[] copyBytes(byte[] bytes) { private byte[] copyBytes(byte[] bytes) {
@ -120,9 +120,9 @@ public class PrivilegeTest {
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testFailAuthenticationNOk() throws Exception { public void testFailAuthenticationNOk() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_BAD)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_BAD));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -132,9 +132,9 @@ public class PrivilegeTest {
@Test(expected = PrivilegeException.class) @Test(expected = PrivilegeException.class)
public void testFailAuthenticationPWNull() throws Exception { public void testFailAuthenticationPWNull() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, null); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, null);
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -144,18 +144,18 @@ public class PrivilegeTest {
@Test @Test
public void testAddUserBobAsAdmin() throws Exception { public void testAddUserBobAsAdmin() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
// let's add a new user bob // let's add a new user bob
UserRep userRep = new UserRep("1", BOB, "Bob", "Newman", UserState.NEW, new HashSet<String>(), null, UserRep userRep = new UserRep("1", PrivilegeTest.BOB, "Bob", "Newman", UserState.NEW, new HashSet<String>(), null,
new HashMap<String, String>()); new HashMap<String, String>());
privilegeHandler.addOrReplaceUser(certificate, userRep, null); PrivilegeTest.privilegeHandler.addOrReplaceUser(certificate, userRep, null);
logger.info("Added user " + BOB); PrivilegeTest.logger.info("Added user " + PrivilegeTest.BOB);
// set bob's password // set bob's password
privilegeHandler.setUserPassword(certificate, BOB, copyBytes(PASS_BOB)); PrivilegeTest.privilegeHandler.setUserPassword(certificate, PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
logger.info("Set Bob's password"); PrivilegeTest.logger.info("Set Bob's password");
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -166,8 +166,8 @@ public class PrivilegeTest {
*/ */
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testFailAuthAsBob() throws Exception { public void testFailAuthAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -176,9 +176,9 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testEnableUserBob() throws Exception { public void testEnableUserBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
privilegeHandler.setUserState(certificate, BOB, UserState.ENABLED); PrivilegeTest.privilegeHandler.setUserState(certificate, PrivilegeTest.BOB, UserState.ENABLED);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -190,9 +190,9 @@ public class PrivilegeTest {
@Test(expected = PrivilegeException.class) @Test(expected = PrivilegeException.class)
public void testFailAuthUserBob() throws Exception { public void testFailAuthUserBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -201,13 +201,13 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testAddRole() throws Exception { public void testAddRole() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
Map<String, PrivilegeRep> privilegeMap = new HashMap<String, PrivilegeRep>(); Map<String, PrivilegeRep> privilegeMap = new HashMap<String, PrivilegeRep>();
RoleRep roleRep = new RoleRep(ROLE_USER, privilegeMap); RoleRep roleRep = new RoleRep(PrivilegeTest.ROLE_USER, privilegeMap);
privilegeHandler.addOrReplaceRole(certificate, roleRep); PrivilegeTest.privilegeHandler.addOrReplaceRole(certificate, roleRep);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -216,9 +216,9 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testAddRoleToBob() throws Exception { public void testAddRoleToBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
privilegeHandler.addRoleToUser(certificate, BOB, ROLE_USER); PrivilegeTest.privilegeHandler.addRoleToUser(certificate, PrivilegeTest.BOB, PrivilegeTest.ROLE_USER);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -227,8 +227,8 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testAuthAsBob() throws Exception { public void testAuthAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -241,15 +241,15 @@ public class PrivilegeTest {
public void testFailAddUserTedAsBob() throws Exception { public void testFailAddUserTedAsBob() throws Exception {
// auth as Bog // auth as Bog
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// let's add a new user Ted // let's add a new user Ted
UserRep userRep = new UserRep("1", TED, "Ted", "And then Some", UserState.NEW, new HashSet<String>(), null, UserRep userRep = new UserRep("1", PrivilegeTest.TED, "Ted", "And then Some", UserState.NEW, new HashSet<String>(), null,
new HashMap<String, String>()); new HashMap<String, String>());
privilegeHandler.addOrReplaceUser(certificate, userRep, null); PrivilegeTest.privilegeHandler.addOrReplaceUser(certificate, userRep, null);
logger.info("Added user " + TED); PrivilegeTest.logger.info("Added user " + PrivilegeTest.TED);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -259,10 +259,10 @@ public class PrivilegeTest {
@Test @Test
public void testAddAdminRoleToBob() throws Exception { public void testAddAdminRoleToBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
privilegeHandler.addRoleToUser(certificate, BOB, PrivilegeHandler.PRIVILEGE_ADMIN_ROLE); PrivilegeTest.privilegeHandler.addRoleToUser(certificate, PrivilegeTest.BOB, PrivilegeHandler.PRIVILEGE_ADMIN_ROLE);
logger.info("Added " + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE + " to " + ADMIN); PrivilegeTest.logger.info("Added " + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE + " to " + PrivilegeTest.ADMIN);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -272,18 +272,18 @@ public class PrivilegeTest {
@Test @Test
public void testAddUserTedAsBob() throws Exception { public void testAddUserTedAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// let's add a new user ted // let's add a new user ted
HashSet<String> roles = new HashSet<String>(); HashSet<String> roles = new HashSet<String>();
roles.add(ROLE_USER); roles.add(PrivilegeTest.ROLE_USER);
UserRep userRep = new UserRep("2", TED, "Ted", "Newman", UserState.ENABLED, roles, null, UserRep userRep = new UserRep("2", PrivilegeTest.TED, "Ted", "Newman", UserState.ENABLED, roles, null,
new HashMap<String, String>()); new HashMap<String, String>());
privilegeHandler.addOrReplaceUser(certificate, userRep, null); PrivilegeTest.privilegeHandler.addOrReplaceUser(certificate, userRep, null);
logger.info("Added user " + TED); PrivilegeTest.logger.info("Added user " + PrivilegeTest.TED);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -293,13 +293,13 @@ public class PrivilegeTest {
@Test @Test
public void testSetTedPwdAsBob() throws Exception { public void testSetTedPwdAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// set ted's password to default // set ted's password to default
privilegeHandler.setUserPassword(certificate, TED, copyBytes(PASS_DEF)); PrivilegeTest.privilegeHandler.setUserPassword(certificate, PrivilegeTest.TED, copyBytes(PrivilegeTest.PASS_DEF));
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -308,9 +308,9 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testTedChangesOwnPwd() throws Exception { public void testTedChangesOwnPwd() throws Exception {
Certificate certificate = privilegeHandler.authenticate(TED, copyBytes(PASS_DEF)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.TED, copyBytes(PrivilegeTest.PASS_DEF));
privilegeHandler.setUserPassword(certificate, TED, copyBytes(PASS_TED)); PrivilegeTest.privilegeHandler.setUserPassword(certificate, PrivilegeTest.TED, copyBytes(PrivilegeTest.PASS_TED));
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -319,8 +319,8 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testAuthAsTed() throws Exception { public void testAuthAsTed() throws Exception {
Certificate certificate = privilegeHandler.authenticate(TED, copyBytes(PASS_TED)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.TED, copyBytes(PrivilegeTest.PASS_TED));
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -330,13 +330,13 @@ public class PrivilegeTest {
@Test @Test
public void testPerformRestrictableAsAdmin() throws Exception { public void testPerformRestrictableAsAdmin() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// see if eitch can perform restrictable // see if eitch can perform restrictable
Restrictable restrictable = new TestRestrictable(); Restrictable restrictable = new TestRestrictable();
privilegeHandler.actionAllowed(certificate, restrictable); PrivilegeTest.privilegeHandler.actionAllowed(certificate, restrictable);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -347,15 +347,15 @@ public class PrivilegeTest {
*/ */
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testFailPerformRestrictableAsBob() throws Exception { public void testFailPerformRestrictableAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// see if bob can perform restrictable // see if bob can perform restrictable
Restrictable restrictable = new TestRestrictable(); Restrictable restrictable = new TestRestrictable();
try { try {
privilegeHandler.actionAllowed(certificate, restrictable); PrivilegeTest.privilegeHandler.actionAllowed(certificate, restrictable);
} finally { } finally {
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
} }
@ -366,10 +366,10 @@ public class PrivilegeTest {
@Test @Test
public void testAddAppRoleToBob() throws Exception { public void testAddAppRoleToBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
privilegeHandler.addRoleToUser(certificate, BOB, ROLE_APP_USER); PrivilegeTest.privilegeHandler.addRoleToUser(certificate, PrivilegeTest.BOB, PrivilegeTest.ROLE_APP_USER);
logger.info("Added " + ROLE_APP_USER + " to " + BOB); PrivilegeTest.logger.info("Added " + PrivilegeTest.ROLE_APP_USER + " to " + PrivilegeTest.BOB);
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
/** /**
@ -380,15 +380,15 @@ public class PrivilegeTest {
*/ */
@Test @Test
public void testPerformRestrictableAsBob() throws Exception { public void testPerformRestrictableAsBob() throws Exception {
Certificate certificate = privilegeHandler.authenticate(BOB, copyBytes(PASS_BOB)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.BOB, copyBytes(PrivilegeTest.PASS_BOB));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// see if bob can perform restrictable // see if bob can perform restrictable
Restrictable restrictable = new TestRestrictable(); Restrictable restrictable = new TestRestrictable();
try { try {
privilegeHandler.actionAllowed(certificate, restrictable); PrivilegeTest.privilegeHandler.actionAllowed(certificate, restrictable);
} finally { } finally {
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
} }
@ -402,10 +402,10 @@ public class PrivilegeTest {
public void testPerformSystemRestrictable() throws Exception { public void testPerformSystemRestrictable() throws Exception {
// create the action to be performed as a system user // create the action to be performed as a system user
TestSystemUserAction action = new TestSystemUserAction(privilegeHandler); TestSystemUserAction action = new TestSystemUserAction(PrivilegeTest.privilegeHandler);
// and then perform the action // and then perform the action
privilegeHandler.runAsSystem(SYSTEM_USER_ADMIN, action); PrivilegeTest.privilegeHandler.runAsSystem(PrivilegeTest.SYSTEM_USER_ADMIN, action);
} }
/** /**
@ -418,10 +418,10 @@ public class PrivilegeTest {
public void testPerformSystemRestrictableFailPrivilege() throws Exception { public void testPerformSystemRestrictableFailPrivilege() throws Exception {
// create the action to be performed as a system user // create the action to be performed as a system user
TestSystemUserActionDeny action = new TestSystemUserActionDeny(privilegeHandler); TestSystemUserActionDeny action = new TestSystemUserActionDeny(PrivilegeTest.privilegeHandler);
// and then perform the action // and then perform the action
privilegeHandler.runAsSystem(SYSTEM_USER_ADMIN, action); PrivilegeTest.privilegeHandler.runAsSystem(PrivilegeTest.SYSTEM_USER_ADMIN, action);
} }
/** /**
@ -433,13 +433,13 @@ public class PrivilegeTest {
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
public void testLoginSystemUser() throws Exception { public void testLoginSystemUser() throws Exception {
privilegeHandler.authenticate(SYSTEM_USER_ADMIN, SYSTEM_USER_ADMIN.getBytes()); PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.SYSTEM_USER_ADMIN, PrivilegeTest.SYSTEM_USER_ADMIN.getBytes());
} }
@Test @Test
public void testCertificateThreadLocal() { public void testCertificateThreadLocal() {
Certificate certificate = privilegeHandler.authenticate(ADMIN, copyBytes(PASS_ADMIN)); Certificate certificate = PrivilegeTest.privilegeHandler.authenticate(PrivilegeTest.ADMIN, copyBytes(PrivilegeTest.PASS_ADMIN));
org.junit.Assert.assertTrue("Certificate is null!", certificate != null); org.junit.Assert.assertTrue("Certificate is null!", certificate != null);
// set certificate into thread local // set certificate into thread local
@ -448,9 +448,9 @@ public class PrivilegeTest {
// see if bob can perform restrictable by returning certificate from CertificateThreadLocal // see if bob can perform restrictable by returning certificate from CertificateThreadLocal
Restrictable restrictable = new TestRestrictable(); Restrictable restrictable = new TestRestrictable();
try { try {
privilegeHandler.actionAllowed(CertificateThreadLocal.getInstance().get(), restrictable); PrivilegeTest.privilegeHandler.actionAllowed(CertificateThreadLocal.getInstance().get(), restrictable);
} finally { } finally {
privilegeHandler.invalidateSession(certificate); PrivilegeTest.privilegeHandler.invalidateSession(certificate);
} }
} }
} }

View File

@ -46,7 +46,7 @@ public class TestSystemUserAction implements SystemUserAction {
TestSystemRestrictable restrictable = new TestSystemRestrictable(); TestSystemRestrictable restrictable = new TestSystemRestrictable();
handler.actionAllowed(certificate, restrictable); this.handler.actionAllowed(certificate, restrictable);
} }
} }

View File

@ -45,6 +45,6 @@ public class TestSystemUserActionDeny implements SystemUserAction {
public void execute(Certificate certificate) { public void execute(Certificate certificate) {
TestRestrictable restrictable = new TestRestrictable(); TestRestrictable restrictable = new TestRestrictable();
handler.actionAllowed(certificate, restrictable); this.handler.actionAllowed(certificate, restrictable);
} }
} }