mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
fix authentication on api requests
This commit is contained in:
@@ -36,14 +36,18 @@ package sonia.scm.security;
|
|||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.*;
|
import static com.google.common.base.Preconditions.*;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,6 +59,14 @@ import javax.inject.Inject;
|
|||||||
public final class BearerTokenGenerator
|
public final class BearerTokenGenerator
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for BearerTokenGenerator
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(BearerTokenGenerator.class);
|
||||||
|
|
||||||
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new token generator.
|
* Constructs a new token generator.
|
||||||
*
|
*
|
||||||
@@ -84,16 +96,23 @@ public final class BearerTokenGenerator
|
|||||||
{
|
{
|
||||||
checkNotNull(user, "user is required");
|
checkNotNull(user, "user is required");
|
||||||
|
|
||||||
SecureKey key = keyResolver.getSecureKey(user.getName());
|
String username = user.getName();
|
||||||
|
|
||||||
|
String id = keyGenerator.createKey();
|
||||||
|
|
||||||
|
logger.trace("create new token {} for user {}", id, username);
|
||||||
|
|
||||||
|
SecureKey key = keyResolver.getSecureKey(username);
|
||||||
|
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
// TODO: should be configurable
|
// TODO: should be configurable
|
||||||
long expiration = TimeUnit.MILLISECONDS.convert(10, TimeUnit.HOURS);
|
long expiration = TimeUnit.MILLISECONDS.convert(10, TimeUnit.HOURS);
|
||||||
|
|
||||||
//J-
|
//J-
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.setSubject(user.getName())
|
.setSubject(username)
|
||||||
.setId(keyGenerator.createKey())
|
.setId(id)
|
||||||
.signWith(SignatureAlgorithm.HS256, key.getBytes())
|
.signWith(SignatureAlgorithm.HS256, key.getBytes())
|
||||||
.setIssuedAt(now)
|
.setIssuedAt(now)
|
||||||
.setExpiration(new Date(now.getTime() + expiration))
|
.setExpiration(new Date(now.getTime() + expiration))
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Filter to handle authentication for the rest api of SCM-Manager.
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
@@ -65,23 +66,16 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
public class ApiAuthenticationFilter extends AuthenticationFilter
|
public class ApiAuthenticationFilter extends AuthenticationFilter
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
/** login uri */
|
||||||
public static final String URI_LOGIN = "/api/rest/authentication/login";
|
public static final String URI_LOGIN = "/api/rest/authentication/login";
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
public static final String URI_LOGOUT = "/api/rest/authentication/logout";
|
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
public static final String URI_STATE = "/api/rest/authentication/state";
|
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs ...
|
* Constructs a new ApiAuthenticationFilter
|
||||||
*
|
*
|
||||||
*
|
* @param configuration scm main configuration
|
||||||
* @param configuration
|
* @param tokenGenerators web token generators
|
||||||
* @param tokenGenerators
|
|
||||||
*/
|
*/
|
||||||
@Inject
|
@Inject
|
||||||
public ApiAuthenticationFilter(ScmConfiguration configuration,
|
public ApiAuthenticationFilter(ScmConfiguration configuration,
|
||||||
@@ -93,12 +87,13 @@ public class ApiAuthenticationFilter extends AuthenticationFilter
|
|||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* The filter skips the authentication chain on the login resource, for all
|
||||||
|
* other resources the request is delegated to the
|
||||||
|
* {@link AuthenticationFilter}.
|
||||||
*
|
*
|
||||||
*
|
* @param request http servlet request
|
||||||
* @param request
|
* @param response http servlet response
|
||||||
* @param response
|
* @param chain filter chain
|
||||||
* @param chain
|
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws ServletException
|
* @throws ServletException
|
||||||
@@ -108,11 +103,8 @@ public class ApiAuthenticationFilter extends AuthenticationFilter
|
|||||||
HttpServletResponse response, FilterChain chain)
|
HttpServletResponse response, FilterChain chain)
|
||||||
throws IOException, ServletException
|
throws IOException, ServletException
|
||||||
{
|
{
|
||||||
|
// skip filter on login resource
|
||||||
// skip filter on authentication resource
|
if (request.getRequestURI().contains(URI_LOGIN))
|
||||||
if (request.getRequestURI().contains(URI_LOGIN)
|
|
||||||
|| request.getRequestURI().contains(URI_STATE)
|
|
||||||
|| request.getRequestURI().contains(URI_LOGOUT))
|
|
||||||
{
|
{
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
@@ -123,12 +115,12 @@ public class ApiAuthenticationFilter extends AuthenticationFilter
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* The filter process the chain on unauthorized requests and does not prompt
|
||||||
|
* for authentication.
|
||||||
*
|
*
|
||||||
*
|
* @param request http servlet request
|
||||||
* @param request
|
* @param response http servlet response
|
||||||
* @param response
|
* @param chain filter chain
|
||||||
* @param chain
|
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws ServletException
|
* @throws ServletException
|
||||||
|
|||||||
@@ -66,7 +66,8 @@
|
|||||||
<logger name="sonia.scm.web.cgi.DefaultCGIExecutor" level="DEBUG" />
|
<logger name="sonia.scm.web.cgi.DefaultCGIExecutor" level="DEBUG" />
|
||||||
|
|
||||||
<!-- shiro -->
|
<!-- shiro -->
|
||||||
<logger name="org.apache.shiro" level="TRACE" />
|
<logger name="org.apache.shiro" level="INFO" />
|
||||||
|
<logger name="org.apache.shiro.authc.pam.ModularRealmAuthenticator" level="DEBUG" />
|
||||||
|
|
||||||
<!-- svnkit -->
|
<!-- svnkit -->
|
||||||
<!--
|
<!--
|
||||||
|
|||||||
@@ -86,7 +86,8 @@
|
|||||||
|
|
||||||
<!-- shiro -->
|
<!-- shiro -->
|
||||||
<!--
|
<!--
|
||||||
<logger name="org.apache.shiro" level="TRACE" />
|
<logger name="org.apache.shiro" level="INFO" />
|
||||||
|
<logger name="org.apache.shiro.authc.pam.ModularRealmAuthenticator" level="DEBUG" />
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- svnkit -->
|
<!-- svnkit -->
|
||||||
|
|||||||
Reference in New Issue
Block a user