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

Commit 36d91b9

Browse files
committed
F2F fix
1 parent 506da65 commit 36d91b9

File tree

12 files changed

+230
-16
lines changed

12 files changed

+230
-16
lines changed

buildtokenproperties.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ AUTH_SECRET=$(eval "echo \$${ENV}_AUTH_SECRET")
3232
EVENTBUSSERVICE_ENDPOINT=$(eval "echo \$${ENV}_EVENTBUSSERVICE_ENDPOINT")
3333
EVENTBUSSERVICE_TOPIC=$(eval "echo \$${ENV}_EVENTBUSSERVICE_TOPIC")
3434
EVENTBUSSERVICE_ORIGINATOR=$(eval "echo \$${ENV}_EVENTBUSSERVICE_ORIGINATOR")
35+
MEMBERSERVICE_ENDPOINT=$(eval "echo \$${ENV}_MEMBERSERVICE_ENDPOINT")
3536
M2MAUTHCONFIG_CID=$(eval "echo \$${ENV}_M2MAUTHCONFIG_CID")
3637
M2MAUTHCONFIG_SECRET=$(eval "echo \$${ENV}_M2MAUTHCONFIG_SECRET")
3738
M2MAUTHCONFIG_AUDIENCE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_AUDIENCE")
@@ -120,6 +121,7 @@ perl -pi -e "s/\{\{AUTH_DB_PASSWORD\}\}/$AUTH_DB_PASSWORD/g" $CONFFILENAME
120121
perl -pi -e "s|\{\{EVENTBUSSERVICE_ENDPOINT\}\}|$EVENTBUSSERVICE_ENDPOINT|g" $CONFFILENAME
121122
perl -pi -e "s/\{\{EVENTBUSSERVICE_TOPIC\}\}/$EVENTBUSSERVICE_TOPIC/g" $CONFFILENAME
122123
perl -pi -e "s/\{\{EVENTBUSSERVICE_ORIGINATOR\}\}/$EVENTBUSSERVICE_ORIGINATOR/g" $CONFFILENAME
124+
perl -pi -e "s|\{\{MEMBERSERVICE_ENDPOINT\}\}|$MEMBERSERVICE_ENDPOINT|g" $CONFFILENAME
123125
perl -pi -e "s/\{\{M2MAUTHCONFIG_CID\}\}/$M2MAUTHCONFIG_CID/g" $CONFFILENAME
124126
perl -pi -e "s/\{\{M2MAUTHCONFIG_SECRET\}\}/$M2MAUTHCONFIG_SECRET/g" $CONFFILENAME
125127
#perl -pi -e "s/\{\{M2MAUTHCONFIG_AUDIENCE\}\}/$M2MAUTHCONFIG_AUDIENCE/g" $CONFFILENAME

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.appirio.tech.core.api.v3.dropwizard.APIApplication;
3636
import com.appirio.tech.core.api.v3.util.jdbi.TCIDArgumentFactory;
3737
import com.appirio.tech.core.service.identity.clients.EventBusServiceClient;
38+
import com.appirio.tech.core.service.identity.clients.MemberServiceClient;
3839
import com.appirio.tech.core.service.identity.dao.ClientDAO;
3940
import com.appirio.tech.core.service.identity.dao.ExternalAccountDAO;
4041
import com.appirio.tech.core.service.identity.dao.GroupDAO;
@@ -196,6 +197,10 @@ public void run(IdentityConfiguration configuration, Environment environment) th
196197
final DBIFactory authDBIFactory = new DBIFactory();
197198
final DBI authjdbi = authDBIFactory.build(environment, configuration.getAuthorizationDatabase(), "Authorization");
198199
authjdbi.registerArgumentFactory(new TCIDArgumentFactory());
200+
201+
final Client apiClient = new JerseyClientBuilder(environment).using(new JerseyClientConfiguration())
202+
.build(getName());
203+
199204
// configure shiro
200205
Shiro shiroSettings = configuration.getShiroSettings();
201206
RoleDAO roleDao = null; // RoleDAO for AuthorizationResource
@@ -204,12 +209,15 @@ public void run(IdentityConfiguration configuration, Environment environment) th
204209
SecurityManager securityManager = securityFactory.getInstance();
205210
SecurityUtils.setSecurityManager(securityManager);
206211

