Delete Lambda prototype

Prototype the User DELETE lambda. Amplify does not include this functionality, so putting it in a Lambda with the fine-grained API. This currently does not invalidate outstanding tokens nor does it clean up the database.
@claytonwwilson You will want to add the clean up code instead of the deleted stuff in UserDeleter
This commit is contained in:
NMerz 2020-10-05 12:29:28 -04:00
parent 1907dc6ce5
commit fe846fb81f
11 changed files with 158 additions and 9 deletions

View File

@ -0,0 +1,3 @@
{
"userPoolId": "us-east-2_MFgSVKQMd",
}

View File

@ -0,0 +1,11 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
public class UserDELETE implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, UserDeleter.class);
}
}

View File

@ -0,0 +1,59 @@
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder;
import com.amazonaws.services.cognitoidp.model.AdminDeleteUserRequest;
import com.amazonaws.services.cognitoidp.model.AdminUserGlobalSignOutRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class UserDeleter implements CallHandler {
private DBConnector connector;
private String cognitoID;
//private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);";
public UserDeleter(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
AWSCognitoIdentityProvider awsCognitoIdentityProvider = AWSCognitoIdentityProviderClientBuilder.defaultClient();
Properties cognitoProperties;
try {
cognitoProperties = DBConnector.loadProperties("cognitoProperties.json");
} catch (IOException e) {
e.printStackTrace();
return null;
}
String userPoolId = cognitoProperties.get("userPoolId").toString();
System.out.println(userPoolId);
AdminDeleteUserRequest adminDeleteUserRequest = new AdminDeleteUserRequest().withUserPoolId(userPoolId);
adminDeleteUserRequest.setUsername(cognitoID);
System.out.println(adminDeleteUserRequest);
awsCognitoIdentityProvider.adminDeleteUser(adminDeleteUserRequest);
AdminUserGlobalSignOutRequest adminUserGlobalSignOutRequest = new AdminUserGlobalSignOutRequest().withUserPoolId(userPoolId);
adminUserGlobalSignOutRequest.setUsername(cognitoID);
System.out.println(adminUserGlobalSignOutRequest);
awsCognitoIdentityProvider.adminUserGlobalSignOut(adminUserGlobalSignOutRequest);
return null;
// Connection connection = connector.getConnection();
// try {
// PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST);
// statement.setInt(1, (Integer) bodyMap.get("ProductID"));
// statement.setInt(2, (Integer) bodyMap.get("ListID"));
// System.out.println(statement);
// statement.executeUpdate();
// connection.commit();
// } finally {
// connection.close();
// }
// return null;
}
}

View File

@ -42,13 +42,53 @@
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.5</version> <version>4.5.12</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.8.6</version> <version>2.8.6</version>
</dependency> </dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cognitoidentity</artifactId>
<version>1.11.875</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cognitoidp</artifactId>
<version>1.11.875</version>
</dependency>
<dependency>
<groupId>software.amazon.ion</groupId>
<artifactId>ion-java</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.source>1.11</maven.compiler.source> <maven.compiler.source>1.11</maven.compiler.source>

Binary file not shown.

View File

@ -0,0 +1,5 @@
#Generated by Maven
#Mon Oct 05 10:19:24 EDT 2020
groupId=groupId
artifactId=Lists
version=1.0-SNAPSHOT

View File

@ -0,0 +1,4 @@
/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/CallHandler.java
/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/DBConnector.java
/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/BasicHandler.java
/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/InputUtils.java

View File

