rest-test enhancements

This commit is contained in:
Jason Rivard 2019-06-01 14:09:11 -04:00
parent b1d45313fe
commit 34396c3309
5 changed files with 228 additions and 46 deletions

View file

@ -0,0 +1,36 @@
/*
* Password Management Servlets (PWM)
* http://www.pwm-project.org
*
* Copyright (c) 2006-2009 Novell, Inc.
* Copyright (c) 2009-2018 The PWM Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package password.pwm.resttest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet(
name = "ExternalMacroTest",
urlPatterns = { "/macro", }
)
public class RestTestExternalMacroServlet extends HttpServlet
{
}

View file

@ -0,0 +1,64 @@
/*
* Password Management Servlets (PWM)
* http://www.pwm-project.org
*
* Copyright (c) 2006-2009 Novell, Inc.
* Copyright (c) 2009-2018 The PWM Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package password.pwm.resttest;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@WebServlet(
name = "RestTestExternalTokenDestinationServlet",
urlPatterns = { "/external-token-destination", }
)
public class RestTestExternalTokenDestinationServlet extends HttpServlet
{
@Override
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp )
throws ServletException, IOException
{
System.out.println( "--External Token Destination--" );
final InputStream inputStream = req.getInputStream();
final String body = IOUtils.toString( inputStream );
final JsonObject jsonObject = new JsonParser().parse( body ).getAsJsonObject();
final String email = jsonObject.getAsJsonObject( "tokenDestination" ).get( "email" ).getAsString();
final String sms = jsonObject.getAsJsonObject( "tokenDestination" ).get( "sms" ).getAsString();
final String displayValue = "YourTokenDestination";
resp.setHeader( "Content-Type", "application/json" );
final PrintWriter writer = resp.getWriter();
final String response = "{\"email\":\"" + email + "\",\"sms\":\"" + sms + "\",\"displayValue\":\"" + displayValue + "\"}";
writer.write( response );
writer.close();
}
}

View file

@ -0,0 +1,63 @@
/*
* Password Management Servlets (PWM)
* http://www.pwm-project.org
*
* Copyright (c) 2006-2009 Novell, Inc.
* Copyright (c) 2009-2018 The PWM Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package password.pwm.resttest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
@WebServlet(
name = "RestTestPasswordCheckServlet",
urlPatterns = { "/external-password-check" }
)
public class RestTestPasswordCheckServlet extends HttpServlet
{
@Override
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException
{
System.out.println( "REST TEST: --External Password Check--" );
final Gson gson = new Gson();
final Map<String, String> inputJson = gson.fromJson( RestTestUtilities.readRequestBodyAsString( req ), new TypeToken<Map<String, Object>>()
{
}.getType() );
final String inputPassword = inputJson.get( "password" );
final boolean error = inputPassword.contains( "aaa" );
final String errorMessage = error ? "TOO Many aaa's (REMOTE REST SERVICE)" : "No error. (REMOTE REST SERVICE)";
resp.setHeader( "Content-Type", "application/json" );
final PrintWriter writer = resp.getWriter();
final String response = "{\"error\":\"" + error + "\",\"errorMessage\":\"" + errorMessage + "\"}";
writer.write( response );
writer.close();
}
}

View file

@ -20,12 +20,9 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package password.pwm.resttest;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
@ -38,25 +35,18 @@ import java.io.InputStream;
import java.io.PrintWriter;
@WebServlet(
name = "NewUserServlet",
urlPatterns = { "/sms", "/macro", "/external-token-destination", "/external-password-check" }
name = "RestTestSmsGatewayServlet",
urlPatterns = { "/sms" }
)
public class ExternalMacroServlet extends HttpServlet
public class RestTestSmsGatewayServlet extends HttpServlet
{
private static final String USERNAME_PARAMETER = "username";
private static final String SUCCESSFUL = "true";
private static final String UNSUCCESSFUL = "false";
private static final String SMS_URL = "/sms";
private static final String MACRO_URL = "/macro";
private static final String EXTERNAL_TOKEN_DESTINATION_URL = "/external-token-destination";
private static final String EXTERNAL_PASSWORD_CHECK_URL = "/external-password-check";
@Override
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException
{
if ( req.getServletPath().equals( SMS_URL ) )
{
final SmsResponse instance = SmsResponse.getInstance();
final InputStream inputStream = req.getInputStream();
final String body = IOUtils.toString( inputStream );
@ -75,43 +65,11 @@ public class ExternalMacroServlet extends HttpServlet
final PrintWriter writer = resp.getWriter();
writer.write( "{\"output\":\"Message Received\"}" );
writer.close();
}
else if ( req.getServletPath().equals( EXTERNAL_TOKEN_DESTINATION_URL ) )
{
System.out.println( "External Token Destination" );
final InputStream inputStream = req.getInputStream();
final String body = IOUtils.toString( inputStream );
final JsonObject jsonObject = new JsonParser().parse( body ).getAsJsonObject();
final String email = jsonObject.getAsJsonObject( "tokenDestination" ).get( "email" ).getAsString();
final String sms = jsonObject.getAsJsonObject( "tokenDestination" ).get( "sms" ).getAsString();
final String displayValue = "YourTokenDestination";
resp.setHeader( "Content-Type", "application/json" );
final PrintWriter writer = resp.getWriter();
final String response = "{\"email\":\"" + email + "\",\"sms\":\"" + sms + "\",\"displayValue\":\"" + displayValue + "\"}";
writer.write( response );
writer.close();
}
else if ( req.getServletPath().equals( EXTERNAL_PASSWORD_CHECK_URL ) )
{
System.out.println( "External Password Check" );
final boolean error = false;
final String errorMessage = "No error.";
resp.setHeader( "Content-Type", "application/json" );
final PrintWriter writer = resp.getWriter();
final String response = "{\"error\":\"" + error + "\",\"errorMessage\":\"" + errorMessage + "\"}";
writer.write( response );
writer.close();
}
}
@Override
protected void doGet( final HttpServletRequest req, final HttpServletResponse resp ) throws IOException
{
if ( req.getServletPath().equals( SMS_URL ) )
{
//Check request
final SmsResponse instance = SmsResponse.getInstance();
final String requestUsername = req.getParameter( USERNAME_PARAMETER );
@ -136,5 +94,5 @@ public class ExternalMacroServlet extends HttpServlet
writer.close();
}
}
}

View file

@ -0,0 +1,61 @@
/*
* Password Management Servlets (PWM)
* http://www.pwm-project.org
*
* Copyright (c) 2006-2009 Novell, Inc.
* Copyright (c) 2009-2018 The PWM Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package password.pwm.resttest;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.Charset;
public class RestTestUtilities
{
public static String readRequestBodyAsString( final HttpServletRequest req )
throws IOException
{
final StringWriter stringWriter = new StringWriter();
final Reader readerStream = new InputStreamReader(
req.getInputStream(),
Charset.forName( "UTF8" )
);
try
{
IOUtils.copy( readerStream, stringWriter );
}
catch ( Exception e )
{
final String errorMsg = "error reading request body stream: " + e.getMessage();
throw new IOException( errorMsg );
}
finally
{
IOUtils.closeQuietly( readerStream );
}
return stringWriter.toString();
}
}