212+
final MemberServiceClient memberServiceClient = new MemberServiceClient(apiClient,
213+
configuration.getMemberServiceClientConfig(), configuration.getM2mAuthConfiguration());
207214
// JDBI based DAOs for Authorization
208215
final RoleDAO roleDAO = authjdbi.onDemand(RoleDAO.class);
209216
// for AuthorizationResource
210217
roleDao = authjdbi.onDemand(RoleDAO.class);
211218

212219
roleDAO.setShiroSettings(shiroSettings);
220+
roleDAO.setMemberServiceClient(memberServiceClient);
213221

214222
// creating new resource for every request
215223
RoleResource roleResource = new RoleResource(roleDAO);
@@ -221,8 +229,6 @@ public void run(IdentityConfiguration configuration, Environment environment) th
221229
environment.jersey().register(polResource);
222230
}
223231

224-
final Client apiClient = new JerseyClientBuilder(environment).using(new JerseyClientConfiguration())
225-
.build(getName());
226232
final EventBusServiceClient eventBusServiceClient = new EventBusServiceClient(apiClient,
227233
configuration.getEventBusServiceClientConfig(), configuration.getM2mAuthConfiguration());
228234
// Resources::users

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public class IdentityConfiguration extends APIBaseConfiguration {
108108
@NotNull
109109
@JsonProperty("eventBusServiceClient")
110110
private final BaseClientConfiguration eventBusServiceClientConfig = new BaseClientConfiguration();
111+
112+
/**
113+
* The member service client configuration
114+
*/
115+
@Valid
116+
@NotNull
117+
@JsonProperty("memberServiceClient")
118+
private final BaseClientConfiguration memberServiceClientConfig = new BaseClientConfiguration();
111119

112120

113121

@@ -181,4 +189,13 @@ public M2mAuthConfiguration getM2mAuthConfiguration() {
181189
public BaseClientConfiguration getEventBusServiceClientConfig() {
182190
return this.eventBusServiceClientConfig;
183191
}
192+
193+
/**
194+
* Get memberServiceClientConfig
195+
*
196+
* @return the memberServiceClientConfig
197+
*/
198+
public BaseClientConfiguration getMemberServiceClientConfig() {
199+
return this.memberServiceClientConfig;
200+
}
184201
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.appirio.tech.core.service.identity;
2+
3+
import org.hibernate.validator.constraints.NotEmpty;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
/**
8+
* The MemberServiceClientConfiguration is for configuration.
9+
*
10+
* It's added in F2F - Topcoder Identity Service - Update identity service for additional role details
11+
*
12+
* @author TCSCODER
13+
* @version 1.0
14+
*/
15+
public class MemberServiceClientConfiguration {
16+
/**
17+
* Represents the endpoint attribute.
18+
*/
19+
@JsonProperty
20+
@NotEmpty
21+
private String endpoint;
22+
23+
/**
24+
* Get endpoint
25+
*
26+
* @return the endpoint
27+
*/
28+
public String getEndpoint() {
29+
return this.endpoint;
30+
}
31+
32+
/**
33+
* Set endpoint
34+
*
35+
* @return the endpoint to set
36+
*/
37+
public void setEndpoint(String endpoint) {
38+
this.endpoint = endpoint;
39+
}
40+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.appirio.tech.core.service.identity.clients;
2+
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import javax.ws.rs.client.Client;
8+
import javax.ws.rs.client.WebTarget;
9+
import javax.ws.rs.core.MediaType;
10+
import javax.ws.rs.core.Response;
11+
12+
import org.eclipse.jetty.http.HttpStatus;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import com.appirio.clients.BaseClient;
17+
import com.appirio.clients.BaseClientConfiguration;
18+
import com.appirio.supply.SupplyException;
19+
import com.appirio.tech.core.api.v3.TCID;
20+
import com.appirio.tech.core.service.identity.M2mAuthConfiguration;
21+
import com.appirio.tech.core.service.identity.representation.RoleSubject;
22+
import com.appirio.tech.core.service.identity.util.Utils;
23+
import com.fasterxml.jackson.databind.JsonNode;
24+
25+
/**
26+
* The client to fetch the group information from the REST API.
27+
*
28+
* It's added in F2F - Topcoder Identity Service - Update identity service for additional role details
29+
*
30+
* @author TCSCODER
31+
* @version 1.0
32+
*/
33+
public class MemberServiceClient extends BaseClient {
34+
/**
35+
* The logger
36+
*/
37+
private final static Logger LOGGER = LoggerFactory.getLogger(MemberServiceClient.class);
38+
39+
/**
40+
* The M2M auth configuration
41+
*/
42+
private final M2mAuthConfiguration m2mAuthConfiguration;
43+
44+
/**
45+
* Constructor
46+
*
47+
* @param client the Jersey client
48+
* @param config the configuration
49+
* @param m2mAuthConfiguration the M2M auth configuration
50+
*/
51+
public MemberServiceClient(Client client, BaseClientConfiguration config, M2mAuthConfiguration m2mAuthConfiguration) {
52+
super(client, config);
53+
this.m2mAuthConfiguration = m2mAuthConfiguration;
54+
}
55+
56+
/**
57+
* Get the member handle and email from the configured endpoint
58+
*
59+
* @param userIds the user ids
60+
* @return the user list
61+
* @throws Exception if fail to fetch the member information from the configured endpoint
62+
*/
63+
public Set<RoleSubject> getMembers(List<TCID> userIds) throws Exception {
64+
StringBuilder url = new StringBuilder(this.config.getEndpoint());
65+
url.append("?fields=userId,handle,email");
66+
for (TCID userId : userIds) {
67+
url.append("&userIds=" + userId.getId());
68+
}
69+
WebTarget target = this.client.target(url.toString());
70+
71+
String m2mToken = Utils.generateAuthToken(this.m2mAuthConfiguration);
72+
73+
Response response = target.request(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + m2mToken).get();
74+
75+
if (response.getStatusInfo().getStatusCode() != HttpStatus.OK_200) {
76+
LOGGER.error("Unable to get members: {}", response);
77+
throw new SupplyException("Unable to get members from the API, the returned status code is: " + response.getStatusInfo().getStatusCode());
78+
}
79+
80+
JsonNode apiResponse = response.readEntity(JsonNode.class);
81+
Set<RoleSubject> users = new HashSet<RoleSubject>();
82+
if (apiResponse.isArray()) {
83+
for (JsonNode member : apiResponse) {
84+
RoleSubject user = new RoleSubject();
85+
86+
user.setUserId(member.path("userId").asLong());
87+
user.setHandle(member.path("handle").textValue());
88+
user.setEmail(member.path("email").textValue());
89+
90+
users.add(user);
91+
}
92+
}
93+
94+
return users;
95+
}
96+
}

src/main/java/com/appirio/tech/core/service/identity/dao/RoleDAO.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import com.appirio.tech.core.api.v3.exception.APIRuntimeException;
3030
import com.appirio.tech.core.api.v3.request.FieldSelector;
3131
import com.appirio.tech.core.api.v3.util.jdbi.TCBeanMapperFactory;
32+
import com.appirio.tech.core.service.identity.clients.MemberServiceClient;
3233
import com.appirio.tech.core.service.identity.representation.Role;
34+
import com.appirio.tech.core.service.identity.representation.RoleSubject;
3335
import com.appirio.tech.core.service.identity.util.Utils;
3436
import com.appirio.tech.core.service.identity.util.shiro.Shiro;
3537

@@ -58,7 +60,17 @@ public Shiro getShiroSettings() {
5860
public void setShiroSettings(Shiro shiroSettings) {
5961
this.shiroSettings = shiroSettings;
6062
}
61-
63+
64+
protected MemberServiceClient memberServiceClient;
65+
66+
public MemberServiceClient getMemberServiceClient() {
67+
return memberServiceClient;
68+
}
69+
70+
public void setMemberServiceClient(MemberServiceClient memberServiceClient) {
71+
this.memberServiceClient = memberServiceClient;
72+
}
73+
6274
@SqlQuery(" SELECT "
6375
+ " r.id AS id,"
6476
+ " r.name AS roleName,"
@@ -222,8 +234,8 @@ public Role create(Role role, TCID operatorId) throws Exception{
222234
role.setId(id);
223235

224236
if (role.getSubjects() != null) {
225-
for (TCID subjectId : role.getSubjects()) {
226-
assignRole(Utils.toLongValue(id), Utils.toLongValue(subjectId), Utils.toLongValue(operatorId));
237+
for (RoleSubject subject : role.getSubjects()) {
238+
assignRole(Utils.toLongValue(id), subject.getUserId(), Utils.toLongValue(operatorId));
227239
}
228240
}
229241
return role;
@@ -248,8 +260,8 @@ public TCID update(Role role) throws Exception{
248260

249261
if (role.getSubjects() != null) {
250262
deleteRoleAssignmentsByRoleId(role.getId().getId());
251-
for (TCID subjectId : role.getSubjects()) {
252-
assignRole(Utils.toLongValue(role.getId()), Utils.toLongValue(subjectId), Utils.toLongValue(role.getModifiedBy()));
263+
for (RoleSubject subject : role.getSubjects()) {
264+
assignRole(Utils.toLongValue(role.getId()), subject.getUserId(), Utils.toLongValue(role.getModifiedBy()));
253265
}
254266
}
255267

@@ -262,14 +274,18 @@ public Role getSubjects(TCID roleId) {
262274
throw new IllegalArgumentException("Specified id is invalid. id: "+roleId);
263275

264276
List<TCID> subs = getSubs(Utils.toLongValue(roleId));
277+
278+
Set<RoleSubject> subjects;
279+
try {
280+
subjects = this.getMemberServiceClient().getMembers(subs);
281+
} catch (Exception e) {
282+
// logged
283+
subjects = new HashSet<RoleSubject>();
284+
}
265285

266286
Role role = findRoleById(Utils.toLongValue(roleId));
267-
Set<TCID> subSet = new HashSet<TCID>();
268287

269-
for (TCID s : subs) {
270-
subSet.add(s);
271-
}
272-
role.setSubjects(subSet);
288+
role.setSubjects(subjects);
273289
role.setId(roleId);
274290
return role;
275291
}

src/main/java/com/appirio/tech/core/service/identity/representation/Role.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import java.util.Set;
44

5-
import com.appirio.tech.core.api.v3.TCID;
65
import com.appirio.tech.core.api.v3.model.AbstractIdResource;
76
import com.appirio.tech.core.api.v3.model.annotation.ApiMapping;
87

98

109
public class Role extends AbstractIdResource {
1110

1211
private String roleName;
13-
private Set<TCID> subjects;
12+
private Set<RoleSubject> subjects;
1413

1514
public String getRoleName() {
1615
return roleName;
@@ -21,11 +20,11 @@ public void setRoleName(String roleName) {
2120
}
2221

2322
@ApiMapping(queryDefault=false)
24-
public Set<TCID> getSubjects() {
23+
public Set<RoleSubject> getSubjects() {
2524
return subjects;
2625
}
2726

28-
public void setSubjects(Set<TCID> subjects) {
27+
public void setSubjects(Set<RoleSubject> subjects) {
2928
this.subjects = subjects;
3029
}
3130
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.appirio.tech.core.service.identity.representation;
2+
3+
import com.appirio.tech.core.api.v3.resource.old.RESTResource;
4+
5+
public class RoleSubject implements RESTResource {
6+
private long userId;
7+
private String handle;
8+
private String email;
9+
10+
public long getUserId() {
11+
return userId;
12+
}
13+
public void setUserId(long userId) {
14+
this.userId = userId;
15+
}
16+
public String getHandle() {
17+
return handle;
18+
}
19+
public void setHandle(String handle) {
20+
this.handle = handle;
21+
}
22+
public String getEmail() {
23+
return email;
24+
}
25+
public void setEmail(String email) {
26+
this.email = email;
27+
}
28+
}

src/main/resources/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ eventBusServiceClient:
156156
topic: @eventBusServiceClient.configuration.topic@
157157
originator: @eventBusServiceClient.configuration.originator@
158158

159+
memberServiceClient:
160+
endpoint: @memberServiceClient.endpoint@
161+
159162
m2mAuthConfig:
160163
clientId: @m2mAuthConfig.clientId@
161164
clientSecret: @m2mAuthConfig.clientSecret@

src/main/resources/config.yml.localdev

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ eventBusServiceClient:
142142
topic: topic-name
143143
originator: app.identity.service
144144

145+
memberServiceClient:
146+
endpoint: "https://api.topcoder-dev.com/v5/members"
147+
145148
m2mAuthConfig:
146149
clientId: dummy
147150
clientSecret: dummy

0 commit comments

Comments
 (0)