Changing user REST interface V2

This commit is contained in:
Martin Stockhammer 2020-08-22 15:08:38 +02:00
parent 2c0876f7b4
commit d9fe4b0b8c
4 changed files with 63 additions and 63 deletions

View File

@ -239,12 +239,13 @@ public interface UserService
/** /**
* update only the current logged in user and this fields: fullname, email, password. * update only the current logged in user and this fields: fullname, email, password.
* The service verifies the current logged user with the one passed in the method * The service verifies the current logged user with the one passed in the method
* @return
*/ */
@Path( "me" ) @Path( "me" )
@PUT @PUT
@Produces( { MediaType.APPLICATION_JSON } ) @Produces( { MediaType.APPLICATION_JSON } )
@RedbackAuthorization( noPermission = true ) @RedbackAuthorization( noPermission = true )
ActionStatus updateMe( User user ) User updateMe( User user )
throws RedbackServiceException; throws RedbackServiceException;
@Path( "___ping___" ) @Path( "___ping___" )

View File

@ -220,6 +220,8 @@ public class BearerAuthInterceptor extends AbstractInterceptor
// message.put( AuthenticationResult.class, authenticationResult ); // message.put( AuthenticationResult.class, authenticationResult );
requestContext.setProperty( AUTHENTICATION_RESULT, authenticationResult ); requestContext.setProperty( AUTHENTICATION_RESULT, authenticationResult );
requestContext.setProperty( SECURITY_SESSION, securitySession ); requestContext.setProperty( SECURITY_SESSION, securitySession );
RedbackSecurityContext securityContext = new RedbackSecurityContext(requestContext.getUriInfo(), user, securitySession );
requestContext.setSecurityContext( securityContext );
return; return;
} }
catch ( AuthenticationException e ) catch ( AuthenticationException e )

View File

@ -83,8 +83,6 @@ public class DefaultAuthenticationService
@Context @Context
private SecurityContext securityContext; private SecurityContext securityContext;
private RedbackPrincipal redbackPrincipal;
@Context @Context
private ContainerRequestContext requestContext; private ContainerRequestContext requestContext;

View File

