mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
use bearer authorization header for authentication instead of session cookie
This commit is contained in:
@@ -35,6 +35,8 @@ package sonia.scm.client;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -42,6 +44,7 @@ import sonia.scm.ScmState;
|
|||||||
import sonia.scm.url.UrlProvider;
|
import sonia.scm.url.UrlProvider;
|
||||||
import sonia.scm.url.UrlProviderFactory;
|
import sonia.scm.url.UrlProviderFactory;
|
||||||
import sonia.scm.util.AssertUtil;
|
import sonia.scm.util.AssertUtil;
|
||||||
|
import sonia.scm.util.HttpUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
@@ -51,18 +54,12 @@ import com.sun.jersey.api.client.ClientHandlerException;
|
|||||||
import com.sun.jersey.api.client.ClientRequest;
|
import com.sun.jersey.api.client.ClientRequest;
|
||||||
import com.sun.jersey.api.client.ClientResponse;
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
import com.sun.jersey.api.client.WebResource;
|
import com.sun.jersey.api.client.WebResource;
|
||||||
import com.sun.jersey.api.client.config.ClientConfig;
|
|
||||||
import com.sun.jersey.api.client.config.DefaultClientConfig;
|
import com.sun.jersey.api.client.config.DefaultClientConfig;
|
||||||
import com.sun.jersey.api.client.filter.ClientFilter;
|
import com.sun.jersey.api.client.filter.ClientFilter;
|
||||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||||
import com.sun.jersey.multipart.impl.MultiPartWriter;
|
import com.sun.jersey.multipart.impl.MultiPartWriter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.NewCookie;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -128,11 +125,10 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
Client client =
|
Client client =
|
||||||
Client.create(new DefaultClientConfig(MultiPartWriter.class));
|
Client.create(new DefaultClientConfig(MultiPartWriter.class));
|
||||||
|
|
||||||
client.addFilter(new CookieClientFilter());
|
boolean loginAttempt = isLoginAttempt(username, password);
|
||||||
|
|
||||||
ClientResponse response;
|
ClientResponse response;
|
||||||
|
|
||||||
if (Util.isNotEmpty(username) && Util.isNotEmpty(password))
|
if (loginAttempt)
|
||||||
{
|
{
|
||||||
response = login(urlProvider, client, username, password);
|
response = login(urlProvider, client, username, password);
|
||||||
}
|
}
|
||||||
@@ -151,25 +147,31 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
|
|
||||||
throw new ScmClientException("create ScmClientSession failed");
|
throw new ScmClientException("create ScmClientSession failed");
|
||||||
}
|
}
|
||||||
else if (logger.isInfoEnabled())
|
|
||||||
|
logger.info("create session successfully for user {}", user);
|
||||||
|
|
||||||
|
if (loginAttempt)
|
||||||
{
|
{
|
||||||
logger.info("create session successfully for user {}", user);
|
appendAuthenticationFilter(client, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JerseyClientSession(client, urlProvider, state);
|
return new JerseyClientSession(client, urlProvider, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void appendAuthenticationFilter(Client client, ScmState state)
|
||||||
* Method description
|
{
|
||||||
*
|
String token = state.getToken();
|
||||||
*
|
|
||||||
* @param urlProvider
|
if (Strings.isNullOrEmpty(token))
|
||||||
* @param client
|
{
|
||||||
* @param username
|
throw new ScmClientException(
|
||||||
* @param password
|
"scm-manager does not return a bearer token");
|
||||||
*
|
}
|
||||||
* @return
|
|
||||||
*/
|
// authentication for further requests
|
||||||
|
client.addFilter(new AuthenticationFilter(token));
|
||||||
|
}
|
||||||
|
|
||||||
private ClientResponse login(UrlProvider urlProvider, Client client,
|
private ClientResponse login(UrlProvider urlProvider, Client client,
|
||||||
String username, String password)
|
String username, String password)
|
||||||
{
|
{
|
||||||
@@ -197,15 +199,6 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
ClientResponse.class, formData);
|
ClientResponse.class, formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param urlProvider
|
|
||||||
* @param client
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private ClientResponse state(UrlProvider urlProvider, Client client)
|
private ClientResponse state(UrlProvider urlProvider, Client client)
|
||||||
{
|
{
|
||||||
String stateUrl = urlProvider.getStateUrl();
|
String stateUrl = urlProvider.getStateUrl();
|
||||||
@@ -226,18 +219,34 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
return resource.get(ClientResponse.class);
|
return resource.get(ClientResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
private boolean isLoginAttempt(String username, String password)
|
||||||
|
{
|
||||||
|
return Util.isNotEmpty(username) && Util.isNotEmpty(password);
|
||||||
|
}
|
||||||
|
|
||||||
//~--- inner classes --------------------------------------------------------
|
//~--- inner classes --------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class description
|
* Authentication filter
|
||||||
*
|
|
||||||
*
|
|
||||||
* @version Enter version here..., 14/07/05
|
|
||||||
* @author Enter your name here...
|
|
||||||
*/
|
*/
|
||||||
private class CookieClientFilter extends ClientFilter
|
private class AuthenticationFilter extends ClientFilter
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param bearerToken
|
||||||
|
*/
|
||||||
|
public AuthenticationFilter(String bearerToken)
|
||||||
|
{
|
||||||
|
this.bearerToken = bearerToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- methods ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -252,26 +261,16 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
public ClientResponse handle(ClientRequest request)
|
public ClientResponse handle(ClientRequest request)
|
||||||
throws ClientHandlerException
|
throws ClientHandlerException
|
||||||
{
|
{
|
||||||
for (NewCookie c : cookies)
|
request.getHeaders().putSingle(HttpUtil.HEADER_AUTHORIZATION,
|
||||||
{
|
HttpUtil.AUTHORIZATION_SCHEME_BEARER.concat(" ").concat(bearerToken));
|
||||||
request.getHeaders().putSingle("Cookie", c);
|
|
||||||
}
|
|
||||||
|
|
||||||
ClientResponse response = getNext().handle(request);
|
return getNext().handle(request);
|
||||||
|
|
||||||
if (response.getCookies() != null)
|
|
||||||
{
|
|
||||||
cookies.addAll(response.getCookies());
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields -------------------------------------------------------------
|
//~--- fields -------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private final List<NewCookie> cookies =
|
private final String bearerToken;
|
||||||
Collections.synchronizedList(new ArrayList<NewCookie>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -179,11 +179,11 @@ public class JerseyClientSession implements ScmClientSession
|
|||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private Client client;
|
private final Client client;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private ScmState state;
|
private final ScmState state;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private UrlProvider urlProvider;
|
private final UrlProvider urlProvider;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user