Skip to content

Instantly share code, notes, and snippets.

@serac
Created July 27, 2016 14:00
Show Gist options
  • Select an option

  • Save serac/c3284490cd770b16d9665ddefec0fa5e to your computer and use it in GitHub Desktop.

Select an option

Save serac/c3284490cd770b16d9665ddefec0fa5e to your computer and use it in GitHub Desktop.
ValidateDuoResponse
/**
* Validates the Duo login response.
*
* @author Marvin S. Addison
*/
public class ValidateDuoResponse extends AbstractDuoAction {
/** Success event name, {@value}. */
public static final String SUCCESS_EVENT = "Success";
/** Failure event name, {@value}. */
public static final String FAILURE_EVENT = "Failure";
/** Logger instance. */
private final Logger logger = LoggerFactory.getLogger(ValidateDuoResponse.class);
/** Looks up ProfileRequestContext->SpringRequestContext. */
@Nonnull
private final Function<ProfileRequestContext, SpringRequestContext> springContextLookup;
public ValidateDuoResponse() {
springContextLookup = new ChildContextLookup(SpringRequestContext.class);
}
@Override
protected void doExecute(
@Nonnull final ProfileRequestContext profileRequestContext,
@Nonnull final AuthenticationContext authenticationContext) {
final SpringRequestContext src = springContextLookup.apply(profileRequestContext);
if (src == null) {
throw new IllegalStateException("Cannot find SpringRequestContext");
}
final String sigResponse = src.getRequestContext().getRequestParameters().get("sig_response");
try {
logger.debug("Attempting to validate {}", sigResponse);
final String user = DuoWeb.verifyResponse(
getIntegrationKey(), getSecretKey(), getApplicationKey(), sigResponse);
logger.debug("Duo authentication succeeded for {}", user);
ActionSupport.buildEvent(profileRequestContext, SUCCESS_EVENT);
} catch (Exception e) {
logger.warn("Duo authentication failed with error: {}", e);
ActionSupport.buildEvent(profileRequestContext, FAILURE_EVENT);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment