View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.ldap.client.api.callback;
22  
23  
24  import java.io.IOException;
25  
26  import javax.security.auth.callback.Callback;
27  import javax.security.auth.callback.CallbackHandler;
28  import javax.security.auth.callback.NameCallback;
29  import javax.security.auth.callback.PasswordCallback;
30  import javax.security.auth.callback.UnsupportedCallbackException;
31  import javax.security.sasl.RealmCallback;
32  import javax.security.sasl.RealmChoiceCallback;
33  
34  import org.apache.directory.api.i18n.I18n;
35  import org.apache.directory.api.util.Strings;
36  import org.apache.directory.ldap.client.api.SaslRequest;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * The CallbackHandler implementation used by the LdapConnection during SASL mechanism based bind operations.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class SaslCallbackHandler implements CallbackHandler
47  {
48      /** The sasl request. */
49      private SaslRequest saslReq;
50  
51      /** The logger. */
52      private static final Logger LOG = LoggerFactory.getLogger( SaslCallbackHandler.class );
53  
54  
55      /**
56       * Instantiates a new SASL callback handler.
57       *
58       * @param saslReq the SASL request
59       */
60      public SaslCallbackHandler( SaslRequest saslReq )
61      {
62          this.saslReq = saslReq;
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException
71      {
72          for ( Callback cb : callbacks )
73          {
74              if ( cb instanceof NameCallback )
75              {
76                  NameCallback ncb = ( NameCallback ) cb;
77  
78                  String name = saslReq.getUsername();
79                  
80                  if ( LOG.isDebugEnabled() )
81                  {
82                      LOG.debug( I18n.msg( I18n.MSG_04153_SENDING_NAME_IN_CALLBACK, name ) );
83                  }
84                  
85                  ncb.setName( name );
86              }
87              else if ( cb instanceof PasswordCallback )
88              {
89                  PasswordCallback pcb = ( PasswordCallback ) cb;
90  
91                  if ( LOG.isDebugEnabled() )
92                  {
93                      LOG.debug( I18n.msg( I18n.MSG_04154_SENDING_CREDS_IN_CALLBACK ) );
94                  }
95                  
96                  pcb.setPassword( Strings.utf8ToString( saslReq.getCredentials() ).toCharArray() );
97              }
98              else if ( cb instanceof RealmCallback )
99              {
100                 RealmCallback rcb = ( RealmCallback ) cb;
101 
102                 if ( saslReq.getRealmName() != null )
103                 {
104                     if ( LOG.isDebugEnabled() )
105                     {
106                         LOG.debug( I18n.msg( I18n.MSG_04155_SENDING_USER_REALM_IN_CALLBACK, saslReq.getRealmName() ) );
107                     }
108                     
109                     rcb.setText( saslReq.getRealmName() );
110                 }
111                 else
112                 {
113                     if ( LOG.isDebugEnabled() )
114                     {
115                         LOG.debug( I18n.msg( I18n.MSG_04156_SENDING_DEFAULT_REALM_IN_CALLBACK, rcb.getDefaultText() ) );
116                     }
117                     
118                     rcb.setText( rcb.getDefaultText() );
119                 }
120             }
121             else if ( cb instanceof RealmChoiceCallback )
122             {
123                 RealmChoiceCallback rccb = ( RealmChoiceCallback ) cb;
124 
125                 boolean foundRealmName = false;
126 
127                 String[] realmNames = rccb.getChoices();
128                 for ( int i = 0; i < realmNames.length; i++ )
129                 {
130                     String realmName = realmNames[i];
131                     if ( realmName.equals( saslReq.getRealmName() ) )
132                     {
133                         foundRealmName = true;
134 
135                         if ( LOG.isDebugEnabled() )
136                         {
137                             LOG.debug( I18n.msg( I18n.MSG_04157_SENDING_USER_REALM_IN_CALLBACK, realmName ) );
138                         }
139                         
140                         rccb.setSelectedIndex( i );
141                         break;
142                     }
143                 }
144 
145                 if ( !foundRealmName )
146                 {
147                     throw new IOException(
148                         I18n.err( I18n.ERR_04171_CANNOT_PARSE_MATCHED_DN,
149                             saslReq.getRealmName(), getRealmNamesAsString( realmNames ) ) );
150                 }
151             }
152         }
153     }
154 
155 
156     /**
157      * Gets the realm names as a string.
158      *
159      * @param realmNames the array of realm names
160      * @return  the realm names as a string
161      */
162     private String getRealmNamesAsString( String[] realmNames )
163     {
164         StringBuilder sb = new StringBuilder();
165 
166         if ( ( realmNames != null ) && ( realmNames.length > 0 ) )
167         {
168             for ( String realmName : realmNames )
169             {
170                 sb.append( realmName );
171                 sb.append( ',' );
172             }
173             sb.deleteCharAt( sb.length() - 1 );
174         }
175 
176         return sb.toString();
177     }
178 }