Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 4f8fca5

Browse files
author
sachin-maheshwari
authored
Merge pull request #41 from appirio-tech/dev
Shapeup3 - new endpoint "users/resendEmail"
2 parents 2b90094 + 55e42b0 commit 4f8fca5

File tree

7 files changed

+121
-4
lines changed

7 files changed

+121
-4
lines changed

.circleci/config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ maven_configuration_v1: &maven_configuration_v1
4343
sudo sed -i.bak "/<\/servers>/ s/.*/${C}\n&/" /opt/apache-maven/conf/settings.xml
4444
MVNSERVERSETTING='<server>\n <id>tcreposnap</id> \n <username>${env.MVNUSERNAME}</username> \n <password>${env.MVNPASSWD}</password> \n </server> '
4545
C=$(echo $MVNSERVERSETTING | sed 's/\//\\\//g')
46-
sudo sed -i.bak "/<\/servers>/ s/.*/${C}\n&/" /opt/apache-maven/conf/settings.xml
46+
sudo sed -i.bak "/<\/servers>/ s/.*/${C}\n&/" /opt/apache-maven/conf/settings.xml
47+
sudo sed -i '/<mirror>/,/<\/mirror>/d' /opt/apache-maven/conf/settings.xml
4748
4849
restore_mvn_offline: &restore_mvn_offline
4950
name: maven restore go offline
@@ -145,7 +146,7 @@ workflows:
145146
context : org-global
146147
filters:
147148
branches:
148-
only: [dev, 'feature/api-call-optimization']
149+
only: [dev, 'shapeup3']
149150
# Production build is executed on "master" branch only.
150151
- "build-prod":
151152
context : org-global

buildtokenproperties.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ AUTH_DB_HOST=$(eval "echo \$${ENV}_AUTH_DB_HOST")
4343
AUTH0_NEW_DOMAIN=$(eval "echo \$${ENV}_AUTH0_NEW_DOMAIN")
4444
AUTH0_DOMAIN=$(eval "echo \$${ENV}_AUTH0_DOMAIN")
4545

46+
SENDGRID_RESEND_ACTIVATION_EMAIL_TEMPLATE_ID=$(eval "echo \$${ENV}_SENDGRID_RESEND_ACTIVATION_EMAIL_TEMPLATE_ID")
47+
4648

4749

4850
if [[ -z "$ENV" ]] ; then
@@ -107,3 +109,4 @@ perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_READ\}\}|$M2MAUTHCONFIG_USERPROFIL
107109
perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_DELETE\}\}|$M2MAUTHCONFIG_USERPROFILES_DELETE|g" $CONFFILENAME
108110
perl -pi -e "s/\{\{AUTH0_NEW_DOMAIN\}\}/$AUTH0_NEW_DOMAIN/g" $CONFFILENAME
109111
perl -pi -e "s/\{\{AUTH0_DOMAIN\}\}/$AUTH0_DOMAIN/g" $CONFFILENAME
112+
perl -pi -e "s/\{\{SENDGRID_RESEND_ACTIVATION_EMAIL_TEMPLATE_ID\}\}/$SENDGRID_RESEND_ACTIVATION_EMAIL_TEMPLATE_ID/g" $CONFFILENAME

src/main/java/com/appirio/tech/core/service/identity/IdentityApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public void run(IdentityConfiguration configuration, Environment environment) th
233233
UserResource userResource = new UserResource(userDao, roleDao, cacheService, eventProducer, eventBusServiceClient, configuration.getM2mAuthConfiguration().getUserProfiles());
234234
userResource.setAuth0Client(configuration.getAuth0()); // TODO: constructor
235235
userResource.setDomain(configuration.getAuthDomain());
236+
userResource.setSendgridTemplateId(Utils.getString("sendGridTemplateId"));
236237
// this secret _used_ to be different from the one used in AuthorizationResource.
237238
// it _was_ the secret x2. (userResource.setSecret(getSecret()+getSecret());)
238239
// we assume this was done to further limit the usability of the oneTimeToken generated in userResource

