This commit is contained in:
Bill Burke 2015-06-21 12:09:22 -04:00
parent 4ebdb8d24b
commit 000159226d
3 changed files with 38 additions and 31 deletions

View file

@ -1,8 +0,0 @@
package org.keycloak.authentication;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public interface AuthenticationFlow {
}

View file

@ -325,6 +325,16 @@ public class AuthenticationProcessor {
clientSession.setTimestamp(Time.currentTime()); clientSession.setTimestamp(Time.currentTime());
return accessCode.getCode(); return accessCode.getCode();
} }
@Override
public Response getChallenge() {
return challenge;
}
@Override
public Error getError() {
return error;
}
} }
public static class AuthException extends RuntimeException { public static class AuthException extends RuntimeException {
@ -372,15 +382,6 @@ public class AuthenticationProcessor {
} }
} }
protected boolean isProcessed(AuthenticationExecutionModel model) {
if (model.isDisabled()) return true;
ClientSessionModel.ExecutionStatus status = clientSession.getExecutionStatus().get(model.getId());
if (status == null) return false;
return status == ClientSessionModel.ExecutionStatus.SUCCESS || status == ClientSessionModel.ExecutionStatus.SKIPPED
|| status == ClientSessionModel.ExecutionStatus.ATTEMPTED
|| status == ClientSessionModel.ExecutionStatus.SETUP_REQUIRED;
}
public boolean isSuccessful(AuthenticationExecutionModel model) { public boolean isSuccessful(AuthenticationExecutionModel model) {
ClientSessionModel.ExecutionStatus status = clientSession.getExecutionStatus().get(model.getId()); ClientSessionModel.ExecutionStatus status = clientSession.getExecutionStatus().get(model.getId());
if (status == null) return false; if (status == null) return false;
@ -486,13 +487,9 @@ public class AuthenticationProcessor {
if (authType != null) { if (authType != null) {
event.detail(Details.AUTH_TYPE, authType); event.detail(Details.AUTH_TYPE, authType);
} }
AuthenticatorFactory factory = (AuthenticatorFactory)session.getKeycloakSessionFactory().getProviderFactory(Authenticator.class, model.getAuthenticator());
Authenticator authenticator = factory.create();
Result context = new Result(model, authenticator);
authenticator.action(context);
FlowExecution flowExecution = createFlowExecution(this.flowId); FlowExecution flowExecution = createFlowExecution(this.flowId);
Response challenge = flowExecution.action(execution, context); Response challenge = flowExecution.action(execution);
if (challenge != null) return challenge; if (challenge != null) return challenge;
if (clientSession.getAuthenticatedUser() == null) { if (clientSession.getAuthenticatedUser() == null) {
throw new AuthException(Error.UNKNOWN_USER); throw new AuthException(Error.UNKNOWN_USER);
@ -585,7 +582,17 @@ public class AuthenticationProcessor {
boolean alternativeSuccessful = false; boolean alternativeSuccessful = false;
Iterator<AuthenticationExecutionModel> executions; Iterator<AuthenticationExecutionModel> executions;
public Response action(String actionExecution, Result actionResult) { protected boolean isProcessed(AuthenticationExecutionModel model) {
if (model.isDisabled()) return true;
ClientSessionModel.ExecutionStatus status = clientSession.getExecutionStatus().get(model.getId());
if (status == null) return false;
return status == ClientSessionModel.ExecutionStatus.SUCCESS || status == ClientSessionModel.ExecutionStatus.SKIPPED
|| status == ClientSessionModel.ExecutionStatus.ATTEMPTED
|| status == ClientSessionModel.ExecutionStatus.SETUP_REQUIRED;
}
public Response action(String actionExecution) {
while (executions.hasNext()) { while (executions.hasNext()) {
AuthenticationExecutionModel model = executions.next(); AuthenticationExecutionModel model = executions.next();
if (isProcessed(model)) { if (isProcessed(model)) {
@ -596,12 +603,16 @@ public class AuthenticationProcessor {
if (!model.getId().equals(actionExecution)) { if (!model.getId().equals(actionExecution)) {
if (model.isAutheticatorFlow()) { if (model.isAutheticatorFlow()) {
FlowExecution flowExecution = createFlowExecution(model.getAuthenticator()); FlowExecution flowExecution = createFlowExecution(model.getAuthenticator());
return flowExecution.action(actionExecution, actionResult); return flowExecution.action(actionExecution);
} else { } else {
throw new AuthException("action is not current execution", Error.INTERNAL_ERROR); throw new AuthException("action is not current execution", Error.INTERNAL_ERROR);
} }
} else { // we found the action } else { // we found the action
Response response = processResult(actionResult); AuthenticatorFactory factory = (AuthenticatorFactory)session.getKeycloakSessionFactory().getProviderFactory(Authenticator.class, model.getAuthenticator());
Authenticator authenticator = factory.create();
Result result = new Result(model, authenticator);
authenticator.action(result);
Response response = processResult(result);
if (response == null) return processFlow(); if (response == null) return processFlow();
else return response; else return response;
} }
@ -674,7 +685,7 @@ public class AuthenticationProcessor {
} }
public Response processResult(Result result) { public Response processResult(AuthenticatorContext result) {
AuthenticationExecutionModel execution = result.getExecution(); AuthenticationExecutionModel execution = result.getExecution();
Status status = result.getStatus(); Status status = result.getStatus();
if (status == Status.SUCCESS){ if (status == Status.SUCCESS){
@ -686,10 +697,10 @@ public class AuthenticationProcessor {
logger.debugv("authenticator FAILED: {0}", execution.getAuthenticator()); logger.debugv("authenticator FAILED: {0}", execution.getAuthenticator());
logFailure(); logFailure();
clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.FAILED); clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.FAILED);
if (result.challenge != null) { if (result.getChallenge() != null) {
return sendChallenge(result, execution); return sendChallenge(result, execution);
} }
throw new AuthException(result.error); throw new AuthException(result.getError());
} else if (status == Status.FORCE_CHALLENGE) { } else if (status == Status.FORCE_CHALLENGE) {
clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.CHALLENGED); clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.CHALLENGED);
return sendChallenge(result, execution); return sendChallenge(result, execution);
@ -705,7 +716,7 @@ public class AuthenticationProcessor {
return sendChallenge(result, execution); return sendChallenge(result, execution);
} }
if (execution.isAlternative()) { if (execution.isAlternative()) {
alternativeChallenge = result.challenge; alternativeChallenge = result.getChallenge();
challengedAlternativeExecution = execution; challengedAlternativeExecution = execution;
} else { } else {
clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.SKIPPED); clientSession.setExecutionStatus(execution.getId(), ClientSessionModel.ExecutionStatus.SKIPPED);
@ -731,9 +742,9 @@ public class AuthenticationProcessor {
} }
public Response sendChallenge(Result result, AuthenticationExecutionModel execution) { public Response sendChallenge(AuthenticatorContext result, AuthenticationExecutionModel execution) {
clientSession.setNote(CURRENT_AUTHENTICATION_EXECUTION, execution.getId()); clientSession.setNote(CURRENT_AUTHENTICATION_EXECUTION, execution.getId());
return result.challenge; return result.getChallenge();
} }

View file

@ -77,4 +77,8 @@ public interface AuthenticatorContext {
* @return * @return
*/ */
String generateAccessCode(); String generateAccessCode();
Response getChallenge();
AuthenticationProcessor.Error getError();
} }