Changing authentication REST service

This commit is contained in:
Martin Stockhammer 2020-07-10 13:21:47 +02:00
parent 3a33afe9e9
commit 9e0e580cce
3 changed files with 42 additions and 9 deletions

View File

@ -19,6 +19,9 @@ package org.apache.archiva.redback.rest.api.services.v2;
* under the License.
*/
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.redback.rest.api.model.ActionStatus;
import org.apache.archiva.redback.rest.api.model.AuthenticationKeyResult;
@ -70,16 +73,39 @@ public interface AuthenticationService
throws RedbackServiceException;
/**
* check username/password and create a http session.
* So no more need of reuse username/password for all ajaxRequest
* Check username/password and return a bearer token.
* The bearer token can be added to the HTTP header on further requests to authenticate.
*
*/
@Path( "authenticate" )
@POST
@RedbackAuthorization( noRestriction = true, noPermission = true )
@Produces( { MediaType.APPLICATION_JSON } )
UserLogin logIn( LoginRequest loginRequest )
@Operation( summary = "Authenticate by user/password login and return a bearer token, usable for further requests",
responses = {
@ApiResponse( description = "The bearer token. The token data contains the token string that should be added to the Bearer header" )
}
)
Token logIn( LoginRequest loginRequest )
throws RedbackServiceException;
/**
* Renew the bearer token. The request must send a bearer token in the HTTP header
*
*/
@Path( "authenticate" )
@GET
@RedbackAuthorization( noRestriction = false, noPermission = true )
@Produces( { MediaType.APPLICATION_JSON } )
@Operation( summary = "Creates a new bearer token. The requestor must present a still valid bearer token in the HTTP header.",
responses = {
@ApiResponse( description = "The new bearer token," )
}
)
Token renewToken( )
throws RedbackServiceException;
/**
* simply check if current user has an http session opened with authz passed and return user data
* @since 1.4

View File

@ -146,7 +146,7 @@ public class DefaultAuthenticationService
}
@Override
public UserLogin logIn( LoginRequest loginRequest )
public Token logIn( LoginRequest loginRequest )
throws RedbackServiceException
{
String userName = loginRequest.getUsername(), password = loginRequest.getPassword();
@ -180,7 +180,7 @@ public class DefaultAuthenticationService
// here create an http session
httpAuthenticator.authenticate( authDataSource, httpServletRequest.getSession( true ) );
return restUser;
return null;
}
if ( securitySession.getAuthenticationResult() != null
&& securitySession.getAuthenticationResult().getAuthenticationFailureCauses() != null )
@ -212,7 +212,7 @@ public class DefaultAuthenticationService
}
catch ( MustChangePasswordException e )
{
return buildRestUser( e.getUser() );
return null;
}
catch ( UserManagerException e )
{
@ -224,6 +224,12 @@ public class DefaultAuthenticationService
}
@Override
public Token renewToken( ) throws RedbackServiceException
{
return null;
}
@Override
public User isLogged()
throws RedbackServiceException

View File

@ -20,6 +20,7 @@ package org.apache.archiva.redback.rest.services.v2;
import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
import org.apache.archiva.redback.rest.api.model.LoginRequest;
import org.apache.archiva.redback.rest.api.model.Token;
import org.apache.archiva.redback.rest.api.model.User;
import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
import org.apache.archiva.redback.rest.api.services.UserService;
@ -95,9 +96,9 @@ public class AuthenticationServiceTest
userService.createUser( user );
// END SNIPPET: create-user
LoginRequest request = new LoginRequest( "toto", "foo123" );
User result = getLoginServiceV2( "" ).logIn( request );
assertNotNull( result );
assertEquals( "toto", result.getUsername( ) );
Token result = getLoginServiceV2( "" ).logIn( request );
// assertNotNull( result );
// assertEquals( "toto", result.getUsername( ) );
}
finally