001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 021package org.apache.directory.ldap.client.api.callback; 022 023 024import java.io.IOException; 025import java.text.MessageFormat; 026 027import javax.security.auth.callback.Callback; 028import javax.security.auth.callback.CallbackHandler; 029import javax.security.auth.callback.NameCallback; 030import javax.security.auth.callback.PasswordCallback; 031import javax.security.auth.callback.UnsupportedCallbackException; 032import javax.security.sasl.RealmCallback; 033import javax.security.sasl.RealmChoiceCallback; 034 035import org.apache.directory.ldap.client.api.SaslRequest; 036import org.apache.directory.shared.util.Strings; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040 041/** 042 * The CallbackHandler implementation used by the LdapConnection during SASL mechanism based bind operations. 043 * 044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 045 */ 046public class SaslCallbackHandler implements CallbackHandler 047{ 048 /** The sasl request. */ 049 private SaslRequest saslReq; 050 051 /** The logger. */ 052 private static final Logger LOG = LoggerFactory.getLogger( SaslCallbackHandler.class ); 053 054 055 /** 056 * Instantiates a new SASL callback handler. 057 * 058 * @param saslReq the SASL request 059 */ 060 public SaslCallbackHandler( SaslRequest saslReq ) 061 { 062 this.saslReq = saslReq; 063 } 064 065 066 /** 067 * {@inheritDoc} 068 */ 069 public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException 070 { 071 for ( Callback cb : callbacks ) 072 { 073 if ( cb instanceof NameCallback ) 074 { 075 NameCallback ncb = ( NameCallback ) cb; 076 077 String name = saslReq.getUsername(); 078 LOG.debug( "sending name {} in the NameCallback", name ); 079 ncb.setName( name ); 080 } 081 else if ( cb instanceof PasswordCallback ) 082 { 083 PasswordCallback pcb = ( PasswordCallback ) cb; 084 085 LOG.debug( "sending credentials in the PasswordCallback" ); 086 pcb.setPassword( Strings.utf8ToString( saslReq.getCredentials() ).toCharArray() ); 087 } 088 else if ( cb instanceof RealmCallback ) 089 { 090 RealmCallback rcb = ( RealmCallback ) cb; 091 092 if ( saslReq.getRealmName() != null ) 093 { 094 LOG.debug( "sending the user specified realm value {} in the RealmCallback", saslReq.getRealmName() ); 095 rcb.setText( saslReq.getRealmName() ); 096 } 097 else 098 { 099 LOG.debug( 100 "No user specified relam value, sending the default realm value {} in the RealmCallback", 101 rcb.getDefaultText() ); 102 rcb.setText( rcb.getDefaultText() ); 103 } 104 } 105 else if ( cb instanceof RealmChoiceCallback ) 106 { 107 RealmChoiceCallback rccb = ( RealmChoiceCallback ) cb; 108 109 boolean foundRealmName = false; 110 111 String[] realmNames = rccb.getChoices(); 112 for ( int i = 0; i < realmNames.length; i++ ) 113 { 114 String realmName = realmNames[i]; 115 if ( realmName.equals( saslReq.getRealmName() ) ) 116 { 117 foundRealmName = true; 118 119 LOG.debug( "sending the user specified realm value {} in the RealmChoiceCallback", realmName ); 120 rccb.setSelectedIndex( i ); 121 break; 122 } 123 } 124 125 if ( !foundRealmName ) 126 { 127 throw new IOException( 128 MessageFormat 129 .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}