src/main/java/com/appirio/tech/core/service/identity/clients/EventBusServiceClient.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public EventBusServiceClient(Client client, BaseClientConfiguration config, M2mA
4444
}
4545

4646
/**
47-
* Fire event
47+
* reFire event
4848
*
4949
* @param eventMessage the eventMessage to use
5050
*/
@@ -59,6 +59,30 @@ public void fireEvent(EventMessage eventMessage) {
5959
eventMessage.setTopic(this.config.getAdditionalConfiguration().get("topic"));
6060
Response response = request.header("Authorization", "Bearer " + authToken).post(Entity.entity(eventMessage.getData(), MediaType.APPLICATION_JSON_TYPE));
6161

62+
LOGGER.info("refiring event {}", new ObjectMapper().writer().writeValueAsString(eventMessage));
63+
if (response.getStatusInfo().getStatusCode() != HttpStatus.OK_200 && response.getStatusInfo().getStatusCode()!= HttpStatus.NO_CONTENT_204) {
64+
LOGGER.error("Unable to fire the event: {}", response);
65+
}
66+
} catch (Exception e) {
67+
LOGGER.error("Failed to fire the event: {}", e);
68+
}
69+
}
70+
71+
/**
72+
* Fire event
73+
*
74+
* @param eventMessage the eventMessage to use
75+
*/
76+
public void reFireEvent(EventMessage eventMessage) {
77+
try {
78+
String url = this.config.getEndpoint();
79+
WebTarget target = this.client.target(url);
80+
final Invocation.Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
81+
String authToken = Utils.generateAuthToken(m2mAuthConfiguration);
82+
83+
eventMessage.setOriginator(this.config.getAdditionalConfiguration().get("originator"));
84+
Response response = request.header("Authorization", "Bearer " + authToken).post(Entity.entity(eventMessage.getData(), MediaType.APPLICATION_JSON_TYPE));
85+
6286
LOGGER.info("Fire event {}", new ObjectMapper().writer().writeValueAsString(eventMessage));
6387
if (response.getStatusInfo().getStatusCode() != HttpStatus.OK_200 && response.getStatusInfo().getStatusCode()!= HttpStatus.NO_CONTENT_204) {
6488
LOGGER.error("Unable to fire the event: {}", response);

src/main/java/com/appirio/tech/core/service/identity/resource/UserResource.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.ArrayList;
1212
import java.util.HashMap;
1313
import java.util.List;
14+
import java.util.Map;
15+
import java.util.LinkedHashMap;
1416

1517
import javax.servlet.http.HttpServletRequest;
1618
import javax.servlet.http.HttpServletResponse;
@@ -100,6 +102,8 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
100102
private int oneTimeTokenExpirySeconds = 10 * 60; //10min
101103

102104
private String domain;
105+
106+
private String sendgridTemplateId;
103107

104108
protected UserDAO userDao;
105109

@@ -857,6 +861,78 @@ public ApiResponse changePassword(
857861
return ApiResponseFactory.createResponse("password updated successfully.");
858862
}
859863

864+
/**
865+
* API to resend activation email
866+
* This is supposed to be called from Auth0 custom connection.
867+
* @param email
868+
* @param request
869+
* @return
870+
* @throws Exception
871+
*/
872+
@POST
873+
@Path("/resendEmail")
874+
@Consumes("application/x-www-form-urlencoded")
875+
@Timed
876+
public ApiResponse resendEmail(
877+
@FormParam("email") String email,
878+
@FormParam("handle") String handle,
879+
@Context HttpServletRequest request) throws Exception {
880+
881+
if(Utils.isEmpty(email) && Utils.isEmpty(handle))
882+
throw new APIRuntimeException(SC_BAD_REQUEST, String.format(MSG_TEMPLATE_MANDATORY, "email/handle"));
883+
884+
User user = null;
885+
if (!Utils.isEmpty(handle)) {
886+
user = userDao.findUserByHandle(handle);
887+
} else {
888+
// email address - case sensitive - for auth0 sepecific
889+
user = userDao.findUserByEmailCS(email);
890+
}
891+
892+
if(user==null) {
893+
throw new APIRuntimeException(SC_UNAUTHORIZED, "Credentials are incorrect.");
894+
}
895+
896+
// return 400 if user has been activated
897+
if(user.isActive())
898+
throw new APIRuntimeException(SC_BAD_REQUEST, MSG_TEMPLATE_USER_ALREADY_ACTIVATED);
899+
900+
EventMessage msg = EventMessage.getDefault();
901+
msg.setTopic("external.action.email");
902+
903+
Map<String,Object> payload = new LinkedHashMap<String,Object>();
904+
905+
Map<String,Object> data = new LinkedHashMap<String,Object>();
906+
data.put("handle", user.getHandle());
907+
data.put("code", user.getCredential().getActivationCode());
908+
data.put("domain", getDomain());
909+
data.put("subDomain", "www");
910+
911+
if (user.getRegSource() != null && user.getRegSource().matches("tcBusiness")) {
912+
data.put("subDomain", "connect");
913+
}
914+
915+
payload.put("data", data);
916+
917+
Map<String,Object> from = new LinkedHashMap<String,Object>();
918+
from.put("email", String.format("Topcoder <noreply@%s>", getDomain()));
919+
payload.put("from", from);
920+
921+
payload.put("version", "v3");
922+
payload.put("sendgrid_template_id", this.getSendgridTemplateId());
923+
924+
ArrayList<String> recipients = new ArrayList<String>();
925+
recipients.add(user.getEmail());
926+
927+
payload.put("recipients", recipients);
928+
929+
msg.setPayload(payload);
930+
this.eventBusServiceClient.reFireEvent(msg);
931+
932+
return ApiResponseFactory.createResponse(user);
933+
}
934+
935+
860936
//TODO: should be PATCH?
861937
@PUT
862938
@Path("/activate")
@@ -1660,6 +1736,14 @@ public void setDomain(String domain) {
16601736
this.domain = domain;
16611737
}
16621738

1739+
public String getSendgridTemplateId() {
1740+
return sendgridTemplateId;
1741+
}
1742+
1743+
public void setSendgridTemplateId(String sendgridTemplateId) {
1744+
this.sendgridTemplateId = sendgridTemplateId;
1745+
}
1746+
16631747
public String getSecret() {
16641748
return secret;
16651749
}

src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ context:
1111
defaultPassword: @application.password.default@
1212
passwordHashKey: @application.password.key@
1313
ssoTokenSalt: @application.ssotoken.salt@
14+
sendGridTemplateId: @application.sendgrid.template.id@
1415
jwtExpirySeconds: 600
1516
cookieExpirySeconds: 7776000
1617

token.properties.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
@application.database.host@={{DB_HOST_IP}}
88
@application.database.port@=5432
99

10+
#sendgrid
11+
@application.sendgrid.template.id@={{SENDGRID_RESEND_ACTIVATION_EMAIL_TEMPLATE_ID}}
12+
1013
@ldap.host@={{LDAP_SERVER}}
1114
@ldap.port@=389
1215
@ldap.password@={{LDAP_PASSWORD}}
@@ -79,4 +82,4 @@
7982
@m2mAuthConfig.userProfiles.create@={{M2MAUTHCONFIG_USERPROFILES_CREATE}}
8083
@m2mAuthConfig.userProfiles.read@={{M2MAUTHCONFIG_USERPROFILES_READ}}
8184
@m2mAuthConfig.userProfiles.update@={{M2MAUTHCONFIG_USERPROFILES_UPDATE}}
82-
@m2mAuthConfig.userProfiles.delete@={{M2MAUTHCONFIG_USERPROFILES_DELETE}}
85+
@m2mAuthConfig.userProfiles.delete@={{M2MAUTHCONFIG_USERPROFILES_DELETE}}

0 commit comments

Comments
 (0)