mbohun
9/25/2015 - 9:29 AM

DelegateAuthenticationPrincipalResolver.java add support for null PrincipalValidator

DelegateAuthenticationPrincipalResolver.java add support for null PrincipalValidator

diff --git a/src/main/java/au/org/ala/cas/authentication/principal/DelegateAuthenticationPrincipalResolver.java b/src/main/java/au/org/ala/cas/authentication/principal/DelegateAuthenticationPrincipalResolver.java
index 6730e6f..edd7228 100644
--- a/src/main/java/au/org/ala/cas/authentication/principal/DelegateAuthenticationPrincipalResolver.java
+++ b/src/main/java/au/org/ala/cas/authentication/principal/DelegateAuthenticationPrincipalResolver.java
@@ -22,26 +22,34 @@ public class DelegateAuthenticationPrincipalResolver implements PrincipalResolve
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     @NotNull
+    private final ClientCredentialConvertor credentialConvertor;
+
+    @NotNull
     private final PrincipalResolver principalResolver;
 
     @NotNull
     private final UserCreator userCreator;
 
-    @NotNull
     private final PrincipalValidator principalValidator;
 
-    @NotNull
-    private final ClientCredentialConvertor credentialConvertor;
+    // TODO: add proper javdoc comments
+    public DelegateAuthenticationPrincipalResolver(final ClientCredentialConvertor clientCredentialConvertor,
+						   final PrincipalResolver principalResolver,
+						   final UserCreator userCreator) {
+
+	this(clientCredentialConvertor, principalResolver, userCreator, null);
+    }
 
     // TODO: add proper javdoc comments
-    public DelegateAuthenticationPrincipalResolver(final PrincipalResolver principalResolver,
+    public DelegateAuthenticationPrincipalResolver(final ClientCredentialConvertor clientCredentialConvertor,
+						   final PrincipalResolver principalResolver,
 						   final UserCreator userCreator,
-						   final PrincipalValidator principalValidator,
-						   final ClientCredentialConvertor clientCredentialConvertor) {
+						   final PrincipalValidator principalValidator) {
+
+	this.credentialConvertor = clientCredentialConvertor;
 	this.principalResolver = principalResolver;
 	this.userCreator = userCreator;
 	this.principalValidator = principalValidator;
-	this.credentialConvertor = clientCredentialConvertor;
     }
 
     @Override
@@ -68,8 +76,16 @@ public class DelegateAuthenticationPrincipalResolver implements PrincipalResolve
 		     principal == null ? "N/A" : principal.getAttributes());
 
 	// did we find the user?
-	if (this.principalValidator.validate(principal)) {
-	    return principal;
+	if (principal != null) {
+	    // if no custom PrincipalValidator was setup, a non null Principal is considered a valid Principal
+	    if (this.principalValidator == null ) {
+		return principal;
+
+	    } else {
+		if (this.principalValidator.validate(principal)) {
+		    return principal;
+		}
+	    }
 	}
 
 	logger.debug("user {} not found, creating new user for: {}.",
@@ -87,8 +103,16 @@ public class DelegateAuthenticationPrincipalResolver implements PrincipalResolve
 		     principal == null ? "N/A" : principal.getAttributes());
 
 	// did we find the user this time?
-	if (this.principalValidator.validate(principal)) {
-	    return principal;
+	if (principal != null) {
+	    // if no custom PrincipalValidator was setup, a non null Principal is considered a valid Principal
+	    if (this.principalValidator == null ) {
+		return principal;
+
+	    } else {
+		if (this.principalValidator.validate(principal)) {
+		    return principal;
+		}
+	    }
 	}
 
 	// we failed to lookup the user, after we tried to create the user, so we have a problem