Ver a proveniência

Build random string as Sendgrid Subuser username

pull/61/head
Kristijan Mitrovic há 4 anos
ascendente
cometimento
ea803a2911
1 ficheiros alterados com 12 adições e 19 eliminações
  1. +12
    -19
      bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java

+ 12
- 19
bubble-server/src/main/java/bubble/cloud/email/SendgridSmtpEmailDriver.java Ver ficheiro

@@ -20,13 +20,12 @@ import static bubble.cloud.storage.StorageCryptStream.MIN_DISTINCT_LENGTH;
import static bubble.cloud.storage.StorageCryptStream.MIN_KEY_LENGTH;
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.cobbzilla.util.daemon.ZillaRuntime.die;
import static org.cobbzilla.util.daemon.ZillaRuntime.empty;
import static org.cobbzilla.util.daemon.ZillaRuntime.*;
import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON;
import static org.cobbzilla.util.http.HttpUtil.getResponse;
import static org.cobbzilla.util.json.JsonUtil.json;
import static org.cobbzilla.util.security.CryptoUtil.generatePassword;
import static org.cobbzilla.util.string.StringUtil.repeat;
import static org.cobbzilla.wizard.model.IdentifiableBase.DEFAULT_SHORT_ID_LENGTH;

/**
* Only to be used with Sendgrid account with Subusers supported!
@@ -40,13 +39,6 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
return SENDGRID_SMTP.equals(serviceHost);
}

/**
* Build username which will be used for Subuser created on SendGrid's service for specified Account's object.
*/
@NonNull private String sgUsername(@NonNull final CloudService delegatedService) {
return delegatedService.getShortId();
}

@Override @NonNull public CloudService setupDelegatedCloudService(@NonNull final BubbleConfiguration configuration,
@NonNull final CloudService parentService,
@NonNull final CloudService delegatedService) {
@@ -62,18 +54,17 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {

final Account accountWithDelegate = configuration.getBean(AccountDAO.class)
.findByUuid(delegatedService.getAccount());
final String user = sgUsername(delegatedService);
final String user = uuid().substring(0, DEFAULT_SHORT_ID_LENGTH);
String password = generatePassword(MIN_KEY_LENGTH, MIN_DISTINCT_LENGTH);
final CreateSubuserRequest data = new CreateSubuserRequest(user, accountWithDelegate.getEmail(), password,
new String[]{ sgIp });
doRequest("subusers", HttpMethods.POST, json(data));

delegatedService.setDelegated(null).setTemplate(false);
delegatedService.getCredentials()
.setParam(PARAM_USER, user)
.setParam(PARAM_PASSWORD, password)
.setParam(PARAM_PARENT_SERVICE, parentService.getUuid());
password = repeat("x", MIN_KEY_LENGTH); // Override password (in memory) just in case
final CloudCredentials credentials = delegatedService.getCredentials();
credentials.setParam(PARAM_USER, user)
.setParam(PARAM_PASSWORD, password)
.setParam(PARAM_PARENT_SERVICE, parentService.getUuid());
delegatedService.setCredentials(credentials).setDelegated(null).setTemplate(false);
return delegatedService;
}

@@ -90,8 +81,10 @@ public class SendgridSmtpEmailDriver extends SmtpEmailDriver {
.findByUuid(parentServiceUuid);
if (parentService == null) return;

final String sgUserToDelete = sgUsername(service);
doRequest("subusers/" + sgUserToDelete, HttpMethods.DELETE, null);
final String user = service.getCredentials().getParam(PARAM_USER);
if (empty(user)) return;

doRequest("subusers/" + user, HttpMethods.DELETE, null);
}

private String doRequest(@NonNull final String uri, @NonNull final String method, @Nullable final String json) {


Carregando…
Cancelar
Guardar