Skip to content

Commit

Permalink
Adds basic authentication handling while performing REST actions
Browse files Browse the repository at this point in the history
Returns HTTP code 401 with the WWW-Authenticate header whenever the checkAccess method throw an ERXBasicAuthenticationException.
  • Loading branch information
emarcelino committed Apr 16, 2015
1 parent 9b90f9a commit ef680a9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package er.rest;


/**
* Basic Authentication Exception.
*
* <p>
* This class responsible for exception when use Basic Authentication.
*
* This exception can be used with checkAcces method of ERXRouteController class.
* </p>
*
* <b>Example</b>
*
* <pre>
* protected void checkAccess() throws SecurityException {
* throw new ERXBasicAuthenticationException("invalid credentials");
* }
* </pre>
*/
public class ERXBasicAuthenticationException extends SecurityException {

private final String basicRealm;

/**
* Creates a <code>ERXBasicAuthenticationException</code> with the specified
* detail message and cause.
*
* For @param basicRealm use default 'application'.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
*/
public ERXBasicAuthenticationException(String message) {
this(message, "application");
}

/**
* Creates a <code>ERXBasicAuthenticationException</code> with the specified
* detail message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param basicRealm message about server authentication requested for user.
*/
public ERXBasicAuthenticationException(String message, String basicRealm) {
super(message);

this.basicRealm = basicRealm;
}

/**
* Creates a <code>ERXBasicAuthenticationException</code> with the specified
* detail message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A <tt>null</tt> value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param basicRealm message about server authentication requested for user.
*/
public ERXBasicAuthenticationException(String message, Throwable cause, String basicRealm) {
super(message, cause);

this.basicRealm = basicRealm;
}

/**
* Message about server authentication.
*
* @return basicRealm message
*/
public String basicRealm() {
return basicRealm;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import er.extensions.foundation.ERXStringUtilities;
import er.extensions.localization.ERXLocalizer;
import er.extensions.validation.ERXValidationException;
import er.rest.ERXBasicAuthenticationException;
import er.rest.ERXNotAllowedException;
import er.rest.ERXRequestFormValues;
import er.rest.ERXRestClassDescriptionFactory;
Expand Down Expand Up @@ -1589,6 +1590,11 @@ protected WOActionResults performActionNamedWithError(String actionName, Throwab
if (meaningfulThrowble instanceof ObjectNotAvailableException || meaningfulThrowble instanceof FileNotFoundException || meaningfulThrowble instanceof NoSuchElementException) {
results = errorResponse(meaningfulThrowble, ERXHttpStatusCodes.NOT_FOUND);
}
else if (meaningfulThrowble instanceof ERXBasicAuthenticationException) {
WOResponse response = (WOResponse) errorResponse(meaningfulThrowble, ERXHttpStatusCodes.UNAUTHORIZED);
response.setHeader("Basic realm=\"" + ((ERXBasicAuthenticationException) meaningfulThrowble).basicRealm() + "\"", "WWW-Authenticate");
results = response;
}
else if (meaningfulThrowble instanceof SecurityException) {
results = errorResponse(meaningfulThrowble, ERXHttpStatusCodes.STATUS_FORBIDDEN);
}
Expand Down

0 comments on commit ef680a9

Please sign in to comment.