Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
==================== DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. Copyright 2011-2013 Tirasa. All rights reserved. The contents of this file are subject to the terms of the Common Development and Distribution License("CDDL") (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at See the License for the specific language governing permissions and limitations under the License. When distributing the Covered Code, include this CDDL Header Notice in each file and include the License file at If applicable, add the following below this CDDL Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" ====================
 import java.util.List;
 import java.util.Map;
 public class ADAuthenticate {
     private static final Log LOG = Log.getLog(ADAuthenticate.class);
     private final ADConnection conn;
     private final ObjectClass oclass;
     private final String username;
     private final OperationOptions options;
     public ADAuthenticate(
             final ADConnection conn,
             final ObjectClass oclass,
             final String username,
             final OperationOptions options) {
         this. = conn;
         this. = oclass;
         this. = username;
         this. = options;
     public Uid authenticate(GuardedString password) {
         final ConnectorObject authnObject = getObjectToAuthenticate();
         AuthenticationResult authnResult = null;
         if (authnObject != null) {
             final String entryDN = authnObject.getName().getNameValue();
             authnResult = .authenticate(entryDNpassword);
         if (!isSuccess(authnResult)) {
             throw new InvalidCredentialException(
         try {
         } catch (PasswordExpiredException e) {
             throw e;
         // AuthenticationResult did not throw an exception, 
         // so this authentication was successful.
         return authnObject.getUid();
     private static boolean isSuccess(final AuthenticationResult authResult) {
         // PASSWORD_EXPIRED considered success: credentials were right.
         final AuthenticationResultType type = authResult.getType();
        return authResult != null
                && (type.equals(.)
                || type.equals(.));
        List<StringuserNameAttrs = getUserNameAttributes();
        Map<StringConnectorObjectentryDN2Object = new HashMap<StringConnectorObject>();
        for (String baseContext : ((ADConfiguration.getConfiguration()).getUserBaseContexts()) {
            for (String userNameAttr : userNameAttrs) {
                Attribute attr =;
                for (ConnectorObject object : LdapSearches.findObjects(baseContextattr"entryDN")) {
                    String entryDN = object.getAttributeByName("entryDN").getValue().get(0).toString();
                // If we found more than one authentication candidates, no need to continue
                if (entryDN2Object.size() > 1) {
                    throw new ConnectorSecurityException(.format(
        if (!entryDN2Object.isEmpty()) {
            return entryDN2Object.values().iterator().next();
        return null;
    private List<StringgetUserNameAttributes() {
        String[] result = LdapConstants.getLdapUidAttributes();
        if (result != null && result.length > 0) {
            return Arrays.asList(result);
    public Uid resolveUsername() {
        ConnectorObject authnObject = getObjectToAuthenticate();
        if (authnObject == null) {
            throw new InvalidCredentialException(.format(
        return authnObject.getUid();
New to GrepCode? Check out our FAQ X