pax_global_header 0000666 0000000 0000000 00000000064 13412726345 0014521 g ustar 00root root 0000000 0000000 52 comment=4b9ec36a379d7993244e7055e862067e366beb5d
semanticBMS-master-CommonComponents/ 0000775 0000000 0000000 00000000000 13412726345 0020017 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/ 0000775 0000000 0000000 00000000000 13412726345 0023315 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/.gitignore 0000664 0000000 0000000 00000000025 13412726345 0025302 0 ustar 00root root 0000000 0000000 /target/
/.settings/
semanticBMS-master-CommonComponents/CommonComponents/pom.xml 0000664 0000000 0000000 00000004632 13412726345 0024637 0 ustar 00root root 0000000 0000000
4.0.0
cz.muni.fi.lasaris.sbms
CommonComponents
0.0.1-SNAPSHOT
jar
CommonComponents
https://gitlab.fi.muni.cz/xkucer16/semanticBMS
log4j
log4j
1.2.17
org.glassfish.jersey.core
jersey-server
${jersey.version}
javax.ws.rs
javax.ws.rs-api
2.0.1
junit
junit
4.12
test
cz.muni.fi.lasaris.sbms
ProviderInterfaces
0.0.1-SNAPSHOT
compile
org.apache.maven.plugins
maven-compiler-plugin
3.6.1
1.8
2.25.1
UTF-8
semanticBMS-master-CommonComponents/CommonComponents/src/ 0000775 0000000 0000000 00000000000 13412726345 0024104 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/ 0000775 0000000 0000000 00000000000 13412726345 0025030 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/ 0000775 0000000 0000000 00000000000 13412726345 0025751 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/ 0000775 0000000 0000000 00000000000 13412726345 0026365 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/ 0000775 0000000 0000000 00000000000 13412726345 0027335 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/ 0000775 0000000 0000000 00000000000 13412726345 0027733 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/lasaris/ 0000775 0000000 0000000 00000000000 13412726345 0031371 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/lasaris/sbms/ 0000775 0000000 0000000 00000000000 13412726345 0032335 5 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/lasaris/sbms/auth/ 0000775 0000000 0000000 00000000000 13412726345 0033276 5 ustar 00root root 0000000 0000000 AuthenticationFilter.java 0000664 0000000 0000000 00000010252 13412726345 0040207 0 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/lasaris/sbms/auth package cz.muni.fi.lasaris.sbms.auth;
import java.io.IOException;
import java.security.Principal;
import java.util.Properties;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.SecurityContext;
import org.apache.log4j.Logger;
import org.glassfish.jersey.internal.util.Base64;
//http://stackoverflow.com/questions/17068528/authorization-with-rolesalloweddynamicfeature-and-jersey
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
final static Logger logger = Logger.getLogger(AuthenticationFilter.class);
private AuthProvider ap;
private Properties props = null;
private String appName;
public AuthenticationFilter(String appName, Properties props) {
super();
this.props = props;
this.appName = appName;
if (props.getProperty("auth.enable") == null || !Boolean.parseBoolean(props.getProperty("auth.enable"))) {
logger.debug("Initing allowAll provider for app " + this.appName);
ap = getAllowAllProvider();
return;
}
try {
String className = this.props.getProperty("auth.provider");
if(className != null) {
Class> c = Class.forName(className);
this.ap = (AuthProvider)c.newInstance();
this.ap.init(this.props);
logger.debug("Initing custom auth provider for app " + this.appName);
} else {
throw new IllegalArgumentException(appName + ": customAuth is required but no AuthProvider implementation is provided.");
}
} catch(Exception ex) {
logger.error(ex);
logger.error(appName + ": Unable to load AuthProvider - allowing all users.");
ap = getAllowAllProvider();
}
}
public void filter(final ContainerRequestContext requestContext) throws IOException {
requestContext.setSecurityContext(new SecurityContext() {
private Principal user;
{
String auth = requestContext.getHeaderString("authorization");
final String[] creds = decodeAuth(auth);
if(ap.authenticate(creds[0], creds[1])) {
logger.debug(appName + ": Authenticated.");
user = new Principal() {
public String getName() {
return creds[0];
}
};
} else {
logger.debug(appName + ": Auth failed.");
user = null;
}
}
public Principal getUserPrincipal() {
return user;
}
public boolean isUserInRole(String role) {
if(getUserPrincipal() == null) {
return false;
}
String user = getUserPrincipal().getName();
if(ap.authorize(user, role)) {
logger.debug(appName + ": Authorized: " + user + " in " + role);
return true;
} else {
logger.debug(appName + ": Not authorized: " + user + " in " + role);
return false;
}
}
public boolean isSecure() {
return requestContext.getSecurityContext().isSecure();
}
public String getAuthenticationScheme() {
return requestContext.getSecurityContext().getAuthenticationScheme();
}
});
}
// https://simplapi.wordpress.com/2013/01/24/jersey-jax-rs-implements-a-http-basic-auth-decoder/
private String[] decodeAuth(String header) {
if (header == null) {
return new String[] {"", ""};
}
String auth = header.replaceFirst("[B|b]asic ", "");
//Decode the Base64 into byte[]
//Base64 decoder =
//byte[] decodedBytes = DatatypeConverter.parseBase64Binary(auth);
byte[] decodedBytes = Base64.decode(auth.getBytes());
//If the decode fails in any case
if(decodedBytes == null || decodedBytes.length == 0){
return null;
}
//Now we can convert the byte[] into a splitted array :
// - the first one is login,
// - the second one password
return new String(decodedBytes).split(":", 2);
}
private AuthProvider getAllowAllProvider() {
return new AuthProvider() {
public boolean authenticate(String user, String password) {
return true;
}
public boolean authorize(String user, String role) {
return true;
}
public void init(Properties props) {
}
};
}
}
PropertiesAuthProvider.java 0000664 0000000 0000000 00000002720 13412726345 0040554 0 ustar 00root root 0000000 0000000 semanticBMS-master-CommonComponents/CommonComponents/src/main/java/cz/muni/fi/lasaris/sbms/auth package cz.muni.fi.lasaris.sbms.auth;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class PropertiesAuthProvider implements AuthProvider {
Map passwords;
Map roles;
public PropertiesAuthProvider() {
}
public boolean authenticate(String user, String password) {
return passwords.containsKey(user) && passwords.get(user).equals(password);
}
public boolean authorize(String user, String role) {
return roles.containsKey("user") && roles.get(user).equals(role);
}
public void init(Properties props) {
this.passwords = new HashMap();
this.roles = new HashMap();
String usersProp = props.getProperty("auth.propertiesAuth.users");
String roleUProp = props.getProperty("auth.propertiesAuth.roles.user");
String roleAProp = props.getProperty("auth.propertiesAuth.roles.admin");
if(usersProp != null) {
String[] ups = usersProp.split("\\|");
for(int i = 0; i < ups.length; i++) {
String[] u = ups[i].split(":");
this.passwords.put(u[0],u[1]);
}
}
if(roleUProp != null) {
String[] rps = roleUProp.split("\\|");
for(int i = 0; i < rps.length; i++) {
this.roles.put(rps[i],"user");
}
}
if(roleAProp != null) {
String[] rps = roleAProp.split("\\|");
for(int i = 0; i < rps.length; i++) {
this.roles.put(rps[i],"admin");
}
}
}
}