@@ -790,6 +790,61 @@ public ApiResponse roles(
790790 return ApiResponseFactory .createResponse (user );
791791 }
792792
793+ /**
794+ * API to change password for a user (by email)
795+ * This is supposed to be called from Auth0 custom connection.
796+ * @param email
797+ * @param password
798+ * @param request
799+ * @return
800+ * @throws Exception
801+ */
802+ @ POST
803+ @ Path ("/changePassword" )
804+ @ Timed
805+ public ApiResponse changePassword (
806+ @ FormParam ("email" ) String email ,
807+ @ FormParam ("password" ) String password ,
808+ @ Context HttpServletRequest request ) throws Exception {
809+
810+ logger .info ("auth0 change password request" );
811+
812+ if (Utils .isEmpty (email ))
813+ throw new APIRuntimeException (SC_BAD_REQUEST , String .format (MSG_TEMPLATE_MANDATORY , "email" ));
814+
815+ User user = userDao .findUserByEmail (email );
816+ user .setCredential (new Credential ());
817+ user .getCredential ().setPassword (password );
818+
819+ if (user ==null ) {
820+ throw new APIRuntimeException (SC_UNAUTHORIZED , "Credentials are incorrect." );
821+ }
822+
823+ String error = user .validatePassoword ();
824+ if (error != null ) {
825+ throw new APIRuntimeException (SC_BAD_REQUEST , error );
826+ }
827+
828+ User dbUser = null ;
829+ if (dbUser ==null && user .getEmail ()!=null ) {
830+ logger .debug (String .format ("Auth0: findUserByEmail(%s)" , user .getEmail ()));
831+ dbUser = this .userDao .findUserByEmail (user .getEmail ());
832+ }
833+
834+ if (dbUser ==null ) {
835+ throw new APIRuntimeException (SC_NOT_FOUND , MSG_TEMPLATE_USER_NOT_FOUND );
836+ }
837+
838+ if (dbUser .getCredential ()==null )
839+ dbUser .setCredential (new Credential ());
840+ dbUser .getCredential ().setPassword (user .getCredential ().getPassword ());
841+
842+ logger .debug (String .format ("Auth0: updating password for user: %s" , dbUser .getHandle ()));
843+ userDao .updatePassword (dbUser );
844+
845+ return ApiResponseFactory .createResponse ("password updated successfully." );
846+ }
847+
793848 //TODO: should be PATCH?
794849 @ PUT
795850 @ Path ("/activate" )
0 commit comments