change authentication parameters to match oauth spec

This commit is contained in:
Sebastian Sdorra
2017-02-09 20:52:53 +01:00
parent 1b8370fd1f
commit e0d42d7c92
9 changed files with 44 additions and 33 deletions

View File

@@ -194,6 +194,7 @@ public class JerseyClientProvider implements ScmClientProvider
formData.add("username", username); formData.add("username", username);
formData.add("password", password); formData.add("password", password);
formData.add("grant_type", "password");
return resource.type("application/x-www-form-urlencoded").post( return resource.type("application/x-www-form-urlencoded").post(
ClientResponse.class, formData); ClientResponse.class, formData);

View File

@@ -48,7 +48,7 @@ public class RestUrlProvider implements UrlProvider
public static final String PART_API = "api/rest/"; public static final String PART_API = "api/rest/";
/** Field description */ /** Field description */
public static final String PART_AUTHENTICATION = "authentication/login"; public static final String PART_AUTHENTICATION = "auth/access_token";
/** Field description */ /** Field description */
public static final String PART_CONFIG = "config"; public static final String PART_CONFIG = "config";
@@ -60,7 +60,7 @@ public class RestUrlProvider implements UrlProvider
public static final String PART_REPOSITORIES = "repositories"; public static final String PART_REPOSITORIES = "repositories";
/** Field description */ /** Field description */
public static final String PART_STATE = "authentication"; public static final String PART_STATE = "auth";
/** Field description */ /** Field description */
public static final String PART_USER = "users"; public static final String PART_USER = "users";

View File

