Commit de6c7679 authored by akucera's avatar akucera
Browse files

changes is authentication mechanism required by refactoring

parent fe3a882f
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
<?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>
+23 −0
Original line number Diff line number Diff line
<?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>
+21 −1
Original line number Diff line number Diff line
@@ -33,6 +33,12 @@
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
    </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>
+20 −28
Original line number Diff line number Diff line
@@ -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;
+0 −3
Original line number Diff line number Diff line
@@ -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>();
Loading