@ -47,7 +47,6 @@ import org.apache.archiva.redback.rest.api.model.ActionStatus;
import org.apache.archiva.redback.rest.api.model.v2.AvailabilityStatus; import org.apache.archiva.redback.rest.api.model.v2.AvailabilityStatus;
import org.apache.archiva.redback.rest.api.model.ErrorMessage; import org.apache.archiva.redback.rest.api.model.ErrorMessage;
import org.apache.archiva.redback.rest.api.model.Operation; import org.apache.archiva.redback.rest.api.model.Operation;
import org.apache.archiva.redback.rest.api.model.v2.PasswordStatus;
import org.apache.archiva.redback.rest.api.model.Permission; import org.apache.archiva.redback.rest.api.model.Permission;
import org.apache.archiva.redback.rest.api.model.v2.RegistrationKey; import org.apache.archiva.redback.rest.api.model.v2.RegistrationKey;
import org.apache.archiva.redback.rest.api.model.ResetPasswordRequest; import org.apache.archiva.redback.rest.api.model.ResetPasswordRequest;
@ -61,6 +60,7 @@ import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
import org.apache.archiva.redback.rest.api.services.v2.UserService; import org.apache.archiva.redback.rest.api.services.v2.UserService;
import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal; import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
import org.apache.archiva.redback.rest.services.RedbackRequestInformation; import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
import org.apache.archiva.redback.rest.services.interceptors.RedbackPrincipal;
import org.apache.archiva.redback.rest.services.utils.PasswordValidator; import org.apache.archiva.redback.rest.services.utils.PasswordValidator;
import org.apache.archiva.redback.role.RoleManager; import org.apache.archiva.redback.role.RoleManager;
import org.apache.archiva.redback.role.RoleManagerException; import org.apache.archiva.redback.role.RoleManagerException;
@ -82,7 +82,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -160,6 +162,9 @@ public class DefaultUserService
@Context @Context
private UriInfo uriInfo; private UriInfo uriInfo;
@Context
private SecurityContext securityContext;
@Inject @Inject
public DefaultUserService( @Named( value = "userManager#default" ) UserManager userManager, public DefaultUserService( @Named( value = "userManager#default" ) UserManager userManager,
SecuritySystem securitySystem ) SecuritySystem securitySystem )
@ -168,6 +173,15 @@ public class DefaultUserService
this.securitySystem = securitySystem; this.securitySystem = securitySystem;
} }
RedbackPrincipal getPrincipal() {
if (this.securityContext!=null) {
Principal pri = this.securityContext.getUserPrincipal( );
if (pri!=null && pri instanceof RedbackPrincipal) {
return (RedbackPrincipal) pri;
}
}
return null;
}
@Override @Override
public User createUser( User user ) public User createUser( User user )
@ -345,35 +359,20 @@ public class DefaultUserService
} }
@Override @Override
public ActionStatus updateMe( User user ) public User updateMe( User user )
throws RedbackServiceException throws RedbackServiceException
{ {
// check username == one in the session // check username == one in the session
RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
if ( redbackRequestInformation == null || redbackRequestInformation.getUser() == null ) RedbackPrincipal principal = getPrincipal( );
{ if (principal==null) {
log.warn( "RedbackRequestInformation from ThreadLocal is null" ); throw new RedbackServiceException( ErrorMessage.of( ERR_AUTH_UNAUTHORIZED_REQUEST ), 403 );
throw new RedbackServiceException( new ErrorMessage( "you must be logged to update your profile" ),
Response.Status.FORBIDDEN.getStatusCode() );
}
if ( user == null )
{
throw new RedbackServiceException( new ErrorMessage( "user parameter is mandatory" ),
Response.Status.BAD_REQUEST.getStatusCode() );
}
if ( !StringUtils.equals( redbackRequestInformation.getUser().getUsername(), user.getUserId() ) )
{
throw new RedbackServiceException( new ErrorMessage( "you can update only your profile" ),
Response.Status.FORBIDDEN.getStatusCode() );
} }
if ( StringUtils.isEmpty( user.getPreviousPassword() ) ) // check oldPassword with the current one
{ // only 3 fields to update
throw new RedbackServiceException( new ErrorMessage( "previous password is empty" ), // ui can limit to not update password
Response.Status.BAD_REQUEST.getStatusCode() ); org.apache.archiva.redback.users.User foundUser = updateUser( user.getUserId( ), realUser -> {
}
User realUser = getUser( user.getUserId() );
try try
{ {
String previousEncodedPassword = String previousEncodedPassword =
@ -386,18 +385,18 @@ public class DefaultUserService
if ( !encoder.isPasswordValid( previousEncodedPassword, user.getPreviousPassword( ) ) ) if ( !encoder.isPasswordValid( previousEncodedPassword, user.getPreviousPassword( ) ) )
{ {
throw new RedbackServiceException( new ErrorMessage( "password.provided.does.not.match.existing" ), return new RedbackServiceException( new ErrorMessage( "password.provided.does.not.match.existing" ),
Response.Status.BAD_REQUEST.getStatusCode( ) ); Response.Status.BAD_REQUEST.getStatusCode( ) );
} }
} }
catch ( UserNotFoundException e ) catch ( UserNotFoundException e )
{ {
throw new RedbackServiceException( new ErrorMessage( "user not found" ), return new RedbackServiceException( new ErrorMessage( "user not found" ),
Response.Status.BAD_REQUEST.getStatusCode( ) ); Response.Status.BAD_REQUEST.getStatusCode( ) );
} }
catch ( UserManagerException e ) catch ( UserManagerException e )
{ {
throw new RedbackServiceException( new ErrorMessage( e.getMessage() ) ); return new RedbackServiceException( ErrorMessage.of( ERR_USERMANAGER_FAIL, e.getMessage( ) ) );
} }
// only 3 fields to update // only 3 fields to update
realUser.setFullName( user.getFullName( ) ); realUser.setFullName( user.getFullName( ) );
@ -405,14 +404,12 @@ public class DefaultUserService
// ui can limit to not update password // ui can limit to not update password
if ( StringUtils.isNotBlank( user.getPassword( ) ) ) if ( StringUtils.isNotBlank( user.getPassword( ) ) )
{ {
passwordValidator.validatePassword( user.getPassword(), user.getUserId() );
realUser.setPassword( user.getPassword( ) ); realUser.setPassword( user.getPassword( ) );
} }
return null;
} );
updateUser( realUser.getUserId(), realUser ); return getRestUser( foundUser );
return ActionStatus.SUCCESS;
} }
@Override @Override
@ -1036,8 +1033,9 @@ public class DefaultUserService
} }
private void updateUser( String userId, Function<org.apache.archiva.redback.users.User, RedbackServiceException> updateFunction ) throws RedbackServiceException private org.apache.archiva.redback.users.User updateUser( String userId, Function<org.apache.archiva.redback.users.User, RedbackServiceException> updateFunction ) throws RedbackServiceException
{ {
try try
{ {
org.apache.archiva.redback.users.User rawUser = userManager.findUser( userId, false ); org.apache.archiva.redback.users.User rawUser = userManager.findUser( userId, false );
@ -1051,6 +1049,7 @@ public class DefaultUserService
} else { } else {
throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND, userId ), 404 ); throw new RedbackServiceException( ErrorMessage.of( ERR_USER_NOT_FOUND, userId ), 404 );
} }
return rawUser;
} }
catch ( UserNotFoundException e ) catch ( UserNotFoundException e )
{ {