Reorganize authenticateUser to remove some duplicate code and make it easier to follow.
@@ -160,6 +160,27 @@ public class AuthenticationProviderService {
logger.debug("Error configuring RADIUS server.", e);
throw new GuacamoleInvalidCredentialsException("Authentication error.", CredentialsInfo.USERNAME_PASSWORD);
}
+ finally {
+ radiusService.disconnect();
+ }
+ }
+
+ // This is a response to a previous challenge, authenticate with that.
+ else {
+ try {
+ radPack = radiusService.authenticate(credentials.getUsername(),
+ request.getParameter(RadiusStateField.PARAMETER_NAME),
+ challengeResponse);
+ }
+ catch (GuacamoleException e) {
+ logger.error("Cannot configure RADIUS server: {}", e.getMessage());
+ logger.debug("Error configuring RADIUS server.", e);
+ throw new GuacamoleInvalidCredentialsException("Authentication error.", CredentialsInfo.USERNAME_PASSWORD);
+ }
+ finally {
+ radiusService.disconnect();
+ }
+ }
// No RadiusPacket is returned, we've encountered an error.
if (radPack == null) {
@@ -173,6 +194,13 @@ public class AuthenticationProviderService {
throw new GuacamoleInvalidCredentialsException("Authentication failed.", CredentialsInfo.USERNAME_PASSWORD);
}
+ // Received AccessAccept, authentication has succeeded
+ else if (radPack instanceof AccessAccept) {
+ AuthenticatedUser authenticatedUser = authenticatedUserProvider.get();
+ authenticatedUser.init(credentials);
+ return authenticatedUser;
+ }
+
// Received AccessChallenge packet, more credentials required to complete authentication
else if (radPack instanceof AccessChallenge) {
CredentialsInfo expectedCredentials = getRadiusChallenge(radPack);
@@ -183,56 +211,12 @@ public class AuthenticationProviderService {
throw new GuacamoleInsufficientCredentialsException("LOGIN.INFO_RADIUS_ADDL_REQUIRED", expectedCredentials);
}
- // Received AccessAccept, authentication has succeeded
- else if (radPack instanceof AccessAccept) {
- try {
- AuthenticatedUser authenticatedUser = authenticatedUserProvider.get();
- authenticatedUser.init(credentials);
- return authenticatedUser;
- }
- finally {
- radiusService.disconnect();
- }
- }
-
// Something unanticipated happened, so panic and go back to login.
else {
logger.error("Unexpected failure authenticating with RADIUS server.");
throw new GuacamoleInvalidCredentialsException("Unknown error trying to authenticate.", CredentialsInfo.USERNAME_PASSWORD);
}
- }
-
- // This is a response to a challenge, so authenticate with that response
- else {
-
- try {
- radPack = radiusService.authenticate(credentials.getUsername(),
- request.getParameter(RadiusStateField.PARAMETER_NAME),
- challengeResponse);
- }
- catch (GuacamoleException e) {
- logger.error("Cannot configure RADIUS server: {}", e.getMessage());
- logger.debug("Error configuring RADIUS server.", e);
- throw new GuacamoleInvalidCredentialsException("Authentication error.", CredentialsInfo.USERNAME_PASSWORD);
- }
- finally {
- radiusService.disconnect();
- }
-
- // Received AccessAccept, authentication succeeded.
- if (radPack instanceof AccessAccept) {
- AuthenticatedUser authenticatedUser = authenticatedUserProvider.get();
- authenticatedUser.init(credentials);
- return authenticatedUser;
- }
- // Authentication failed.
- else {
- logger.warn("RADIUS Challenge/Response authentication failed.");
- logger.debug("Received something other than AccessAccept packet from the RADIUS server.");
- throw new GuacamoleInvalidCredentialsException("Authentication failed.", CredentialsInfo.USERNAME_PASSWORD);
- }
- }
}
}
