PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/support/cas-server-support-okta-authentication/src/main/java/org/apereo/cas/okta/OktaAuthenticationStateHandlerAdapter.java

https://github.com/Jasig/cas
Java | 116 lines | 85 code | 20 blank | 11 comment | 5 complexity | 87ee63d45796b8e48865101438235ea8 MD5 | raw file
  1. package org.apereo.cas.okta;
  2. import org.apereo.cas.authentication.AuthenticationPasswordPolicyHandlingStrategy;
  3. import org.apereo.cas.authentication.MessageDescriptor;
  4. import org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException;
  5. import org.apereo.cas.authentication.support.password.PasswordPolicyContext;
  6. import org.apereo.cas.util.CollectionUtils;
  7. import org.apereo.cas.util.LoggingUtils;
  8. import org.apereo.cas.util.function.FunctionUtils;
  9. import com.okta.authn.sdk.AuthenticationStateHandlerAdapter;
  10. import com.okta.authn.sdk.resource.AuthenticationResponse;
  11. import lombok.Getter;
  12. import lombok.RequiredArgsConstructor;
  13. import lombok.extern.slf4j.Slf4j;
  14. import lombok.val;
  15. import org.apache.commons.lang3.StringUtils;
  16. import javax.security.auth.login.AccountExpiredException;
  17. import javax.security.auth.login.AccountLockedException;
  18. import javax.security.auth.login.AccountNotFoundException;
  19. import javax.security.auth.login.FailedLoginException;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * This is {@link OktaAuthenticationStateHandlerAdapter}.
  26. *
  27. * @author Misagh Moayyed
  28. * @since 6.2.0
  29. */
  30. @RequiredArgsConstructor
  31. @Slf4j
  32. @Getter
  33. public class OktaAuthenticationStateHandlerAdapter extends AuthenticationStateHandlerAdapter {
  34. private final AuthenticationPasswordPolicyHandlingStrategy passwordPolicyHandlingStrategy;
  35. private final PasswordPolicyContext passwordPolicyConfiguration;
  36. private final Map<String, List<Object>> userAttributes = new HashMap<>(0);
  37. private String username;
  38. private Exception failureException;
  39. private List<MessageDescriptor> warnings = new ArrayList<>(0);
  40. @Override
  41. public void handleUnknown(final AuthenticationResponse authenticationResponse) {
  42. failureException = new AccountNotFoundException(authenticationResponse.getStatusString());
  43. }
  44. @Override
  45. public void handleUnauthenticated(final AuthenticationResponse unauthenticatedResponse) {
  46. failureException = new FailedLoginException(unauthenticatedResponse.getStatusString());
  47. }
  48. @Override
  49. public void handleSuccess(final AuthenticationResponse successResponse) {
  50. if (StringUtils.isNotBlank(successResponse.getSessionToken())) {
  51. val user = successResponse.getUser();
  52. this.username = user.getLogin();
  53. FunctionUtils.doIfNotNull(successResponse.getSessionToken(), value -> userAttributes.put("oktaSessionToken", CollectionUtils.wrapList(value)));
  54. FunctionUtils.doIfNotNull(successResponse.getStatusString(), value -> userAttributes.put("oktaStatus", CollectionUtils.wrapList(value)));
  55. FunctionUtils.doIfNotNull(successResponse.getType(), value -> userAttributes.put("oktaType", CollectionUtils.wrapList(value)));
  56. FunctionUtils.doIfNotNull(successResponse.getExpiresAt(), value -> userAttributes.put("oktaExpiration", CollectionUtils.wrapList(value)));
  57. FunctionUtils.doIfNotNull(successResponse.getRecoveryType(), value -> userAttributes.put("oktaRecoveryType", CollectionUtils.wrapList(value)));
  58. user.getProfile().forEach((key, value) -> userAttributes.put(key, CollectionUtils.wrapList(value)));
  59. } else {
  60. handleUnauthenticated(successResponse);
  61. }
  62. }
  63. @Override
  64. public void handlePasswordWarning(final AuthenticationResponse passwordWarning) {
  65. try {
  66. if (passwordPolicyHandlingStrategy.supports(passwordWarning)) {
  67. warnings = passwordPolicyHandlingStrategy.handle(passwordWarning, passwordPolicyConfiguration);
  68. }
  69. } catch (final Exception e) {
  70. LoggingUtils.error(LOGGER, e);
  71. }
  72. handleUnknown(passwordWarning);
  73. }
  74. @Override
  75. public void handlePasswordExpired(final AuthenticationResponse passwordExpired) {
  76. failureException = new AccountExpiredException(passwordExpired.getStatusString());
  77. }
  78. @Override
  79. public void handlePasswordReset(final AuthenticationResponse passwordReset) {
  80. failureException = new AccountPasswordMustChangeException(passwordReset.getStatusString());
  81. }
  82. @Override
  83. public void handleLockedOut(final AuthenticationResponse lockedOut) {
  84. failureException = new AccountLockedException(lockedOut.getStatusString());
  85. }
  86. /**
  87. * Throw exception if necessary.
  88. *
  89. * @throws Exception the exception
  90. */
  91. public void throwExceptionIfNecessary() throws Exception {
  92. if (failureException != null) {
  93. throw this.failureException;
  94. }
  95. }
  96. }