Skip to content

Commit 5bed1af

Browse files
authored
Merge pull request #8994 from ezhou365/mistakeFull
modify to correctly translate error messages into correct locales either based on browser or server
2 parents a7e1d16 + e111f08 commit 5bed1af

21 files changed

+373
-435
lines changed

dev/com.ibm.ws.security.oauth/src/com/ibm/oauth/core/api/error/OAuthException.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class OAuthException extends Exception {
2525

2626
/**
2727
* Creates a OAuthException.
28-
*
28+
*
2929
* @param message A message for the error.
3030
* @param cause A root exception.
3131
*/
@@ -35,11 +35,19 @@ public OAuthException(String message, Throwable cause) {
3535

3636
/**
3737
* Gets error type for this OAuth exception
38-
*
38+
*
3939
* @return error type
4040
*/
4141
public abstract String getError();
4242

4343
public abstract String formatSelf(Locale locale, String encoding);
4444

45+
public String getMsgKey() {
46+
return _msgKey;
47+
}
48+
49+
public Object[] getObjects() {
50+
return _objs;
51+
}
52+
4553
}

dev/com.ibm.ws.security.oauth/src/com/ibm/oauth/core/api/error/OidcServerException.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
*******************************************************************************/
1111
package com.ibm.oauth.core.api.error;
1212

13+
import java.util.Enumeration;
14+
import java.util.Locale;
15+
1316
import com.google.gson.JsonObject;
1417
import com.google.gson.JsonPrimitive;
1518
import com.ibm.oauth.core.api.error.oauth20.OAuth20Exception;
19+
import com.ibm.ws.security.oauth20.error.impl.BrowserAndServerLogMessage;
1620
import com.ibm.ws.security.oauth20.util.OidcOAuth20Util;
1721

1822
/**
@@ -27,47 +31,82 @@ public class OidcServerException extends OAuth20Exception {
2731

2832
private final String _errorCode;
2933
private final String _errorDescription;
34+
private final BrowserAndServerLogMessage _browserServerLog;
3035
private int _httpStatus = -1;
3136

3237
/**
3338
* Constructs an instance of this exception with the referenced arguments.
34-
*
39+
*
3540
* @param desription
3641
* The error description for this exception. Can be <code>null</code> if the code is null
37-
*
42+
*
3843
* @param code
3944
* The error code for this exception. Specify <code>null</code> if the code is unknown.
4045
* @param cause
41-
* exception causing the problem
46+
* exception causing the problem
4247
* @param httpStatus
4348
* The HTTP status code to associate to this exception.
4449
*/
4550
public OidcServerException(String description, String code, int httpStatus, Throwable cause) {
46-
super(code, description, cause); //$NON-NLS-1$
51+
super(code, description, cause);
4752
_errorDescription = description;
4853
_errorCode = code;
4954
_httpStatus = httpStatus;
55+
_browserServerLog = null;
5056
}
5157

5258
public OidcServerException(String description, String code, int httpStatus) {
53-
super(code, description, null); //$NON-NLS-1$
59+
super(code, description, null);
5460
_errorDescription = description;
5561
_errorCode = code;
5662
_httpStatus = httpStatus;
63+
_browserServerLog = null;
64+
65+
}
66+
67+
public OidcServerException(BrowserAndServerLogMessage browserServerLogMsg, String code, int httpStatus) {
68+
super(code, null, null);
69+
70+
_errorDescription = null;
71+
_errorCode = code;
72+
_httpStatus = httpStatus;
73+
_browserServerLog = browserServerLogMsg;
74+
}
75+
76+
public OidcServerException(BrowserAndServerLogMessage browserServerLogMsg, String code, int httpStatus, Throwable cause) {
77+
super(code, null, cause);
78+
79+
_errorDescription = null;
80+
_errorCode = code;
81+
_httpStatus = httpStatus;
82+
_browserServerLog = browserServerLogMsg;
5783
}
5884

5985
/**
6086
* Returns the error description for this exception, as an English string.
61-
*
87+
*
6288
* @return The OAuth error description.
6389
*/
6490
public String getErrorDescription() {
65-
return _errorDescription;
91+
if (_browserServerLog == null) {
92+
return _errorDescription;
93+
}
94+
return _browserServerLog.getBrowserErrorMessage();
95+
}
96+
97+
public String getErrorDescription(Enumeration<Locale> locales) {
98+
if (_browserServerLog == null) {
99+
return getErrorDescription();
100+
} else {
101+
_browserServerLog.setLocales(locales);
102+
return _browserServerLog.getBrowserErrorMessage();
103+
}
104+
66105
}
67106

68107
/**
69108
* Returns the error code associated to this exception.
70-
*
109+
*
71110
* @return The error code for this exception.
72111
*/
73112
public String getErrorCode() {
@@ -76,7 +115,7 @@ public String getErrorCode() {
76115

77116
/**
78117
* Returns the HTTP status code associated to this exception.
79-
*
118+
*
80119
* @return The HTTP status code. Will be -1 if no code was specified.
81120
*/
82121
public int getHttpStatus() {
@@ -89,7 +128,7 @@ public boolean isComplete() {
89128

90129
/**
91130
* Constructs an OAuth 2.0 error response from the exception state, per RFC6749 section 5.2.
92-
*
131+
*
93132
* @return An error JSON string - never <code>null</code>.
94133
*/
95134
public String toJSON() {

dev/com.ibm.ws.security.oauth/src/com/ibm/ws/security/oauth20/error/impl/BrowserAndServerLogMessage.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,27 @@
2222
* other line is updated at some point.
2323
*/
2424
public class BrowserAndServerLogMessage {
25-
26-
private final String browserMsg;
27-
private final String serverMsg;
28-
29-
public BrowserAndServerLogMessage(TraceComponent tc, Enumeration<Locale> requestLocales, String msgKey, Object... inserts) {
30-
browserMsg = Tr.formatMessage(tc, requestLocales, msgKey, inserts);
31-
serverMsg = Tr.formatMessage(tc, msgKey, inserts);
25+
private Enumeration<Locale> requestLocales = null;
26+
private final TraceComponent tc;
27+
private final String msgKey;
28+
private final Object[] inserts;
29+
30+
public BrowserAndServerLogMessage(TraceComponent tc, String msgKey, Object... inserts) {
31+
this.tc = tc;
32+
this.msgKey = msgKey;
33+
this.inserts = inserts;
3234
}
3335

3436
public String getBrowserErrorMessage() {
35-
return browserMsg;
37+
return Tr.formatMessage(tc, requestLocales, msgKey, inserts);
3638
}
3739

3840
public String getServerErrorMessage() {
39-
return serverMsg;
41+
return Tr.formatMessage(tc, msgKey, inserts);
42+
}
43+
44+
public void setLocales(Enumeration<Locale> requestLocales) {
45+
this.requestLocales = requestLocales;
4046
}
4147

4248
}

dev/com.ibm.ws.security.oauth/src/com/ibm/ws/security/oauth20/exception/OAuth20BadParameterException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class OAuth20BadParameterException extends OAuth20Exception {
3333
// TODO deal with OAuth20ExceptionUtil
3434
public OAuth20BadParameterException(String msgKey, Object[] params) {
3535
super(INVALID_REQUEST, Tr.formatMessage(tc, msgKey, params), null);
36+
_objs = params;
3637
_msgKey = msgKey;
3738
_paramName = (String) params[0];
3839
_paramValue = (String) params[1];

dev/com.ibm.ws.security.oauth/src/com/ibm/ws/security/oauth20/plugins/OidcBaseClientProvider.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@
3636
public class OidcBaseClientProvider implements OidcOAuth20ClientProvider {
3737

3838
private static TraceComponent tc = Tr.register(OidcBaseClientProvider.class, "OAuth20Provider", "com.ibm.ws.security.oauth20.resources.ProviderMsgs");
39-
private Logger logger = Logger.getLogger(OidcBaseClientProvider.class.getName());
39+
private final Logger logger = Logger.getLogger(OidcBaseClientProvider.class.getName());
4040
private static final String ERROR_DESCRIPTION_UNIMPLEMENTED = "This method is unimplemented for non-database client stores.";
4141

4242
protected static HashMap<String, OidcBaseClient> clientMap = new HashMap<String, OidcBaseClient>();
4343
protected String providerID;
4444
protected boolean hasRewrites; // URI redirect token substitution
4545
protected static final List<OidcBaseClient> clientsList = new ArrayList<OidcBaseClient>();
46-
private String[] providerRewrites;
46+
private final String[] providerRewrites;
4747

4848
public OidcBaseClientProvider(String providerId, String[] providerRewrites) {
4949
this.providerID = providerId;
5050
this.providerRewrites = providerRewrites != null ? providerRewrites.clone() : null;
5151
}
5252

53+
@Override
5354
public void initialize() {
5455
if (tc.isEntryEnabled()) {
5556
Tr.entry(tc, "initialize");
@@ -63,6 +64,7 @@ public void initialize() {
6364
}
6465
}
6566

67+
@Override
6668
public void init(OAuthComponentConfiguration config) {
6769
if (tc.isEntryEnabled()) {
6870
Tr.entry(tc, "init");
@@ -92,6 +94,7 @@ private void loadClients() {
9294
}
9395
}
9496

97+
@Override
9598
public boolean exists(String clientIdentifier) {
9699
if (tc.isEntryEnabled()) {
97100
Tr.entry(tc, "exists");
@@ -106,6 +109,7 @@ public boolean exists(String clientIdentifier) {
106109
return result;
107110
}
108111

112+
@Override
109113
public OidcBaseClient get(String clientIdentifier) {
110114
if (tc.isEntryEnabled()) {
111115
Tr.entry(tc, "get");
@@ -119,6 +123,7 @@ public OidcBaseClient get(String clientIdentifier) {
119123
return result;
120124
}
121125

126+
@Override
122127
public Collection<OidcBaseClient> getAll() throws OidcServerException {
123128
if (tc.isEntryEnabled()) {
124129
Tr.entry(tc, "getAll");
@@ -133,6 +138,7 @@ public Collection<OidcBaseClient> getAll() throws OidcServerException {
133138
return results;
134139
}
135140

141+
@Override
136142
public Collection<OidcBaseClient> getAll(HttpServletRequest request) throws OidcServerException {
137143
if (tc.isEntryEnabled()) {
138144
Tr.entry(tc, "getAll(request)");
@@ -152,6 +158,7 @@ public Collection<OidcBaseClient> getAll(HttpServletRequest request) throws Oidc
152158
return results;
153159
}
154160

161+
@Override
155162
public boolean validateClient(String clientIdentifier, String clientSecret) {
156163
if (tc.isEntryEnabled()) {
157164
Tr.entry(tc, "validateClient");
@@ -175,14 +182,14 @@ public boolean validateClient(String clientIdentifier, String clientSecret) {
175182
return result;
176183
}
177184

185+
@Override
178186
public OidcBaseClient update(OidcBaseClient newClient) throws OidcServerException {
179187
if (tc.isEntryEnabled()) {
180188
Tr.entry(tc, "update");
181189
}
182190
if (tc.isEntryEnabled()) {
183191
Tr.exit(tc, "update");
184192
}
185-
186193
throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
187194
}
188195

@@ -231,8 +238,7 @@ protected OidcBaseClient getClient(String key, HttpServletRequest request) {
231238

232239
// Add client registration URI
233240
if (request != null && result != null/** && (OidcOAuth20Util.isNullEmpty(result.getRegistrationClientUri())) **/
234-
)
235-
{
241+
) {
236242
RegistrationEndpointServices.processClientRegistationUri(result, request);
237243
}
238244

@@ -245,7 +251,7 @@ protected OidcBaseClient getClient(String key, HttpServletRequest request) {
245251
if (result.getClientName() != null) {
246252
result.setClientName(URLDecoder.decode(result.getClientName(), "UTF-8"));
247253
}
248-
} catch(UnsupportedEncodingException ex) {
254+
} catch (UnsupportedEncodingException ex) {
249255
// keep the existing client name
250256
}
251257
}
@@ -256,14 +262,14 @@ protected OidcBaseClient getClient(String key, HttpServletRequest request) {
256262
return result;
257263
}
258264

265+
@Override
259266
public boolean delete(String clientIdentifier) throws OidcServerException {
260267
if (tc.isEntryEnabled()) {
261268
Tr.entry(tc, "delete");
262269
}
263270
if (tc.isEntryEnabled()) {
264271
Tr.exit(tc, "delete");
265272
}
266-
267273
throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
268274
}
269275

@@ -278,14 +284,14 @@ public boolean deleteOverride(String clientIdentifier) throws OidcServerExceptio
278284
return clientMap.remove(getKey(clientIdentifier)) != null;
279285
}
280286

287+
@Override
281288
public OidcBaseClient put(OidcBaseClient newClient) throws OidcServerException {
282289
if (tc.isEntryEnabled()) {
283290
Tr.entry(tc, "put");
284291
}
285292
if (tc.isEntryEnabled()) {
286293
Tr.exit(tc, "put");
287294
}
288-
289295
throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
290296
}
291297
}

0 commit comments

Comments
 (0)