Cast incoming attributes types to String safely

AuthnResponse might include user attributes of different types.
It wasn't correct to expect attributes of ArrayList type only.

Change-Id: I84d80bb511652215d612319326af6c520a214c59
diff --git a/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java b/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
index b91f539..ee4c72f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/saml/SamlWebFilter.java
@@ -25,6 +25,8 @@
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.List;
@@ -232,13 +234,28 @@
   }
 
   private static String getAttribute(SAML2Profile user, String attrName) {
-    List<?> names = (List<?>) user.getAttribute(attrName);
+    // TODO(davido): Replace with the invocation from upstream method.
+    List<String> names = extractAttributeValues(user, attrName);
     if (names != null && !names.isEmpty()) {
-      return (String) names.get(0);
+      return names.get(0);
     }
     return null;
   }
 
+  // TODO(davido): Remove if getAttribute() uses the upstream method.
+  private static List<String> extractAttributeValues(SAML2Profile user, String attrName) {
+    final Object value = user.getAttribute(attrName);
+    if (value instanceof String) {
+      return Collections.singletonList((String) value);
+    } else if (value instanceof String[]) {
+      return Arrays.asList((String[]) value);
+    } else if (value instanceof List) {
+      return (List<String>) value;
+    } else {
+      return null;
+    }
+  }
+
   private static String getAttributeOrElseId(SAML2Profile user, String attrName) {
     String value = getAttribute(user, attrName);
     if (value != null) {