Commit de6c7679 authored by akucera's avatar akucera

changes is authentication mechanism required by refactoring

parent fe3a882f
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CommonComponents</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
......@@ -32,7 +32,13 @@
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......@@ -41,6 +47,20 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
......@@ -9,9 +9,9 @@ import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.SecurityContext;
import javax.xml.bind.DatatypeConverter;
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)
......@@ -20,40 +20,37 @@ public class AuthenticationFilter implements ContainerRequestFilter {
private AuthProvider ap;
private static Properties props = null;
private Properties props = null;
private String appName;
public static void setProperties(Properties props) {
AuthenticationFilter.props = props;
}
public AuthenticationFilter() {
public AuthenticationFilter(String appName, Properties props) {
super();
this.props = props;
try {
String className = AuthenticationFilter.props.getProperty("auth.provider");
String className = this.props.getProperty("auth.provider");
if(className != null) {
Class<?> c = Class.forName(className);
ap = (AuthProvider)c.newInstance();
ap.init(props);
this.appName = appName;
this.ap = (AuthProvider)c.newInstance();
this.ap.init(this.props);
logger.debug("Initing custom auth provider for app " + appName);
} else {
throw new IllegalArgumentException("customAuth is required but no AuthProvider implementation is provided.");
throw new IllegalArgumentException(appName + ": customAuth is required but no AuthProvider implementation is provided.");
}
} catch(Exception ex) {
logger.error(ex);
logger.error("Unable to load AuthProvider - allowing all users.");
logger.error(appName + ": Unable to load AuthProvider - allowing all users.");
ap = new AuthProvider() {
@Override
public boolean authenticate(String user, String password, ContainerRequestContext requestContext) {
return true;
}
@Override
public boolean authorize(String user, String role, ContainerRequestContext requestContext) {
return true;
}
@Override
public void init(Properties props) {
}
......@@ -61,7 +58,6 @@ public class AuthenticationFilter implements ContainerRequestFilter {
}
}
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
requestContext.setSecurityContext(new SecurityContext() {
private Principal user;
......@@ -70,21 +66,20 @@ public class AuthenticationFilter implements ContainerRequestFilter {
String auth = requestContext.getHeaderString("authorization");
// no credentials provided
if(auth == null) {
logger.debug("No auth provided.");
logger.debug(appName + ": No auth provided.");
user = null;
} else {
final String[] creds = decodeAuth(auth);
if(ap.authenticate(creds[0], creds[1], requestContext)) {
logger.debug("Authenticated.");
logger.debug(appName + ": Authenticated.");
user = new Principal() {
@Override
public String getName() {
return creds[0];
}
};
} else {
logger.debug("Auth failed.");
logger.debug(appName + ": Auth failed.");
user = null;
}
}
......@@ -92,32 +87,28 @@ public class AuthenticationFilter implements ContainerRequestFilter {
@Override
public Principal getUserPrincipal() {
return user;
}
@Override
public boolean isUserInRole(String role) {
if(getUserPrincipal() == null) {
return false;
}
String user = getUserPrincipal().getName();
if(ap.authorize(user, role, requestContext)) {
logger.debug("Authorized: " + user + " in " + role);
logger.debug(appName + ": Authorized: " + user + " in " + role);
return true;
} else {
logger.debug("Not authorized: " + user + " in " + role);
logger.debug(appName + ": Not authorized: " + user + " in " + role);
return false;
}
}
@Override
public boolean isSecure() {
return requestContext.getSecurityContext().isSecure();
}
@Override
public String getAuthenticationScheme() {
return requestContext.getSecurityContext().getAuthenticationScheme();
}
......@@ -130,8 +121,9 @@ public class AuthenticationFilter implements ContainerRequestFilter {
String auth = header.replaceFirst("[B|b]asic ", "");
//Decode the Base64 into byte[]
byte[] decodedBytes = DatatypeConverter.parseBase64Binary(auth);
//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;
......
......@@ -15,17 +15,14 @@ public class PropertiesAuthProvider implements AuthProvider {
}
@Override
public boolean authenticate(String user, String password, ContainerRequestContext requestContext) {
return passwords.containsKey(user) && passwords.get(user).equals(password);
}
@Override
public boolean authorize(String user, String role, ContainerRequestContext requestContext) {
return roles.containsKey("user") && roles.get(user).equals(role);
}
@Override
public void init(Properties props) {
this.passwords = new HashMap<String, String>();
this.roles = new HashMap<String, String>();
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DataAccessAPI</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
......@@ -45,7 +45,9 @@ public class Application extends ResourceConfig {
register(RolesAllowedDynamicFeature.class);
if (prop.getProperty("auth.enable") != null && Boolean.parseBoolean(prop.getProperty("auth.enable"))) {
register(AuthenticationFilter.class);
//AuthenticationFilter.setProperties(prop);
//register(AuthenticationFilter.class);
registerInstances(new AuthenticationFilter("data", prop));
}
}
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SemanticAPI</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
......@@ -67,21 +67,6 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>cz.muni.fi.lasaris.sbms.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -45,8 +45,11 @@ public class Application extends ResourceConfig {
packages("cz.muni.fi.lasaris.sbms.semantics.api");
register(RolesAllowedDynamicFeature.class);
if (prop.getProperty("api.customAuth.enable") != null && Boolean.parseBoolean(prop.getProperty("api.customAuth.enable"))) {
register(AuthenticationFilter.class);
if (prop.getProperty("auth.enable") != null && Boolean.parseBoolean(prop.getProperty("auth.enable"))) {
//AuthenticationFilter.setProperties(prop);
//register(AuthenticationFilter.class);
registerInstances(new AuthenticationFilter("semantics", prop));
}
TdbConnector.setPaths((prop.getProperty("tdb.path") != null) ? prop.getProperty("tdb.path") : "./sbmstdb");
......
......@@ -121,7 +121,7 @@ public class DataPointsEndpoint {
}
}
//@RolesAllowed({"user","admin"})
@RolesAllowed({"user","admin"})
@GET
@Path("/{bmsId}")
@Produces(MediaType.APPLICATION_JSON)
......
......@@ -23,7 +23,7 @@
</div>
<h4>Console</h4>
<pre class="bg-info"><small id="logBatch">Ready</small></pre>
<pre class="bg-info"><span style="font-size:small" id="logBatch">Ready</span></pre>
</div>
</div>
\ No newline at end of file
......@@ -16,7 +16,7 @@
<button type="button" id="sendSingle" class="sendButton btn btn-primary">Send data</button></h3>
</div>
<div class="panel-body">
<pre id="console" class="bg-info"><small id="logSingle">Ready</small></pre>
<pre id="console" class="bg-info"><span style="font-size:small" id="logSingle">Ready</span></pre>
</div>
</div>
......
......@@ -43,7 +43,11 @@
var p = new SBMSPage({
noAuth: true,
init: function () {
init: function() {
p.showLoginDialog(p.initAfterLogin);
},
initAfterLogin: function () {
var name = p.getQueryVariable("name");
if(name) {
......@@ -120,7 +124,7 @@
url: query,
data: data,
beforeSend: function (xhr) {
p.setAuth(xhr);
p.setAuth(xhr);
},
success: function(result) {
if(!$.isEmptyObject(result.results) && result.groups.length != 0) {
......
......@@ -79,6 +79,40 @@ function SBMSPage(custom) {
$("#"+ id).html($.t("Finishing..."));
};
SBMSPage.prototype.showLoginDialog = function(func){
if(!$("#scripts_loginPrompt").length) {
var dialog = '<div id="scripts_loginPrompt" title="' + $.t('Login') + '">';
dialog += '<p>' + $.t('Enter your credentials please:') + '</p>';
dialog += '<div class="form-horizontal">'
dialog += '<div class="form-group">';
dialog += '<label class="col-lg-6 control-label" for="scripts_loginPromptUser">' + $.t('User:') + '</label>';
dialog += '<div class="col-lg-6"><input type="text" class="form-control" id="scripts_loginPromptUser"></div></div>';
dialog += '<div class="form-group">';
dialog += '<label class="col-lg-6 control-label" for="scripts_loginPromptPass">' + $.t('Password:') + '</label>';
dialog += '<div class="col-lg-6"><input type="text" class="form-control" id="scripts_loginPromptPass"></div></div>';
dialog += '</div></div>';
$("body").append(dialog);
$("#scripts_loginPrompt").hide();
}
$("#scripts_loginPrompt").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: [{
text: $.t("Login"),
click: function() {
$("#user").val($("#scripts_loginPromptUser").val());
$("#pass").val($("#scripts_loginPromptPass").val());
$( this ).dialog( "close" );
func();
}
}]
});
}
SBMSPage.prototype.loadList = function(type, func, params) {
this.loadList(type, func, params, null);
};
......
......@@ -124,15 +124,15 @@ makeTableFromCSV: function (data, target) {
<div class="form-group">
<textarea id="sparql" name="sparql" rows="20" class="form-control">
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX dul: <http://www.loa.istc.cnr.it/ontologies/DUL.owl#>
PREFIX sbim: <http://is.muni.cz/www/255658/sbms/v2_0/SemanticBIM#>
PREFIX sbms: <http://is.muni.cz/www/255658/sbms/v2_0/SemanticBMS#>
PREFIX sbmsd: <http://is.muni.cz/www/255658/sbms/v2_0/SemanticBMSData#>
PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX owl: &lt;http://www.w3.org/2002/07/owl#&gt;
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;
PREFIX ssn: &lt;http://purl.oclc.org/NET/ssnx/ssn#&gt;
PREFIX dul: &lt;http://www.loa.istc.cnr.it/ontologies/DUL.owl#&gt;
PREFIX sbim: &lt;http://is.muni.cz/www/255658/sbms/v2_0/SemanticBIM#&gt;
PREFIX sbms: &lt;http://is.muni.cz/www/255658/sbms/v2_0/SemanticBMS#&gt;
PREFIX sbmsd: &lt;http://is.muni.cz/www/255658/sbms/v2_0/SemanticBMSData#&gt;
SELECT *
WHERE {
......@@ -150,7 +150,7 @@ WHERE {
<span id="status" class="label label-success">Ready</span></h3>
</div>
<div class="form-group">
<div id="results" name="results"></div>
<div id="results"></div>
</div>
</form>
</div>
......
......@@ -22,6 +22,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
......@@ -11,12 +11,12 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<name>net.sourceforge.metrics.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sourceforge.metrics.builder</name>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
......
......@@ -6,7 +6,7 @@
<groupId>cz.muni.fi.lasaris.sbms</groupId>
<artifactId>SemanticBMSServer</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<version>1.0</version>
<name>SemanticBMSServer</name>
<dependencyManagement>
......@@ -37,14 +37,14 @@
</dependency>
<dependency>
<groupId>cz.muni.fi.lasaris</groupId>
<groupId>cz.muni.fi.lasaris.sbms</groupId>
<artifactId>SemanticAPI</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cz.muni.fi.lasaris</groupId>
<groupId>cz.muni.fi.lasaris.sbms</groupId>
<artifactId>DataAccessAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment