Skip to content

Commit

Permalink
Merge pull request #8994 from ezhou365/mistakeFull
Browse files Browse the repository at this point in the history
modify to correctly translate error messages into correct locales either based on browser or server
  • Loading branch information
ezhou365 authored Nov 19, 2019
2 parents a7e1d16 + e111f08 commit 5bed1af
Show file tree
Hide file tree
Showing 21 changed files with 373 additions and 435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class OAuthException extends Exception {

/**
* Creates a OAuthException.
*
*
* @param message A message for the error.
* @param cause A root exception.
*/
Expand All @@ -35,11 +35,19 @@ public OAuthException(String message, Throwable cause) {

/**
* Gets error type for this OAuth exception
*
*
* @return error type
*/
public abstract String getError();

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

public String getMsgKey() {
return _msgKey;
}

public Object[] getObjects() {
return _objs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
*******************************************************************************/
package com.ibm.oauth.core.api.error;

import java.util.Enumeration;
import java.util.Locale;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.ibm.oauth.core.api.error.oauth20.OAuth20Exception;
import com.ibm.ws.security.oauth20.error.impl.BrowserAndServerLogMessage;
import com.ibm.ws.security.oauth20.util.OidcOAuth20Util;

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

private final String _errorCode;
private final String _errorDescription;
private final BrowserAndServerLogMessage _browserServerLog;
private int _httpStatus = -1;

/**
* Constructs an instance of this exception with the referenced arguments.
*
*
* @param desription
* The error description for this exception. Can be <code>null</code> if the code is null
*
*
* @param code
* The error code for this exception. Specify <code>null</code> if the code is unknown.
* @param cause
* exception causing the problem
* exception causing the problem
* @param httpStatus
* The HTTP status code to associate to this exception.
*/
public OidcServerException(String description, String code, int httpStatus, Throwable cause) {
super(code, description, cause); //$NON-NLS-1$
super(code, description, cause);
_errorDescription = description;
_errorCode = code;
_httpStatus = httpStatus;
_browserServerLog = null;
}

public OidcServerException(String description, String code, int httpStatus) {
super(code, description, null); //$NON-NLS-1$
super(code, description, null);
_errorDescription = description;
_errorCode = code;
_httpStatus = httpStatus;
_browserServerLog = null;

}

public OidcServerException(BrowserAndServerLogMessage browserServerLogMsg, String code, int httpStatus) {
super(code, null, null);

_errorDescription = null;
_errorCode = code;
_httpStatus = httpStatus;
_browserServerLog = browserServerLogMsg;
}

public OidcServerException(BrowserAndServerLogMessage browserServerLogMsg, String code, int httpStatus, Throwable cause) {
super(code, null, cause);

_errorDescription = null;
_errorCode = code;
_httpStatus = httpStatus;
_browserServerLog = browserServerLogMsg;
}

/**
* Returns the error description for this exception, as an English string.
*
*
* @return The OAuth error description.
*/
public String getErrorDescription() {
return _errorDescription;
if (_browserServerLog == null) {
return _errorDescription;
}
return _browserServerLog.getBrowserErrorMessage();
}

public String getErrorDescription(Enumeration<Locale> locales) {
if (_browserServerLog == null) {
return getErrorDescription();
} else {
_browserServerLog.setLocales(locales);
return _browserServerLog.getBrowserErrorMessage();
}

}

/**
* Returns the error code associated to this exception.
*
*
* @return The error code for this exception.
*/
public String getErrorCode() {
Expand All @@ -76,7 +115,7 @@ public String getErrorCode() {

/**
* Returns the HTTP status code associated to this exception.
*
*
* @return The HTTP status code. Will be -1 if no code was specified.
*/
public int getHttpStatus() {
Expand All @@ -89,7 +128,7 @@ public boolean isComplete() {

/**
* Constructs an OAuth 2.0 error response from the exception state, per RFC6749 section 5.2.
*
*
* @return An error JSON string - never <code>null</code>.
*/
public String toJSON() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@
* other line is updated at some point.
*/
public class BrowserAndServerLogMessage {

private final String browserMsg;
private final String serverMsg;

public BrowserAndServerLogMessage(TraceComponent tc, Enumeration<Locale> requestLocales, String msgKey, Object... inserts) {
browserMsg = Tr.formatMessage(tc, requestLocales, msgKey, inserts);
serverMsg = Tr.formatMessage(tc, msgKey, inserts);
private Enumeration<Locale> requestLocales = null;
private final TraceComponent tc;
private final String msgKey;
private final Object[] inserts;

public BrowserAndServerLogMessage(TraceComponent tc, String msgKey, Object... inserts) {
this.tc = tc;
this.msgKey = msgKey;
this.inserts = inserts;
}

public String getBrowserErrorMessage() {
return browserMsg;
return Tr.formatMessage(tc, requestLocales, msgKey, inserts);
}

public String getServerErrorMessage() {
return serverMsg;
return Tr.formatMessage(tc, msgKey, inserts);
}

public void setLocales(Enumeration<Locale> requestLocales) {
this.requestLocales = requestLocales;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class OAuth20BadParameterException extends OAuth20Exception {
// TODO deal with OAuth20ExceptionUtil
public OAuth20BadParameterException(String msgKey, Object[] params) {
super(INVALID_REQUEST, Tr.formatMessage(tc, msgKey, params), null);
_objs = params;
_msgKey = msgKey;
_paramName = (String) params[0];
_paramValue = (String) params[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@
public class OidcBaseClientProvider implements OidcOAuth20ClientProvider {

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

protected static HashMap<String, OidcBaseClient> clientMap = new HashMap<String, OidcBaseClient>();
protected String providerID;
protected boolean hasRewrites; // URI redirect token substitution
protected static final List<OidcBaseClient> clientsList = new ArrayList<OidcBaseClient>();
private String[] providerRewrites;
private final String[] providerRewrites;

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

@Override
public void initialize() {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "initialize");
Expand All @@ -63,6 +64,7 @@ public void initialize() {
}
}

@Override
public void init(OAuthComponentConfiguration config) {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "init");
Expand Down Expand Up @@ -92,6 +94,7 @@ private void loadClients() {
}
}

@Override
public boolean exists(String clientIdentifier) {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "exists");
Expand All @@ -106,6 +109,7 @@ public boolean exists(String clientIdentifier) {
return result;
}

@Override
public OidcBaseClient get(String clientIdentifier) {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "get");
Expand All @@ -119,6 +123,7 @@ public OidcBaseClient get(String clientIdentifier) {
return result;
}

@Override
public Collection<OidcBaseClient> getAll() throws OidcServerException {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "getAll");
Expand All @@ -133,6 +138,7 @@ public Collection<OidcBaseClient> getAll() throws OidcServerException {
return results;
}

@Override
public Collection<OidcBaseClient> getAll(HttpServletRequest request) throws OidcServerException {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "getAll(request)");
Expand All @@ -152,6 +158,7 @@ public Collection<OidcBaseClient> getAll(HttpServletRequest request) throws Oidc
return results;
}

@Override
public boolean validateClient(String clientIdentifier, String clientSecret) {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "validateClient");
Expand All @@ -175,14 +182,14 @@ public boolean validateClient(String clientIdentifier, String clientSecret) {
return result;
}

@Override
public OidcBaseClient update(OidcBaseClient newClient) throws OidcServerException {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "update");
}
if (tc.isEntryEnabled()) {
Tr.exit(tc, "update");
}

throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

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

// Add client registration URI
if (request != null && result != null/** && (OidcOAuth20Util.isNullEmpty(result.getRegistrationClientUri())) **/
)
{
) {
RegistrationEndpointServices.processClientRegistationUri(result, request);
}

Expand All @@ -245,7 +251,7 @@ protected OidcBaseClient getClient(String key, HttpServletRequest request) {
if (result.getClientName() != null) {
result.setClientName(URLDecoder.decode(result.getClientName(), "UTF-8"));
}
} catch(UnsupportedEncodingException ex) {
} catch (UnsupportedEncodingException ex) {
// keep the existing client name
}
}
Expand All @@ -256,14 +262,14 @@ protected OidcBaseClient getClient(String key, HttpServletRequest request) {
return result;
}

@Override
public boolean delete(String clientIdentifier) throws OidcServerException {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "delete");
}
if (tc.isEntryEnabled()) {
Tr.exit(tc, "delete");
}

throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

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

@Override
public OidcBaseClient put(OidcBaseClient newClient) throws OidcServerException {
if (tc.isEntryEnabled()) {
Tr.entry(tc, "put");
}
if (tc.isEntryEnabled()) {
Tr.exit(tc, "put");
}

throw new OidcServerException(ERROR_DESCRIPTION_UNIMPLEMENTED, OIDCConstants.ERROR_SERVER_ERROR, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}
}
Loading

0 comments on commit 5bed1af

Please sign in to comment.