@ -8,6 +8,7 @@ import com.amplifyframework.auth.options.AuthSignUpOptions;
import com.amplifyframework.auth.result.AuthSignInResult; import com.amplifyframework.auth.result.AuthSignInResult;
import com.amplifyframework.auth.result.AuthSignUpResult; import com.amplifyframework.auth.result.AuthSignUpResult;
import com.amplifyframework.core.Amplify; import com.amplifyframework.core.Amplify;
import com.example.listify.data.User;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -85,6 +86,10 @@ public class AuthManager {
waiting = false; waiting = false;
} }
public void signOutSuccess() {
waiting = false;
}
public void startSignUp(String email, String password) throws AuthException { public void startSignUp(String email, String password) throws AuthException {
this.email = email; this.email = email;
this.password = password; this.password = password;
@ -124,6 +129,11 @@ public class AuthManager {
throwIfAuthError(); throwIfAuthError();
} }
public void deleteUser(Requestor requestor) {
requestor.deleteObject("N/A", User.class);
Amplify.Auth.signOut(this::signOutSuccess, error -> setAuthError(error));
}
public static Properties loadProperties(Context context, String path) throws IOException, JSONException { public static Properties loadProperties(Context context, String path) throws IOException, JSONException {

View File

@ -14,8 +14,8 @@ import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI; import androidx.navigation.ui.NavigationUI;
import com.amplifyframework.auth.AuthException; import com.amplifyframework.auth.AuthException;
import com.example.listify.data.Item; import com.example.listify.data.Item;
import com.example.listify.data.ListEntry;
import com.example.listify.data.List; import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import org.json.JSONException; import org.json.JSONException;
@ -60,7 +60,7 @@ public class MainActivity extends AppCompatActivity {
//------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------//
boolean testAPI = false; boolean testAPI = true;
//----------------------------------API Testing---------------------------------------------// //----------------------------------API Testing---------------------------------------------//
if (testAPI) { if (testAPI) {
@ -78,6 +78,9 @@ public class MainActivity extends AppCompatActivity {
} }
Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey")); Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey"));
authManager.deleteUser(requestor);
//The name is the only part of this that is used, the rest is generated by the Lambda. //The name is the only part of this that is used, the rest is generated by the Lambda.
List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli()); List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli());
//Everything except addedDate is used for ItemEntry //Everything except addedDate is used for ItemEntry

View File

@ -24,20 +24,30 @@ public class Requestor {
client = new OkHttpClient(); client = new OkHttpClient();
} }
public <T> void getObject(String id, Class<T> classType, Receiver<T> receiver) {
getObject(id, classType, receiver, null);
}
public <T> void getListOfIds(Class<T> ofType, Receiver<Integer[]> successHandler, RequestErrorHandler failureHandler) { public <T> void getListOfIds(Class<T> ofType, Receiver<Integer[]> successHandler, RequestErrorHandler failureHandler) {
String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?id=-1"; String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?id=-1";
Request postRequest = buildBaseRequest(getURL, "GET", null); Request postRequest = buildBaseRequest(getURL, "GET", null);
launchCall(postRequest, successHandler, Integer[].class, failureHandler); launchCall(postRequest, successHandler, Integer[].class, failureHandler);
} }
public <T> void getObject(String id, Class<T> classType, Receiver<T> receiver) {
getObject(id, classType, receiver, null);
}
public <T> void getObject(String id, Class<T> classType, Receiver<T> successHandler, RequestErrorHandler failureHandler) { public <T> void getObject(String id, Class<T> classType, Receiver<T> successHandler, RequestErrorHandler failureHandler) {
String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id;
Request postRequest = buildBaseRequest(getURL, "GET", null); Request getRequest = buildBaseRequest(getURL, "GET", null);
launchCall(postRequest, successHandler, classType, failureHandler); launchCall(getRequest, successHandler, classType, failureHandler);
}
public void deleteObject(String id, Class classType) {
deleteObject(id, classType, null);
}
public void deleteObject(String id, Class classType, RequestErrorHandler failureHandler) {
String deleteURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id;
Request deleteRequest = buildBaseRequest(deleteURL, "DELETE", "{}");
launchCall(deleteRequest, null, classType, failureHandler);
} }
public void postObject(Object toPost) throws JSONException { public void postObject(Object toPost) throws JSONException {

View File

@ -0,0 +1,4 @@
package com.example.listify.data;
public class User {
}