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

Commit 5fb4fbf

Browse files
authored
Merge pull request #12 from appirio-tech/make_scopes_configurable
make m2m scopes configurable
2 parents b26b8da + 64bca80 commit 5fb4fbf

File tree

10 files changed

+258
-65
lines changed

10 files changed

+258
-65
lines changed

buildtokenproperties.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ M2MAUTHCONFIG_AUTHDOMAIN=$(eval "echo \$${ENV}_M2MAUTHCONFIG_AUTHDOMAIN")
2929
M2MAUTHCONFIG_TOKENEXPIRETIME=$(eval "echo \$${ENV}_M2MAUTHCONFIG_TOKENEXPIRETIME")
3030
M2MAUTHCONFIG_USERID=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERID")
3131
M2MAUTHCONFIG_AUTHPROXYSERVERURL=$(eval "echo \$${ENV}_M2MAUTHCONFIG_AUTHPROXYSERVERURL")
32+
M2MAUTHCONFIG_USERPROFILES_CREATE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_CREATE")
33+
M2MAUTHCONFIG_USERPROFILES_UPDATE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_UPDATE")
34+
M2MAUTHCONFIG_USERPROFILES_READ=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_READ")
35+
M2MAUTHCONFIG_USERPROFILES_DELETE=$(eval "echo \$${ENV}_M2MAUTHCONFIG_USERPROFILES_DELETE")
3236

3337
DOMAIN=$(eval "echo \$${ENV}_DOMAIN")
3438
SMTP=$(eval "echo \$${ENV}_SMTP")
@@ -97,5 +101,9 @@ perl -pi -e "s/\{\{M2MAUTHCONFIG_TOKENEXPIRETIME\}\}/$M2MAUTHCONFIG_TOKENEXPIRET
97101
perl -pi -e "s/\{\{M2MAUTHCONFIG_USERID\}\}/$M2MAUTHCONFIG_USERID/g" $CONFFILENAME
98102
#perl -pi -e "s/\{\{M2MAUTHCONFIG_AUTHPROXYSERVERURL\}\}/$M2MAUTHCONFIG_AUTHPROXYSERVERURL/g" $CONFFILENAME
99103
perl -pi -e "s|\{\{M2MAUTHCONFIG_AUTHPROXYSERVERURL\}\}|$M2MAUTHCONFIG_AUTHPROXYSERVERURL|g" $CONFFILENAME
104+
perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_CREATE\}\}|$M2MAUTHCONFIG_USERPROFILES_CREATE|g" $CONFFILENAME
105+
perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_UPDATE\}\}|$M2MAUTHCONFIG_USERPROFILES_UPDATE|g" $CONFFILENAME
106+
perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_READ\}\}|$M2MAUTHCONFIG_USERPROFILES_READ|g" $CONFFILENAME
107+
perl -pi -e "s|\{\{M2MAUTHCONFIG_USERPROFILES_DELETE\}\}|$M2MAUTHCONFIG_USERPROFILES_DELETE|g" $CONFFILENAME
100108
perl -pi -e "s/\{\{AUTH0_NEW_DOMAIN\}\}/$AUTH0_NEW_DOMAIN/g" $CONFFILENAME
101-
perl -pi -e "s/\{\{AUTH0_DOMAIN\}\}/$AUTH0_DOMAIN/g" $CONFFILENAME
109+
perl -pi -e "s/\{\{AUTH0_DOMAIN\}\}/$AUTH0_DOMAIN/g" $CONFFILENAME

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public void run(IdentityConfiguration configuration, Environment environment) th
230230
configuration.getEventBusServiceClientConfig(), configuration.getM2mAuthConfiguration());
231231
// Resources::users
232232
CacheService cacheService = configuration.getCache().createCacheService();
233-
UserResource userResource = new UserResource(userDao, roleDao, cacheService, eventProducer, eventBusServiceClient);
233+
UserResource userResource = new UserResource(userDao, roleDao, cacheService, eventProducer, eventBusServiceClient, configuration.getM2mAuthConfiguration().getUserProfiles());
234234
userResource.setAuth0Client(configuration.getAuth0()); // TODO: constructor
235235
userResource.setDomain(configuration.getAuthDomain());
236236
// this secret _used_ to be different from the one used in AuthorizationResource.

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.appirio.tech.core.service.identity;
22

3+
import com.appirio.tech.core.service.identity.util.m2mscope.UserProfilesFactory;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import javax.validation.constraints.NotNull;
56

@@ -61,6 +62,17 @@ public class M2mAuthConfiguration {
6162
@JsonProperty
6263
private String authProxyServerUrl;
6364

65+
@JsonProperty
66+
private UserProfilesFactory userProfiles = new UserProfilesFactory();
67+
68+
public UserProfilesFactory getUserProfiles() {
69+
return userProfiles;
70+
}
71+
72+
public void setUserProfiles(UserProfilesFactory userProfiles) {
73+
this.userProfiles = userProfiles;
74+
}
75+
6476
/**
6577
* Get clientId
6678
*
@@ -73,7 +85,7 @@ public String getClientId() {
7385
/**
7486
* Set clientId
7587
*
76-
* @return the clientId to set
88+
* @param clientId the clientId to set
7789
*/
7890
public void setClientId(String clientId) {
7991
this.clientId = clientId;
@@ -91,7 +103,7 @@ public String getClientSecret() {
91103
/**
92104
* Set clientSecret
93105
*
94-
* @return the clientSecret to set
106+
* @param clientSecret the clientSecret to set
95107
*/
96108
public void setClientSecret(String clientSecret) {
97109
this.clientSecret = clientSecret;
@@ -109,7 +121,7 @@ public String getAudience() {
109121
/**
110122
* Set audience
111123
*
112-
* @return the audience to set
124+
* @param audience the audience to set
113125
*/
114126
public void setAudience(String audience) {
115127
this.audience = audience;
@@ -127,7 +139,7 @@ public String getM2mAuthDomain() {
127139
/**
128140
* Set m2mAuthDomain
129141
*
130-
* @return the m2mAuthDomain to set
142+
* @param m2mAuthDomain the m2mAuthDomain to set
131143
*/
132144
public void setM2mAuthDomain(String m2mAuthDomain) {
133145
this.m2mAuthDomain = m2mAuthDomain;
@@ -145,7 +157,7 @@ public Integer getTokenExpireTimeInMinutes() {
145157
/**
146158
* Set tokenExpireTimeInMinutes
147159
*
148-
* @return the tokenExpireTimeInMinutes to set
160+
* @param tokenExpireTimeInMinutes the tokenExpireTimeInMinutes to set
149161
*/
150162
public void setTokenExpireTimeInMinutes(Integer tokenExpireTimeInMinutes) {
151163
this.tokenExpireTimeInMinutes = tokenExpireTimeInMinutes;
@@ -163,7 +175,7 @@ public Long getUserId() {
163175
/**
164176
* Set userId
165177
*
166-
* @return the userId to set
178+
* @param userId the userId to set
167179
*/
168180
public void setUserId(Long userId) {
169181
this.userId = userId;
@@ -181,7 +193,7 @@ public String getAuthProxyServerUrl() {
181193
/**
182194
* Set authProxyServerUrl
183195
*
184-
* @return the authProxyServerUrl to set
196+
* @param authProxyServerUrl the authProxyServerUrl to set
185197
*/
186198
public void setAuthServerProxyUrl(String authProxyServerUrl) {
187199
this.authProxyServerUrl = authProxyServerUrl;

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static com.appirio.tech.core.service.identity.util.Constants.*;
44
import static javax.servlet.http.HttpServletResponse.*;
5+
6+
import com.appirio.tech.core.service.identity.util.m2mscope.UserProfilesFactory;
57
import io.dropwizard.auth.Auth;
68
import io.dropwizard.jersey.PATCH;
79

@@ -91,26 +93,6 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
9193
// TODO: switch to slf4j directly (this delegates to it) - it's more efficient
9294
private static final Logger logger = Logger.getLogger(UserResource.class);
9395

94-
/**
95-
* Represents the create scopes for machine token validation.
96-
*/
97-
public static final String[] ReadScopes = {"read:user_profiles", "all:user_profiles"};
98-
99-
/**
100-
* Represents the create scopes for machine token validation.
101-
*/
102-
public static final String[] CreateScopes = {"create:user_profiles", "all:user_profiles"};
103-
104-
/**
105-
* Represents the delete scopes for machine token validation.
106-
*/
107-
public static final String[] DeleteScopes = {"delete:user_profiles", "all:user_profiles"};
108-
109-
/**
110-
* Represents the update scopes for machine token validation.
111-
*/
112-
public static final String[] UpdateScopes = {"update:user_profiles", "all:user_profiles"};
113-
11496
private int resetTokenExpirySeconds = 30 * 60; //30min
11597

11698
private int resendActivationCodeExpirySeconds = 30 * 60; //30min
@@ -139,6 +121,8 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
139121
* The event bus service client field used to send the event
140122
*/
141123
private final EventBusServiceClient eventBusServiceClient;
124+
125+
private final UserProfilesFactory userProfilesFactory;
142126

143127
/**
144128
* Create UserResource
@@ -148,18 +132,43 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
148132
* @param cacheService the cacheService to use
149133
* @param eventProducer the eventProducer to use
150134
* @param eventBusServiceClient the eventBusServiceClient to use
135+
* @param userProfilesFactory the user profiles scopes configuration.
151136
*/
152137
public UserResource(
153138
UserDAO userDao,
154139
RoleDAO roleDao,
155140
CacheService cacheService,
156141
EventProducer eventProducer,
157-
EventBusServiceClient eventBusServiceClient) {
142+
EventBusServiceClient eventBusServiceClient, UserProfilesFactory userProfilesFactory) {
158143
this.userDao = userDao;
159144
this.roleDao = roleDao;
160145
this.cacheService = cacheService;
161146
this.eventProducer = eventProducer;
162147
this.eventBusServiceClient = eventBusServiceClient;
148+
if (userProfilesFactory == null) {
149+
// create a default one
150+
this.userProfilesFactory = new UserProfilesFactory();
151+
} else {
152+
this.userProfilesFactory = userProfilesFactory;
153+
}
154+
}
155+
156+
/**
157+
* Create UserResource
158+
*
159+
* @param userDao the userDao to use
160+
* @param roleDao the roleDao to use
161+
* @param cacheService the cacheService to use
162+
* @param eventProducer the eventProducer to use
163+
* @param eventBusServiceClient the eventBusServiceClient to use
164+
*/
165+
public UserResource(
166+
UserDAO userDao,
167+
RoleDAO roleDao,
168+
CacheService cacheService,
169+
EventProducer eventProducer,
170+
EventBusServiceClient eventBusServiceClient) {
171+
this(userDao, roleDao, cacheService, eventProducer, eventBusServiceClient, null);
163172
}
164173

165174
protected void setObjectMapper(ObjectMapper objectMapper) {
@@ -205,7 +214,7 @@ public ApiResponse createSSOUserLogin(@Auth AuthUser authUser,
205214
@Valid PostPutRequest<UserProfile> postRequest) {
206215
UserProfile profile = postRequest.getParam();
207216

208-
checkAccessAndUserProfile(authUser, userId, profile, CreateScopes);
217+
checkAccessAndUserProfile(authUser, userId, profile, userProfilesFactory.getCreateScopes());
209218

210219
try {
211220
SSOUserDAO ssoUserDao = this.userDao.createSSOUserDAO();
@@ -246,7 +255,7 @@ public ApiResponse updateSSOUserLogin(@Auth AuthUser authUser,
246255
@PathParam("userId") long userId,
247256
@Valid PostPutRequest<UserProfile> postRequest) {
248257
UserProfile profile = postRequest.getParam();
249-
checkAccessAndUserProfile(authUser, userId, profile, UpdateScopes);
258+
checkAccessAndUserProfile(authUser, userId, profile, userProfilesFactory.getUpdateScopes());
250259

251260
try {
252261
SSOUserDAO ssoUserDao = this.userDao.createSSOUserDAO();
@@ -283,7 +292,7 @@ public ApiResponse updateSSOUserLogin(@Auth AuthUser authUser,
283292
@Path("/{userId}/SSOUserLogin")
284293
public ApiResponse deleteSSOUserLogin(@Auth AuthUser authUser,
285294
@PathParam("userId") long userId, @QueryParam("provider") String provider, @QueryParam("providerId") Long providerId) {
286-
Utils.checkAccess(authUser, DeleteScopes, Utils.AdminRoles);
295+
Utils.checkAccess(authUser, userProfilesFactory.getDeleteScopes(), Utils.AdminRoles);
287296
if (userId <= 0) {
288297
throw new APIRuntimeException(SC_BAD_REQUEST, "userId should be positive:" + userId);
289298
}
@@ -339,7 +348,7 @@ public ApiResponse deleteSSOUserLogin(@Auth AuthUser authUser,
339348
@Path("/{userId}/SSOUserLogins")
340349
public ApiResponse getSSOUserLoginsByUserId(@Auth AuthUser authUser,
341350
@PathParam("userId") long userId) {
342-
Utils.checkAccess(authUser, ReadScopes, Utils.AdminRoles);
351+
Utils.checkAccess(authUser, userProfilesFactory.getReadScopes(), Utils.AdminRoles);
343352
if (userId <= 0) {
344353
throw new APIRuntimeException(SC_BAD_REQUEST, "userId should be positive:" + userId);
345354
}
@@ -365,7 +374,7 @@ public ApiResponse getObjects(
365374
@APIQueryParam(repClass = User.class) QueryParameter query,
366375
@Context HttpServletRequest request) {
367376
logger.info("getObjects");
368-
Utils.checkAccess(authUser, ReadScopes, Utils.AdminRoles);
377+
Utils.checkAccess(authUser, userProfilesFactory.getReadScopes(), Utils.AdminRoles);
369378

370379
try {
371380
List<User> users = userDao.findUsers(
@@ -394,7 +403,7 @@ public ApiResponse getObject(
394403
@PathParam("resourceId") TCID resourceId,
395404
@APIFieldParam(repClass = User.class) FieldSelector selector,
396405
@Context HttpServletRequest request) throws Exception {
397-
validateResourceIdAndCheckPermission(authUser, resourceId, ReadScopes);
406+
validateResourceIdAndCheckPermission(authUser, resourceId, userProfilesFactory.getReadScopes());
398407

399408
User user = this.userDao.populateById(selector, resourceId);
400409
if (user == null) {
@@ -508,7 +517,7 @@ public ApiResponse updateObject(
508517

509518
TCID id = new TCID(resourceId);
510519

511-
validateResourceIdAndCheckPermission(authUser, id, UpdateScopes);
520+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getUpdateScopes());
512521
// checking param
513522
checkParam(patchRequest);
514523

@@ -603,7 +612,7 @@ public ApiResponse createUserProfile(
603612
logger.info(String.format("createUserProfile(%s)", resourceId));
604613

605614
TCID id = new TCID(resourceId);
606-
validateResourceIdAndCheckPermission(authUser, id, CreateScopes);
615+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getCreateScopes());
607616
// checking param
608617
checkParam(postRequest);
609618

@@ -678,7 +687,7 @@ public ApiResponse deleteUserProfile(
678687
throw new APIRuntimeException(SC_BAD_REQUEST, String.format(Constants.MSG_TEMPLATE_MANDATORY, "provider"));
679688

680689
TCID id = new TCID(resourceId);
681-
validateResourceIdAndCheckPermission(authUser, id, DeleteScopes);
690+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getDeleteScopes());
682691

683692
ProviderType providerType = ProviderType.getByName(provider);
684693
if(providerType==null)
@@ -842,7 +851,7 @@ public ApiResponse updateHandle(
842851
logger.info(String.format("updateHandle(%s)", resourceId));
843852

844853
TCID id = new TCID(resourceId);
845-
validateResourceIdAndCheckPermission(authUser, id, UpdateScopes);
854+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getUpdateScopes());
846855
// checking param
847856
checkParam(patchRequest);
848857

@@ -888,7 +897,7 @@ public ApiResponse updatePrimaryEmail(
888897
logger.info(String.format("updatePrimaryEmail(%s)", resourceId));
889898

890899
TCID id = new TCID(resourceId);
891-
validateResourceIdAndCheckPermission(authUser, id, UpdateScopes);
900+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getUpdateScopes());
892901
// checking param
893902
checkParam(patchRequest);
894903

@@ -993,7 +1002,7 @@ public ApiResponse updateStatus(
9931002
logger.info(String.format("updateStatus(%s, %s)", resourceId, comment));
9941003

9951004
TCID id = new TCID(resourceId);
996-
validateResourceIdAndCheckPermission(authUser, id, UpdateScopes);
1005+
validateResourceIdAndCheckPermission(authUser, id, userProfilesFactory.getUpdateScopes());
9971006
// checking param
9981007
checkParam(patchRequest);
9991008

@@ -1165,7 +1174,7 @@ public ApiResponse getAchievements(
11651174

11661175
logger.info(String.format("getAchievements(%s)", resourceId));
11671176

1168-
validateResourceIdAndCheckPermission(authUser, resourceId, ReadScopes);
1177+
validateResourceIdAndCheckPermission(authUser, resourceId, userProfilesFactory.getReadScopes());
11691178

11701179
Long userId = Utils.toLongValue(resourceId);
11711180
logger.debug(String.format("findUserById(%s)", userId));

0 commit comments

Comments
 (0)