Skip to content

Commit 501a41c

Browse files
committed
Take into account the change in the Authentication API in Silverpeas 6.4
1 parent 689d6a5 commit 501a41c

File tree

4 files changed

+119
-65
lines changed

4 files changed

+119
-65
lines changed

mobile-war/src/main/java/org/silverpeas/mobile/server/services/AbstractAuthenticateService.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.silverpeas.core.SilverpeasException;
2929
import org.silverpeas.core.admin.user.model.UserDetail;
3030
import org.silverpeas.core.security.authentication.AuthenticationCredential;
31+
import org.silverpeas.core.security.authentication.AuthenticationResponse;
3132
import org.silverpeas.core.security.authentication.AuthenticationService;
3233
import org.silverpeas.core.security.authentication.AuthenticationServiceProvider;
3334
import org.silverpeas.core.util.ResourceLocator;
@@ -65,20 +66,31 @@ protected static SettingBundle getSettings() {
6566
return ResourceLocator.getSettingBundle("org.silverpeas.mobile.mobileSettings");
6667
}
6768

68-
protected void setMainsessioncontroller(String login, String password, String domainId) throws SilverpeasException {
69+
protected void setMainsessioncontroller(String login, String password, String domainId)
70+
throws SilverpeasException {
6971
AuthenticationService authService = AuthenticationServiceProvider.getService();
7072
AuthenticationCredential credential = AuthenticationCredential.newWithAsLogin(login);
71-
String key = authService.authenticate(credential
72-
.withAsPassword(password)
73-
.withAsDomainId(domainId));
74-
MainSessionController mainSessionController = new MainSessionController(key, getThreadLocalRequest().getSession());
73+
AuthenticationResponse response =
74+
authService.authenticate(credential.withAsPassword(password).withAsDomainId(domainId));
75+
String key;
76+
if (response == null) {
77+
key = null;
78+
} else if (response.getStatus().succeeded()) {
79+
key = response.getToken();
80+
} else {
81+
key = response.getStatus().getCode();
82+
}
83+
MainSessionController mainSessionController =
84+
new MainSessionController(key, getThreadLocalRequest().getSession());
7585
}
7686

7787
protected MainSessionController getMainSessionController() throws Exception {
78-
return (MainSessionController) getThreadLocalRequest().getSession().getAttribute(MAINSESSIONCONTROLLER_ATTRIBUT_NAME);
88+
return (MainSessionController) getThreadLocalRequest().getSession()
89+
.getAttribute(MAINSESSIONCONTROLLER_ATTRIBUT_NAME);
7990
}
8091

81-
protected StreamingList createStreamingList(CommandCreateList command, int callNumber, int callSize, String cacheKey) throws Exception {
92+
protected StreamingList createStreamingList(CommandCreateList command, int callNumber,
93+
int callSize, String cacheKey) throws Exception {
8294
List list;
8395
if (callNumber == 0) {
8496
list = command.execute();
@@ -90,14 +102,19 @@ protected StreamingList createStreamingList(CommandCreateList command, int callN
90102

91103
int calledSize = 0;
92104
boolean moreElements = true;
93-
if (callNumber > 0) calledSize = callSize * callNumber;
105+
if (callNumber > 0) {
106+
calledSize = callSize * callNumber;
107+
}
94108

95109
if ((calledSize + callSize) >= list.size()) {
96110
moreElements = false;
97111
callSize = list.size() - calledSize;
98112
}
99-
StreamingList<BaseDTO> streamingList = new StreamingList<BaseDTO>(list.subList(calledSize, calledSize + callSize), moreElements);
100-
if (!streamingList.getMoreElement()) getThreadLocalRequest().getSession().removeAttribute(cacheKey);
113+
StreamingList<BaseDTO> streamingList =
114+
new StreamingList<BaseDTO>(list.subList(calledSize, calledSize + callSize), moreElements);
115+
if (!streamingList.getMoreElement()) {
116+
getThreadLocalRequest().getSession().removeAttribute(cacheKey);
117+
}
101118
return streamingList;
102119
}
103120
}

mobile-war/src/main/java/org/silverpeas/mobile/server/services/AbstractRestWebService.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.silverpeas.core.SilverpeasException;
44
import org.silverpeas.core.security.authentication.AuthenticationCredential;
5+
import org.silverpeas.core.security.authentication.AuthenticationResponse;
56
import org.silverpeas.core.security.authentication.AuthenticationService;
67
import org.silverpeas.core.security.authentication.AuthenticationServiceProvider;
78
import org.silverpeas.core.util.ResourceLocator;
@@ -11,7 +12,6 @@
1112
import org.silverpeas.mobile.server.common.CommandCreateList;
1213
import org.silverpeas.mobile.shared.StreamingList;
1314
import org.silverpeas.mobile.shared.dto.BaseDTO;
14-
import org.silverpeas.mobile.shared.dto.notifications.NotificationSendedDTO;
1515

1616
import javax.servlet.http.HttpServletRequest;
1717
import java.util.ArrayList;
@@ -28,7 +28,7 @@ protected static SettingBundle getSettings() {
2828
return ResourceLocator.getSettingBundle("org.silverpeas.mobile.mobileSettings");
2929
}
3030

31-
protected MainSessionController getMainSessionController() throws Exception {
31+
protected MainSessionController getMainSessionController() {
3232
return (MainSessionController) getHttpRequest().getSession()
3333
.getAttribute(MAINSESSIONCONTROLLER_ATTRIBUT_NAME);
3434
}
@@ -37,13 +37,22 @@ protected void setMainsessioncontroller(String login, String password, String do
3737
throws SilverpeasException {
3838
AuthenticationService authService = AuthenticationServiceProvider.getService();
3939
AuthenticationCredential credential = AuthenticationCredential.newWithAsLogin(login);
40-
String key =
40+
AuthenticationResponse response =
4141
authService.authenticate(credential.withAsPassword(password).withAsDomainId(domainId));
42+
String key;
43+
if (response == null) {
44+
key = null;
45+
} else if (response.getStatus().succeeded()) {
46+
key = response.getToken();
47+
} else {
48+
key = response.getStatus().getCode();
49+
}
4250
MainSessionController mainSessionController =
4351
new MainSessionController(key, getHttpRequest().getSession());
4452
}
4553

46-
protected StreamingList createStreamingList(CommandCreateList command, int callNumber, int callSize, String cacheKey) throws Exception {
54+
protected StreamingList createStreamingList(CommandCreateList command, int callNumber,
55+
int callSize, String cacheKey) throws Exception {
4756
List list;
4857
if (callNumber == 0) {
4958
list = command.execute();
@@ -55,25 +64,30 @@ protected StreamingList createStreamingList(CommandCreateList command, int callN
5564

5665
int calledSize = 0;
5766
boolean moreElements = true;
58-
if (callNumber > 0) calledSize = callSize * callNumber;
67+
if (callNumber > 0) {
68+
calledSize = callSize * callNumber;
69+
}
5970

6071
if ((calledSize + callSize) >= list.size()) {
6172
moreElements = false;
6273
callSize = list.size() - calledSize;
6374
}
64-
StreamingList<BaseDTO> streamingList = new StreamingList<BaseDTO>(list.subList(calledSize, calledSize + callSize), moreElements);
65-
if (!streamingList.getMoreElement()) getHttpRequest().getSession().removeAttribute(cacheKey);
75+
StreamingList<BaseDTO> streamingList =
76+
new StreamingList<BaseDTO>(list.subList(calledSize, calledSize + callSize), moreElements);
77+
if (!streamingList.getMoreElement()) {
78+
getHttpRequest().getSession().removeAttribute(cacheKey);
79+
}
6680
return streamingList;
6781
}
6882

69-
protected StreamingList<?> makeStreamingList(int callNumber, String CACHE_NAME, HttpServletRequest request, Populator populator) {
83+
protected StreamingList<?> makeStreamingList(int callNumber, String CACHE_NAME,
84+
HttpServletRequest request, Populator populator) {
7085
int callSize = 25;
7186

72-
List<?> list = (List<?>) request.getSession()
73-
.getAttribute(CACHE_NAME);
87+
List<?> list = (List<?>) request.getSession().getAttribute(CACHE_NAME);
7488
if (list == null) {
75-
list = populator.execute();
76-
request.getSession().setAttribute(CACHE_NAME, list);
89+
list = populator.execute();
90+
request.getSession().setAttribute(CACHE_NAME, list);
7791
}
7892

7993
int calledSize = 0;
@@ -88,9 +102,10 @@ protected StreamingList<?> makeStreamingList(int callNumber, String CACHE_NAME,
88102
}
89103

90104
List<?> sbList = list.subList(calledSize, calledSize + callSize);
91-
StreamingList<?> streamingList =
92-
new StreamingList<>(sbList, moreElements);
93-
if (callNumber == 0) streamingList.setFirstCall(true);
105+
StreamingList<?> streamingList = new StreamingList<>(sbList, moreElements);
106+
if (callNumber == 0) {
107+
streamingList.setFirstCall(true);
108+
}
94109
if (!streamingList.getMoreElement()) {
95110
getHttpRequest().getSession().removeAttribute(CACHE_NAME);
96111
}

mobile-war/src/main/java/org/silverpeas/mobile/server/services/ServiceConnection.java

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,15 @@
3333
import org.silverpeas.core.admin.user.model.UserFull;
3434
import org.silverpeas.core.annotation.WebService;
3535
import org.silverpeas.core.security.authentication.AuthenticationCredential;
36+
import org.silverpeas.core.security.authentication.AuthenticationResponse;
3637
import org.silverpeas.core.security.authentication.AuthenticationServiceProvider;
37-
import org.silverpeas.core.security.authentication.exception.AuthenticationPasswordExpired;
38-
import org.silverpeas.core.security.authentication.exception.AuthenticationPasswordMustBeChangedAtNextLogon;
39-
import org.silverpeas.core.security.authentication.exception.AuthenticationPasswordMustBeChangedOnFirstLogin;
40-
import org.silverpeas.core.security.authentication.exception.AuthenticationPwdNotAvailException;
41-
import org.silverpeas.core.security.authentication.exception.AuthenticationUserAccountBlockedException;
42-
import org.silverpeas.core.security.authentication.exception.AuthenticationUserAccountDeactivatedException;
38+
import org.silverpeas.core.security.authentication.exception.AuthenticationException;
4339
import org.silverpeas.core.web.chat.listeners.ChatUserAuthenticationListener;
4440
import org.silverpeas.core.web.rs.UserPrivilegeValidation;
4541
import org.silverpeas.mobile.server.helpers.DataURLHelper;
4642
import org.silverpeas.mobile.server.services.helpers.UserHelper;
4743
import org.silverpeas.mobile.shared.dto.DetailUserDTO;
4844
import org.silverpeas.mobile.shared.dto.DomainDTO;
49-
import org.silverpeas.mobile.shared.exceptions.AuthenticationException;
5045
import org.silverpeas.mobile.shared.exceptions.AuthenticationException.AuthenticationError;
5146

5247
import javax.inject.Inject;
@@ -79,7 +74,8 @@ public class ServiceConnection extends AbstractRestWebService {
7974
@Context
8075
HttpServletRequest request;
8176

82-
private OrganizationController organizationController = OrganizationController.get();
77+
@Inject
78+
private OrganizationController organizationController;
8379

8480
static final String PATH = "mobile/connection";
8581

@@ -98,27 +94,37 @@ public DetailUserDTO login(List<String> ids) {
9894
String domainId = ids.get(2);
9995

10096
// vérification
101-
AuthenticationCredential credential =
102-
AuthenticationCredential.newWithAsLogin(login).withAsPassword(password)
103-
.withAsDomainId(domainId);
104-
String key = AuthenticationServiceProvider.getService().authenticate(credential);
105-
//SilverLogger.getLogger(this).debug("mobile authentification : {0} {1}", login, key);
106-
if (key == null || key.startsWith("Error_")) {
107-
if (key.equals("Error_5")) {
108-
throw new WebApplicationException(AuthenticationError.PwdNotAvailable.name());
109-
} else if (key.equals("Error_PwdExpired")) {
110-
throw new WebApplicationException(AuthenticationError.PwdExpired.name());
111-
} else if(key.equals("Error_PwdMustBeChanged")) {
112-
throw new WebApplicationException(AuthenticationError.PwdMustBeChanged.name());
113-
} else if (key.equals("Error_PwdMustBeChangedOnFirstLogin")) {
114-
throw new WebApplicationException(AuthenticationError.PwdMustBeChangedOnFirstLogin.name());
115-
} else if (key.equals("Error_UserAccountBlocked")) {
116-
throw new WebApplicationException(AuthenticationError.UserAccountBlocked.name());
117-
} else if (key.equals("Error_UserAccountDeactivated")) {
118-
throw new WebApplicationException(AuthenticationError.UserAccountDeactivated.name());
119-
} else {
120-
throw new WebApplicationException(AuthenticationError.BadCredential.name());
97+
AuthenticationCredential credential = getCredentials(login, password, domainId);
98+
AuthenticationResponse result =
99+
AuthenticationServiceProvider.getService().authenticate(credential);
100+
if (result == null || result.getStatus().isInError()) {
101+
AuthenticationResponse.Status status =
102+
result == null ? AuthenticationResponse.Status.BAD_LOGIN_PASSWORD : result.getStatus();
103+
WebApplicationException e;
104+
switch (status) {
105+
case NO_PASSWORD:
106+
e = new WebApplicationException(AuthenticationError.PwdNotAvailable.name());
107+
break;
108+
case PASSWORD_EXPIRED:
109+
e = new WebApplicationException(AuthenticationError.PwdExpired.name());
110+
break;
111+
case PASSWORD_TO_CHANGE:
112+
e = new WebApplicationException(AuthenticationError.PwdMustBeChanged.name());
113+
break;
114+
case PASSWORD_EMAIL_TO_CHANGE_ON_FIRST_LOGIN:
115+
e = new WebApplicationException(AuthenticationError.PwdMustBeChangedOnFirstLogin.name());
116+
break;
117+
case USER_ACCOUNT_BLOCKED:
118+
e = new WebApplicationException(AuthenticationError.UserAccountBlocked.name());
119+
break;
120+
case USER_ACCOUNT_DEACTIVATED:
121+
e = new WebApplicationException(AuthenticationError.UserAccountDeactivated.name());
122+
break;
123+
default:
124+
e = new WebApplicationException(AuthenticationError.BadCredential.name());
125+
break;
121126
}
127+
throw e;
122128
}
123129

124130
// récupération des informations de l'utilisateur
@@ -137,10 +143,10 @@ public DetailUserDTO login(List<String> ids) {
137143
throw new WebApplicationException(AuthenticationError.CanCreateMainSessionController.name());
138144
}
139145

140-
DetailUserDTO userDTO = new DetailUserDTO();
141-
userDTO = UserHelper.getInstance().populate(user);
146+
DetailUserDTO userDTO = UserHelper.getInstance().populate(user);
142147

143-
String avatar = DataURLHelper.convertAvatarToUrlData(user.getAvatarFileName(), getSettings().getString("big.avatar.size", "40x"));
148+
String avatar = DataURLHelper.convertAvatarToUrlData(user.getAvatarFileName(),
149+
getSettings().getString("big.avatar.size", "40x"));
144150
userDTO.setAvatar(avatar);
145151
try {
146152
userDTO.setStatus(new ServiceRSE().getStatus().getDescription());
@@ -157,10 +163,11 @@ public DetailUserDTO login(List<String> ids) {
157163
@GET
158164
@Produces(MediaType.APPLICATION_JSON)
159165
@Path("userExist/{login}/{domainId}")
160-
public Boolean userExist(@PathParam("login") String login, @PathParam("domainId") String domainId) {
166+
public Boolean userExist(@PathParam("login") String login,
167+
@PathParam("domainId") String domainId) {
161168
try {
162169
String id = getUserId(login, domainId);
163-
return !(id == null);
170+
return id != null;
164171
} catch (Exception e) {
165172
return false;
166173
}
@@ -171,7 +178,7 @@ public Boolean userExist(@PathParam("login") String login, @PathParam("domainId"
171178
@Path("setTabletMode")
172179
public Boolean setTabletMode() {
173180
if (!isUserGUIMobileForTablets()) {
174-
request.getSession().setAttribute("tablet", Boolean.valueOf(true));
181+
request.getSession().setAttribute("tablet", Boolean.TRUE);
175182
return true;
176183
}
177184
return false;
@@ -189,7 +196,7 @@ public List<DomainDTO> getDomains() {
189196
return domains;
190197
}
191198

192-
private String getUserId(String login, String domainId) throws Exception {
199+
private String getUserId(String login, String domainId) throws AdminException {
193200
return Administration.get().getUserIdByLoginAndDomain(login, domainId);
194201
}
195202

@@ -209,14 +216,16 @@ private DomainDTO populate(Domain domain) {
209216
@Produces(MediaType.APPLICATION_JSON)
210217
@Path("changePwd/")
211218
public void changePwd(String newPwd) {
212-
if (getUserInSession() == null) throw new NotAuthorizedException(getHttpServletResponse());
213-
UserFull user = null;
219+
if (getUserInSession() == null) {
220+
throw new NotAuthorizedException(getHttpServletResponse());
221+
}
222+
UserFull user;
214223
try {
215224
user = Administration.get().getUserFull(getUserInSession().getId());
216225
user.setPassword(newPwd);
217226
Administration.get().updateUserFull(user);
218227
} catch (AdminException e) {
219-
throw new WebApplicationException(e);
228+
throw new WebApplicationException(e);
220229
}
221230
}
222231

@@ -236,7 +245,8 @@ protected void setUserInSession(UserDetail user) {
236245
}
237246

238247
protected UserDetail getUserInSession() {
239-
return (UserDetail) request.getSession().getAttribute(AbstractAuthenticateService.USER_ATTRIBUT_NAME);
248+
return (UserDetail) request.getSession()
249+
.getAttribute(AbstractAuthenticateService.USER_ATTRIBUT_NAME);
240250
}
241251

242252
@Override
@@ -251,5 +261,16 @@ public String getComponentId() {
251261

252262
@Override
253263
public void validateUserAuthorization(final UserPrivilegeValidation validation) {
264+
// no need to validate the authorization
265+
}
266+
267+
private AuthenticationCredential getCredentials(String login, String password, String domainId) {
268+
try {
269+
return AuthenticationCredential.newWithAsLogin(login)
270+
.withAsPassword(password)
271+
.withAsDomainId(domainId);
272+
} catch (AuthenticationException e) {
273+
throw new WebApplicationException(AuthenticationError.BadCredential.name());
274+
}
254275
}
255276
}

mobile-war/src/main/java/org/silverpeas/mobile/server/services/ServiceTermsOfService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.silverpeas.core.annotation.WebService;
2828
import org.silverpeas.core.security.authentication.AuthenticationCredential;
29+
import org.silverpeas.core.security.authentication.exception.AuthenticationException;
2930
import org.silverpeas.core.security.authentication.exception.AuthenticationUserMustAcceptTermsOfService;
3031
import org.silverpeas.core.security.authentication.verifier.AuthenticationUserVerifierFactory;
3132
import org.silverpeas.core.template.SilverpeasTemplate;
@@ -75,7 +76,7 @@ public Boolean isShow() {
7576
AuthenticationCredential credential = AuthenticationCredential.newWithAsLogin(getUser().getLogin());
7677
credential.setDomainId(getUser().getDomainId());
7778
AuthenticationUserVerifierFactory.getUserMustAcceptTermsOfServiceVerifier(credential).verify();
78-
} catch (AuthenticationUserMustAcceptTermsOfService authenticationUserMustAcceptTermsOfService) {
79+
} catch (AuthenticationException e) {
7980
return true;
8081
}
8182
}

0 commit comments

Comments
 (0)