Skip to content

Instantly share code, notes, and snippets.

@omindu
Created December 4, 2017 19:32
Show Gist options
  • Save omindu/a54eddc948ad3b75f8ec8b0d5748c24e to your computer and use it in GitHub Desktop.
Save omindu/a54eddc948ad3b75f8ec8b0d5748c24e to your computer and use it in GitHub Desktop.
package sample.extended.saml.authenticator;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException;
import org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.authenticator.samlsso.SAMLSSOAuthenticator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExtendedSAMLSSOAuthenticator extends SAMLSSOAuthenticator implements FederatedApplicationAuthenticator {
private static final long serialVersionUID = 3037637380716421420L;
public ExtendedSAMLSSOAuthenticator() {
super();
}
public String getContextIdentifier(HttpServletRequest httpServletRequest) {
return null;
}
@Override
public String getFriendlyName() {
return "ExtendedSAMLAuthenticator";
}
@Override
public String getName() {
return "ExtendedSAMLAuthenticator";
}
@Override
protected void initiateAuthenticationRequest(HttpServletRequest request, HttpServletResponse response,
AuthenticationContext context)
throws AuthenticationFailedException {
populateProperties(context);
super.initiateAuthenticationRequest(request, response, context);
}
@Override
protected void initiateLogoutRequest(HttpServletRequest request,
HttpServletResponse response, AuthenticationContext context)
throws LogoutFailedException {
populatePropertiesForLogout(context);
super.initiateLogoutRequest(request, response, context);
}
@Override
protected void processAuthenticationResponse(HttpServletRequest request,
HttpServletResponse response,
AuthenticationContext context) throws AuthenticationFailedException {
populateProperties(context);
super.processAuthenticationResponse(request, response, context);
}
private void populateProperties(AuthenticationContext context) throws AuthenticationFailedException {
// We need to fetch the SAMLSSOAuthenticator Properties to get the authentication using the super class.
String idpName = context.getExternalIdP().getName();
Map<String, String> samlFedAuthProperties =
FrameworkUtils.getAuthenticatorPropertyMapFromIdP(context.getExternalIdP(), super.getName());
if (samlFedAuthProperties == null || samlFedAuthProperties.isEmpty()) {
String msg = "SAML Federated Authenticator not configured for '%s' federated IDP. %s authenticator " +
"requires the SAML Federated Authenticator to be configured.";
throw new AuthenticationFailedException(String.format(msg, idpName, getFriendlyName()));
}
context.getAuthenticatorProperties().putAll(samlFedAuthProperties);
}
private void populatePropertiesForLogout(AuthenticationContext context) throws LogoutFailedException {
// We need to fetch the SAMLSSOAuthenticator Properties to get the authentication using the super class.
String idpName = context.getExternalIdP().getName();
Map<String, String> samlFedAuthProperties =
FrameworkUtils.getAuthenticatorPropertyMapFromIdP(context.getExternalIdP(), super.getName());
if (samlFedAuthProperties == null || samlFedAuthProperties.isEmpty()) {
String msg = "SAML Federated Authenticator not configured for '%s' federated IDP. %s authenticator " +
"requires the SAML Federated Authenticator to be configured.";
throw new LogoutFailedException(String.format(msg, idpName, getFriendlyName()));
}
context.getAuthenticatorProperties().putAll(samlFedAuthProperties);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment