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   *     http://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                  LOG.debug( "sending name {} in the NameCallback", name );
80                  ncb.setName( name );
81              }
82              else if ( cb instanceof PasswordCallback )
83              {
84                  PasswordCallback pcb = ( PasswordCallback ) cb;
85  
86                  LOG.debug( "sending credentials in the PasswordCallback" );
87                  pcb.setPassword( Strings.utf8ToString( saslReq.getCredentials() ).toCharArray() );
88              }
89              else if ( cb instanceof RealmCallback )
90              {
91                  RealmCallback rcb = ( RealmCallback ) cb;
92  
93                  if ( saslReq.getRealmName() != null )
94                  {
95                      LOG.debug( "sending the user specified realm value {} in the RealmCallback", saslReq.getRealmName() );
96                      rcb.setText( saslReq.getRealmName() );
97                  }
98                  else
99                  {
100                     LOG.debug(
101                         "No user specified relam value, sending the default realm value {} in the RealmCallback",
102                         rcb.getDefaultText() );
103                     rcb.setText( rcb.getDefaultText() );
104                 }
105             }
106             else if ( cb instanceof RealmChoiceCallback )
107             {
108                 RealmChoiceCallback rccb = ( RealmChoiceCallback ) cb;
109 
110                 boolean foundRealmName = false;
111 
112                 String[] realmNames = rccb.getChoices();
113                 for ( int i = 0; i < realmNames.length; i++ )
114                 {
115                     String realmName = realmNames[i];
116                     if ( realmName.equals( saslReq.getRealmName() ) )
117                     {
118                         foundRealmName = true;
119 
120                         LOG.debug( "sending the user specified realm value {} in the RealmChoiceCallback", realmName );
121                         rccb.setSelectedIndex( i );
122                         break;
123                     }
124                 }
125 
126                 if ( !foundRealmName )
127                 {
128                     throw new IOException(
129                         I18n.format(
130                             "Cannot match ''java.naming.security.sasl.realm'' property value ''{0}'' with choices ''{1}'' in RealmChoiceCallback.",
131                             saslReq.getRealmName(), getRealmNamesAsString( realmNames ) ) );
132                 }
133             }
134         }
135     }
136 
137 
138     /**
139      * Gets the realm names as a string.
140      *
141      * @param realmNames the array of realm names
142      * @return  the realm names as a string
143      */
144     private String getRealmNamesAsString( String[] realmNames )
145     {
146         StringBuilder sb = new StringBuilder();
147 
148         if ( ( realmNames != null ) && ( realmNames.length > 0 ) )
149         {
150             for ( String realmName : realmNames )
151             {
152                 sb.append( realmName );
153                 sb.append( ',' );
154             }
155             sb.deleteCharAt( sb.length() - 1 );
156         }
157 
158         return sb.toString();
159     }
160 }