@@ -76,7 +76,7 @@ public abstract class RestUrlProviderTestBase extends UrlProviderTestBase
@Override @Override
protected String getExpectedAuthenticationUrl(String baseUrl) protected String getExpectedAuthenticationUrl(String baseUrl)
{ {
return createRestUrl(baseUrl, "authentication/login"); return createRestUrl(baseUrl, "auth/access_token");
} }
/** /**
@@ -104,6 +104,6 @@ public abstract class RestUrlProviderTestBase extends UrlProviderTestBase
@Override @Override
protected String getExpectedStateUrl(String baseUrl) protected String getExpectedStateUrl(String baseUrl)
{ {
return createRestUrl(baseUrl, "authentication"); return createRestUrl(baseUrl, "auth");
} }
} }

View File

@@ -67,7 +67,6 @@ import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@@ -86,7 +85,7 @@ import sonia.scm.security.Scope;
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@Singleton @Singleton
@Path("authentication") @Path("auth")
@ExternallyManagedLifecycle @ExternallyManagedLifecycle
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public class AuthenticationResource public class AuthenticationResource
@@ -140,6 +139,7 @@ public class AuthenticationResource
* *
* @param request current http request * @param request current http request
* @param response current http response * @param response current http response
* @param grantType grant type, currently only password is supported
* @param username the username for the authentication * @param username the username for the authentication
* @param password the password for the authentication * @param password the password for the authentication
* @param cookie create authentication token * @param cookie create authentication token
@@ -148,19 +148,20 @@ public class AuthenticationResource
* @return * @return
*/ */
@POST @POST
@Path("login") @Path("access_token")
@TypeHint(ScmState.class) @TypeHint(ScmState.class)
public Response authenticate(@Context HttpServletRequest request, public Response authenticate(
@Context HttpServletRequest request,
@Context HttpServletResponse response, @Context HttpServletResponse response,
@FormParam("grant_type") String grantType,
@FormParam("username") String username, @FormParam("username") String username,
@FormParam("password") String password, @FormParam("password") String password,
@QueryParam("cookie") boolean cookie, @FormParam("cookie") boolean cookie,
@QueryParam("scope") List<String> scope) @FormParam("scope") List<String> scope)
{ {
Preconditions.checkArgument(!Strings.isNullOrEmpty(username), Preconditions.checkArgument(!Strings.isNullOrEmpty(grantType), "grant_type parameter is required");
"username parameter is required"); Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "username parameter is required");
Preconditions.checkArgument(!Strings.isNullOrEmpty(password), Preconditions.checkArgument(!Strings.isNullOrEmpty(password), "password parameter is required");
"password parameter is required");
Response res; Response res;
Subject subject = SecurityUtils.getSubject(); Subject subject = SecurityUtils.getSubject();
@@ -168,6 +169,7 @@ public class AuthenticationResource
try try
{ {
subject.login(Tokens.createAuthenticationToken(request, username, password)); subject.login(Tokens.createAuthenticationToken(request, username, password));
AccessTokenBuilder tokenBuilder = tokenBuilderFactory.create(); AccessTokenBuilder tokenBuilder = tokenBuilderFactory.create();
if ( scope != null ) { if ( scope != null ) {
tokenBuilder.scope(Scope.valueOf(scope)); tokenBuilder.scope(Scope.valueOf(scope));
@@ -211,8 +213,7 @@ public class AuthenticationResource
} }
else else
{ {
logger.warn("authentication failed, account {} is temporary locked", logger.warn("authentication failed, account {} is temporary locked", username);
username);
} }
res = handleFailedAuthentication(request, ex, Response.Status.FORBIDDEN, res = handleFailedAuthentication(request, ex, Response.Status.FORBIDDEN,
@@ -229,8 +230,7 @@ public class AuthenticationResource
logger.warn("authentication failed for user {}", username); logger.warn("authentication failed for user {}", username);
} }
res = handleFailedAuthentication(request, ex, res = handleFailedAuthentication(request, ex, Response.Status.UNAUTHORIZED,
Response.Status.UNAUTHORIZED,
WUIAuthenticationFailure.WRONG_CREDENTIALS); WUIAuthenticationFailure.WRONG_CREDENTIALS);
} }

View File

@@ -70,7 +70,7 @@ public class SecurityFilter extends HttpFilter
static final String ATTRIBUTE_REMOTE_USER = "principal"; static final String ATTRIBUTE_REMOTE_USER = "principal";
/** Field description */ /** Field description */
public static final String URL_AUTHENTICATION = "/api/rest/authentication"; public static final String URL_AUTHENTICATION = "/api/rest/auth";
//~--- constructors --------------------------------------------------------- //~--- constructors ---------------------------------------------------------

View File

@@ -61,7 +61,7 @@ Sonia.login.Form = Ext.extend(Ext.FormPanel,{
var config = { var config = {
labelWidth: 120, labelWidth: 120,
url: restUrl + "authentication/login.json?cookie=true", url: restUrl + "auth/access_token.json",
frame: true, frame: true,
title: this.titleText, title: this.titleText,
defaultType: 'textfield', defaultType: 'textfield',
@@ -93,6 +93,14 @@ Sonia.login.Form = Ext.extend(Ext.FormPanel,{
scope: this scope: this
} }
} }
}, {
name: 'grant_type',
value: 'password',
xtype: 'hidden'
}, {
name: 'cookie',
value: 'true',
xtype: 'hidden'
}], }],
buttons: buttons buttons: buttons
}; };
@@ -110,6 +118,7 @@ Sonia.login.Form = Ext.extend(Ext.FormPanel,{
authenticate: function(){ authenticate: function(){
var form = this.getForm(); var form = this.getForm();
form.submit({ form.submit({
scope: this, scope: this,
method: 'POST', method: 'POST',

View File

@@ -339,7 +339,7 @@ Sonia.scm.Main = Ext.extend(Ext.util.Observable, {
checkLogin: function(){ checkLogin: function(){
Ext.Ajax.request({ Ext.Ajax.request({
url: restUrl + 'authentication/state.json', url: restUrl + 'auth/state.json',
method: 'GET', method: 'GET',
scope: this, scope: this,
success: function(response){ success: function(response){
@@ -367,7 +367,7 @@ Sonia.scm.Main = Ext.extend(Ext.util.Observable, {
logout: function(){ logout: function(){
Ext.Ajax.request({ Ext.Ajax.request({
url: restUrl + 'authentication/logout.json', url: restUrl + 'auth/logout.json',
method: 'GET', method: 'GET',
scope: this, scope: this,
success: function(response){ success: function(response){

View File

@@ -108,15 +108,17 @@ public class AuthorizationScopeITCase {
private String createAuthenticationToken(String scope) { private String createAuthenticationToken(String scope) {
Client client = createClient(); Client client = createClient();
String url = createResourceUrl("authentication/login"); String url = createResourceUrl("auth/access_token");
if (!Strings.isNullOrEmpty(scope)) {
url = url.concat("?scope=").concat(scope);
}
WebResource wr = client.resource(url); WebResource wr = client.resource(url);
MultivaluedMap<String, String> formData = new MultivaluedMapImpl(); MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
formData.add("username", ADMIN_USERNAME); formData.add("username", ADMIN_USERNAME);
formData.add("password", ADMIN_PASSWORD); formData.add("password", ADMIN_PASSWORD);
formData.add("grant_type", "password");
if (!Strings.isNullOrEmpty(scope)) {
formData.add("scope", scope);
}
ClientResponse response = wr.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData); ClientResponse response = wr.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);
if (response.getStatus() >= 300 ){ if (response.getStatus() >= 300 ){

View File

@@ -110,17 +110,16 @@ public final class IntegrationTestUtil
* *
* @return * @return
*/ */
public static ClientResponse authenticate(Client client, String username, public static ClientResponse authenticate(Client client, String username, String password) {
String password) WebResource wr = client.resource(createResourceUrl("auth/access_token"));
{
WebResource wr = client.resource(createResourceUrl("authentication/login").concat("?cookie=true"));
MultivaluedMap<String, String> formData = new MultivaluedMapImpl(); MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
formData.add("username", username); formData.add("username", username);
formData.add("password", password); formData.add("password", password);
formData.add("cookie", "true");
formData.add("grant_type", "password");
return wr.type("application/x-www-form-urlencoded").post( return wr.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);
ClientResponse.class, formData);
} }
/** /**
@@ -294,7 +293,7 @@ public final class IntegrationTestUtil
*/ */
public static void logoutClient(Client client) public static void logoutClient(Client client)
{ {
WebResource wr = createResource(client, "authentication/logout"); WebResource wr = createResource(client, "auth/logout");
ClientResponse response = wr.get(ClientResponse.class); ClientResponse response = wr.get(ClientResponse.class);
assertNotNull(response); assertNotNull(response);