Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors in the facebook authenticator #57

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.wso2.carbon.identity.application.authentication.framework.model.AdditionalData;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorData;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorMessage;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.authenticator.oidc.OIDCAuthenticatorConstants;
Expand Down Expand Up @@ -104,9 +105,12 @@ public class FacebookAuthenticator extends AbstractApplicationAuthenticator impl

private static final long serialVersionUID = -4844100162196896194L;
private static final Log log = LogFactory.getLog(FacebookAuthenticator.class);
private static final String ERROR_REASON = "errorReason";
private static final String INVALID_REQUEST = "invalid_request";
private String tokenEndpoint;
private String oAuthEndpoint;
private String userInfoEndpoint;
private static final String AUTHENTICATOR_MESSAGE = "authenticatorMessage";

/**
* Initiate tokenEndpoint.
Expand Down Expand Up @@ -257,10 +261,11 @@ protected void initiateAuthenticationRequest(HttpServletRequest request,
log.error("Exception while sending to the login page.", e);
throw new AuthenticationFailedException(e.getMessage(), e);
} catch (OAuthSystemException e) {
log.error("Exception while building authorization code request.", e);
String message = "Exception while building the authorization code request.";
setAuthenticatorMessageToContext(message, INVALID_REQUEST, null, context);
log.error(message, e);
throw new AuthenticationFailedException(e.getMessage(), e);
}
return;
}

/**
Expand Down Expand Up @@ -815,6 +820,7 @@ private void handleErrorResponse(HttpServletRequest request, HttpServletResponse
if (log.isDebugEnabled()) {
log.debug("Failed to authenticate via Facebook. " + errorMessage.toString());
}
setAuthenticatorMessageToContext(error, errorCode, errorReason, context);
throw new InvalidCredentialsException(errorMessage.toString());
}
}
Expand Down Expand Up @@ -1025,4 +1031,17 @@ private boolean isNativeSDKBasedFederationCall(HttpServletRequest request) {

return request.getParameter(ACCESS_TOKEN_PARAM) != null && request.getParameter(ID_TOKEN_PARAM) != null;
}

private static void setAuthenticatorMessageToContext(String errorMessage, String errorCode, String errorReason,
AuthenticationContext context) {

Map<String, String> messageContext = new HashMap<>();
if (StringUtils.isNotEmpty(errorReason)) {
messageContext.put(ERROR_REASON, errorReason);
}

AuthenticatorMessage authenticatorMessage = new AuthenticatorMessage(FrameworkConstants.
AuthenticatorMessageType.ERROR, errorCode, errorMessage, messageContext);
context.setProperty(AUTHENTICATOR_MESSAGE, authenticatorMessage);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@

<properties>
<!--Carbon framework version-->
<carbon.identity.framework.version>5.25.491</carbon.identity.framework.version>
<carbon.identity.framework.version>5.25.509</carbon.identity.framework.version>
<identity.outbound.auth.facebook.export.version>${project.version}</identity.outbound.auth.facebook.export.version>
<carbon.identity.framework.import.version.range>[5.25.260, 7.0.0)</carbon.identity.framework.import.version.range>

Expand Down
Loading