FileDocCategorySizeDatePackage
AuthenticatorWindow.javaAPI DocAzureus 3.0.3.415084Thu Jan 11 09:00:54 GMT 2007org.gudy.azureus2.ui.swt.auth

AuthenticatorWindow

public class AuthenticatorWindow extends Object implements SEPasswordListener
author
parg

Fields Summary
private static final String
CONFIG_PARAM
protected Map
auth_cache
protected AEMonitor
this_mon
Constructors Summary
public AuthenticatorWindow()

		
	
	
	
		SESecurityManager.addPasswordListener( this );
		
		// System.out.println( "AuthenticatorWindow");
		
		Map	cache = COConfigurationManager.getMapParameter( CONFIG_PARAM, new HashMap());
		
		try{
			Iterator	it = cache.entrySet().iterator();
			
			while( it.hasNext()){
				
				Map.Entry entry	= (Map.Entry)it.next();
				
				String	key 	= (String)entry.getKey();
				Map		value 	= (Map)entry.getValue();
				
				String	user = new String((byte[])value.get( "user" ), "UTF-8" );
				char[]	pw	 = new String((byte[])value.get( "pw" ), "UTF-8" ).toCharArray();
				
				auth_cache.put( key, new authCache(	key, new PasswordAuthentication( user, pw ), true ));
			}
		
		}catch( Throwable e ){
			
			COConfigurationManager.setParameter( CONFIG_PARAM, new HashMap());
			
			Debug.printStackTrace(e);
		}
	
Methods Summary
public voidclearPasswords()

		try{
			this_mon.enter();

			auth_cache = new HashMap();
			
			saveAuthCache();
			
		}finally{
			
			this_mon.exit();
		}	
	
public java.net.PasswordAuthenticationgetAuthentication(java.lang.String realm, java.net.URL tracker)

		try{
			this_mon.enter();
	
			return( getAuthentication( realm, tracker.getProtocol(), tracker.getHost(), tracker.getPort()));
			
		}finally{
			
			this_mon.exit();
		}
	
public java.net.PasswordAuthenticationgetAuthentication(java.lang.String realm, java.lang.String protocol, java.lang.String host, int port)

		try{
			this_mon.enter();
	
			String	tracker = protocol + "://" + host + ":" + port + "/";
	
			InetAddress bind_ip = NetworkAdmin.getSingleton().getDefaultBindAddress();
			
			String	self_addr;
	
			// System.out.println( "auth req for " + realm + " - " + tracker );
			
			if ( bind_ip == null){
		
				self_addr = "127.0.0.1";
		
			}else{
		
				self_addr = bind_ip.getHostAddress();
			}
	
				// when the tracker is connected to internally we don't want to prompt
				// for the password. Here we return a special user and the password hash
				// which is picked up in the tracker auth code - search for "<internal>"!
				
				// also include the tracker IP as well as for scrapes these can occur on
				// a raw torrent which hasn't been modified to point to localhost
			
			if ( 	host.equals(self_addr) ||
					host.equals(COConfigurationManager.getStringParameter("Tracker IP", ""))){
			
				try{
					byte[]	pw	= COConfigurationManager.getByteParameter("Tracker Password", new byte[0]);
				
					String str_pw = new String( Base64.encode(pw));
					
					return( new PasswordAuthentication( "<internal>", str_pw.toCharArray()));
						
				}catch( Throwable e ){
					
					Debug.printStackTrace( e );
				}	
			}
			
			String auth_key = realm+":"+tracker;
								
			authCache	cache = (authCache)auth_cache.get( auth_key );
						
			if ( cache != null ){
				
				PasswordAuthentication	auth = cache.getAuth();
				
				if ( auth != null ){
					
					return( auth );
				}
			}
				
			String[]	res = getAuthenticationDialog( realm, tracker );
				
			if ( res == null ){
				
				return( null );
				
			}else{
								
				PasswordAuthentication auth =  new PasswordAuthentication( res[0], res[1].toCharArray());
				
				boolean	save_pw = res[2].equals("true");
				
				boolean	old_entry_existed = auth_cache.put( auth_key, new authCache( auth_key, auth, save_pw )) != null;
				
				if ( save_pw || old_entry_existed ){
					
					saveAuthCache();
				}
				
				return( auth );
			}	
		}finally{
			
			this_mon.exit();
		}
	
protected java.lang.String[]getAuthenticationDialog(java.lang.String realm, java.lang.String tracker)

		final Display	display = SWTThread.getInstance().getDisplay();
		
		if ( display.isDisposed()){
			
			return( null );
		}
		
		final AESemaphore	sem = new AESemaphore("SWTAuth");
		
		final authDialog[]	dialog = new authDialog[1];
		
		TOTorrent		torrent = TorrentUtils.getTLSTorrent();
		
		final	String	torrent_name;
		
		if ( torrent == null ){
			
			torrent_name	= null;
			
		}else{
			
			torrent_name	= TorrentUtils.getLocalisedName( torrent );
		}
		
		try{
			display.asyncExec(
				new AERunnable()
				{
					public void
					runSupport()
					{
						dialog[0] = new authDialog( sem, display, realm, tracker, torrent_name );
					}
				});
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
			return( null );
		}
		sem.reserve();
		
		String	user 	= dialog[0].getUsername();
		String	pw		= dialog[0].getPassword();
		String	persist	= dialog[0].savePassword()?"true":"false";
		
		if ( user == null ){
			
			return( null );
		}
		
		return( new String[]{ user, pw == null?"":pw, persist });
	
protected voidsaveAuthCache()

		try{
			this_mon.enter();
	
			HashMap	map = new HashMap();

			Iterator	it = auth_cache.values().iterator();
			
			while( it.hasNext()){
				
				authCache	value 	= (authCache)it.next();
				
				if ( value.isPersistent()){
					
					try{
						HashMap	entry_map = new HashMap();
						
						entry_map.put( "user", value.getAuth().getUserName().getBytes( "UTF-8" ));
						entry_map.put( "pw", new String(value.getAuth().getPassword()).getBytes( "UTF-8" ));
						
						map.put( value.getKey(), entry_map );
						
					}catch( Throwable e ){
						
						Debug.printStackTrace(e);
					}
				}
			}
			
			COConfigurationManager.setParameter( CONFIG_PARAM, map );
			
		}finally{
			
			this_mon.exit();
		}
	
public voidsetAuthenticationOutcome(java.lang.String realm, java.net.URL tracker, boolean success)

		try{
			this_mon.enter();
		
			setAuthenticationOutcome( realm, tracker.getProtocol(), tracker.getHost(), tracker.getPort(), success );
			
		}finally{
			
			this_mon.exit();
		}
	
public voidsetAuthenticationOutcome(java.lang.String realm, java.lang.String protocol, java.lang.String host, int port, boolean success)

		try{
			this_mon.enter();
		
			String	tracker = protocol + "://" + host + ":" + port + "/";
			
			String auth_key = realm+":"+tracker;
			
			authCache	cache = (authCache)auth_cache.get( auth_key );
	
			if ( cache != null ){
	
				cache.setOutcome( success );
			}
		}finally{
			
			this_mon.exit();
		}