added DemoAuthenticator

This commit is contained in:
Sebastian Sdorra
2010-09-08 10:59:54 +02:00
parent f1619a098a
commit cbb85745f3
6 changed files with 424 additions and 27 deletions

View File

@@ -0,0 +1,139 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm;
//~--- JDK imports ------------------------------------------------------------
import java.io.Serializable;
import java.security.Principal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "users")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "name", "displayName", "mail" })
public class User implements Principal, Serializable
{
/** Field description */
private static final long serialVersionUID = -3089541936726329663L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public User() {}
/**
* Constructs ...
*
*
* @param name
* @param displayName
* @param mail
*/
public User(String name, String displayName, String mail)
{
this.name = name;
this.displayName = displayName;
this.mail = mail;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getDisplayName()
{
return displayName;
}
/**
* Method description
*
*
* @return
*/
public String getMail()
{
return mail;
}
/**
* Method description
*
*
* @return
*/
@Override
public String getName()
{
return name;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param displayName
*/
public void setDisplayName(String displayName)
{
this.displayName = displayName;
}
/**
* Method description
*
*
* @param mail
*/
public void setMail(String mail)
{
this.mail = mail;
}
/**
* Method description
*
*
*
* @param name
*/
public void setName(String name)
{
this.name = name;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String displayName;
/** Field description */
private String mail;
/** Field description */
private String name;
}

View File

@@ -29,6 +29,19 @@ public class ScmState
*/ */
public ScmState() {} public ScmState() {}
/**
* Constructs ...
*
*
* @param user
* @param repositoryTypes
*/
public ScmState(User user, RepositoryType[] repositoryTypes)
{
this.user = user;
this.repositoryTypes = repositoryTypes;
}
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
/** /**
@@ -48,9 +61,9 @@ public class ScmState
* *
* @return * @return
*/ */
public String getUsername() public User getUser()
{ {
return username; return user;
} }
/** /**
@@ -92,11 +105,11 @@ public class ScmState
* Method description * Method description
* *
* *
* @param username * @param user
*/ */
public void setUsername(String username) public void setUser(User user)
{ {
this.username = username; this.user = user;
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
@@ -109,5 +122,5 @@ public class ScmState
private boolean success = true; private boolean success = true;
/** Field description */ /** Field description */
private String username; private User user;
} }

View File

@@ -11,9 +11,12 @@ package sonia.scm.api.rest.resources;
import sonia.scm.RepositoryType; import sonia.scm.RepositoryType;
import sonia.scm.ScmState; import sonia.scm.ScmState;
import sonia.scm.User;
import sonia.scm.security.Authenticator;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -38,6 +41,15 @@ import javax.ws.rs.core.Response;
public class AuthenticationResource public class AuthenticationResource
{ {
/** Field description */
private static final RepositoryType[] types = new RepositoryType[] {
new RepositoryType("hg",
"Mercurial"),
new RepositoryType("svn", "Subversion"),
new RepositoryType("git", "Git") };
//~--- get methods ----------------------------------------------------------
/** /**
* Method description * Method description
* *
@@ -54,11 +66,11 @@ public class AuthenticationResource
@FormParam("password") String password) @FormParam("password") String password)
{ {
ScmState state = null; ScmState state = null;
User user = authenticator.authenticate(request, username, password);
if ("hans".equals(username) && "hans123".equals(password)) if (user != null)
{ {
request.getSession(true).setAttribute("auth", username); state = getState(user);
state = getState(username);
} }
else else
{ {
@@ -80,11 +92,11 @@ public class AuthenticationResource
public ScmState getState(@Context HttpServletRequest request) public ScmState getState(@Context HttpServletRequest request)
{ {
ScmState state = null; ScmState state = null;
String username = (String) request.getSession(true).getAttribute("auth"); User user = authenticator.getUser(request);
if (username != null) if (user != null)
{ {
state = getState(username); state = getState(user);
} }
else else
{ {
@@ -98,23 +110,24 @@ public class AuthenticationResource
* Method description * Method description
* *
* *
* @param username *
* @param user
* *
* @return * @return
*/ */
private ScmState getState(String username) private ScmState getState(User user)
{ {
ScmState state = new ScmState(); ScmState state = new ScmState();
state.setUsername(username); state.setUser(user);
RepositoryType[] types = new RepositoryType[] {
new RepositoryType("hg", "Mercurial"),
new RepositoryType("svn", "Subversion"),
new RepositoryType("git", "Git") };
state.setRepositoryTypes(types); state.setRepositoryTypes(types);
return state; return state;
} }
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private Authenticator authenticator;
} }

View File

@@ -0,0 +1,49 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.User;
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.http.HttpServletRequest;
/**
*
* @author Sebastian Sdorra
*/
public interface Authenticator
{
/**
* Method description
*
*
* @param request
* @param username
* @param password
*
* @return
*/
public User authenticate(HttpServletRequest request, String username,
String password);
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
public User getUser(HttpServletRequest request);
}

View File

@@ -0,0 +1,91 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.User;
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
*
* @author Sebastian Sdorra
*/
public class DemoAuthenticator implements Authenticator
{
/** Field description */
private static final String DEMO_DISPLAYNAME = "Hans am Schalter";
/** Field description */
private static final String DEMO_MAIL = "hans@schalter.de";
/** Field description */
private static final String DEMO_PASSWORD = "hans123";
/** Field description */
private static final String DEMO_USERNAME = "hans";
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
* @param username
* @param password
*
* @return
*/
@Override
public User authenticate(HttpServletRequest request, String username,
String password)
{
User user = null;
if (DEMO_USERNAME.equals(username) && DEMO_PASSWORD.equals(password))
{
user = new User(username, DEMO_DISPLAYNAME, DEMO_MAIL);
HttpSession session = request.getSession(true);
session.setAttribute(DemoAuthenticator.class.getName(), user);
}
return user;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
@Override
public User getUser(HttpServletRequest request)
{
User user = null;
HttpSession session = request.getSession();
if (session != null)
{
user = (User) session.getAttribute(DemoAuthenticator.class.getName());
}
return user;
}
}

View File

@@ -5,12 +5,20 @@
package sonia.scm.api.rest; package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.User;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.IOException; import java.io.IOException;
import java.security.Principal;
import javax.inject.Inject;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig; import javax.servlet.FilterConfig;
@@ -19,6 +27,7 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
/** /**
@@ -68,15 +77,23 @@ public class SecurityFilter implements Filter
String uri = String uri =
request.getRequestURI().substring(request.getContextPath().length()); request.getRequestURI().substring(request.getContextPath().length());
if (uri.startsWith(URL_AUTHENTICATION) if (!uri.startsWith(URL_AUTHENTICATION))
|| (request.getSession(true).getAttribute("auth") != null))
{ {
chain.doFilter(req, res); User user = authenticator.getUser(request);
if (user != null)
{
((HttpServletResponse) res).sendError(
HttpServletResponse.SC_UNAUTHORIZED);
}
else
{
chain.doFilter(new ScmHttpServletRequest(request, user), res);
}
} }
else else
{ {
((HttpServletResponse) res).sendError( chain.doFilter(req, res);
HttpServletResponse.SC_UNAUTHORIZED);
} }
} }
else else
@@ -99,4 +116,79 @@ public class SecurityFilter implements Filter
// do nothing // do nothing
} }
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 10/09/08
* @author Enter your name here...
*/
private static class ScmHttpServletRequest extends HttpServletRequestWrapper
{
/**
* Constructs ...
*
*
* @param request
* @param user
*/
public ScmHttpServletRequest(HttpServletRequest request, User user)
{
super(request);
this.user = user;
}
//~--- get methods --------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public String getRemoteUser()
{
return user.getName();
}
/**
* Method description
*
*
* @return
*/
public User getUser()
{
return user;
}
/**
* Method description
*
*
* @return
*/
@Override
public Principal getUserPrincipal()
{
return user;
}
//~--- fields -------------------------------------------------------------
/** Field description */
private User user;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private Authenticator authenticator;
} }