File Source: IRCAuthenticator.java

     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.parser.irc;
    24  
    25  import java.net.Authenticator;
    26  import java.net.PasswordAuthentication;
    27  
    28  import java.util.Map;
    29  import java.util.HashMap;
    30  
    31  /**
    32   * Handles proxy authentication for the parser.
    33   * 
    34   * @author Shane Mc Cormack
    35   * @see IRCParser
    36   */
    37  public class IRCAuthenticator extends Authenticator {
    38  	/**
    39  	 * A version number for this class. It should be changed whenever the class
    40  	 * structure is changed (or anything else that would prevent serialized
    41  	 * objects being unserialized with the new class).
    42  	 */
    43  	private static final long serialVersionUID = 1;
    44  
    45  	/** Singleton instance of IRCAuthenticator. */
        	 /* 
    P/P 	  *  Method: com.dmdirc.parser.irc.IRCAuthenticator__static_init
        	  * 
        	  *  Postconditions:
        	  *    me == null
        	  */
    46  	private static IRCAuthenticator me = null;
    47  	
    48  	/** List of authentication replies. */
    49  	private final Map<String,PasswordAuthentication> replies = new HashMap<String,PasswordAuthentication>();
    50  	
    51  	/**
    52  	 * Create a new IRCAuthenticator.
    53  	 *
    54  	 * This creates an IRCAuthenticator and registers it as the default
    55  	 * Authenticator.
    56  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.parser.irc.IRCAuthenticator()
        	  * 
        	  *  Postconditions:
        	  *    this.replies == &amp;new HashMap(IRCAuthenticator#1)
        	  *    new HashMap(IRCAuthenticator#1) num objects == 1
        	  */
    57  	private IRCAuthenticator() {
    58  /*		try {
    59  			final Field field = Authenticator.class.getDeclaredField("theAuthenticator");
    60  			field.setAccessible(true);
    61  			final Object authenticator = field.get(null);
    62  			if (authenticator instanceof Authenticator) {
    63  				oldAuthenticator = (Authenticator)authenticator;
    64  			}
    65  		} catch (NoSuchFieldException nsfe) {
    66  		} catch (IllegalAccessException iae) {
    67  		}*/
    68  		Authenticator.setDefault(this);
    69  	}
    70  	
    71  	/**
    72  	 * Get the instance of IRCAuthenticator.
    73  	 *
    74  	 * @return The IRCAuthenticator instance.
    75  	 */
    76  	public static synchronized IRCAuthenticator getIRCAuthenticator() {
        		 /* 
    P/P 		  *  Method: IRCAuthenticator getIRCAuthenticator()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(me)
        		  * 
        		  *  Postconditions:
        		  *    me == One-of{old me, &amp;new IRCAuthenticator(getIRCAuthenticator#1)}
        		  *    me != null
        		  *    return_value == me
        		  *    new HashMap(IRCAuthenticator#1) num objects <= 1
        		  *    new IRCAuthenticator(getIRCAuthenticator#1) num objects <= 1
        		  *    new IRCAuthenticator(getIRCAuthenticator#1).replies == &amp;new HashMap(IRCAuthenticator#1)
        		  * 
        		  *  Test Vectors:
        		  *    me: Inverse{null}, Addr_Set{null}
        		  */
    77  		if (me == null) {
    78  			me = new IRCAuthenticator();
    79  		}
    80  		return me;
    81  	}
    82  
    83  	/**
    84  	 * Add a server to authenticate for.
    85  	 *
    86  	 * @param server ServerInfo object with proxy details.
    87  	 */
    88  	public void addAuthentication(final ServerInfo server) {
        		 /* 
    P/P 		  *  Method: void addAuthentication(ServerInfo)
        		  * 
        		  *  Preconditions:
        		  *    server != null
        		  *    init'ed(server.proxyPass)
        		  *    init'ed(server.proxyPort)
        		  *    init'ed(server.proxyUser)
        		  *    (soft) server.proxyHost != null
        		  *    (soft) this.replies != null
        		  */
    89  		addAuthentication(server.getProxyHost(), server.getProxyPort(), server.getProxyUser(), server.getProxyPass());
    90  	}
    91  
    92  	/**
    93  	 * Add a host to authenticate for.
    94  	 *
    95  	 * @param host Hostname
    96  	 * @param port Port
    97  	 * @param username Username to return for authentication
    98  	 * @param password Password to return for authentication
    99  	 */
   100  	public void addAuthentication(final String host, final int port, final String username, final String password) {
        		 /* 
    P/P 		  *  Method: void addAuthentication(String, int, String, String)
        		  * 
        		  *  Preconditions:
        		  *    (soft) host != null
        		  *    (soft) this.replies != null
        		  * 
        		  *  Test Vectors:
        		  *    password: Addr_Set{null}, Inverse{null}
        		  *    username: Addr_Set{null}, Inverse{null}
        		  *    java.lang.String:isEmpty(...)@101: {1}, {0}
        		  *    java.lang.String:isEmpty(...)@101: {0}, {1}
        		  *    java.util.Map:containsKey(...)@107: {0}, {1}
        		  */
   101  		if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
   102  			return;
   103  		}
   104  		final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
   105  		final String fullhost = host.toLowerCase()+":"+port;
   106  		
   107  		if (replies.containsKey(fullhost)) {
   108  			replies.remove(fullhost);
   109  		}
   110  		
   111  		replies.put(fullhost, pass);
   112  	}
   113  	
   114  	/** {@inheritDoc} */
   115  	protected PasswordAuthentication getPasswordAuthentication() {
   116  		/*
   117  		 * getRequestingHost: 85.234.138.2
   118  		 * getRequestingPort: 1080
   119  		 * getRequestingPrompt: SOCKS authentication
   120  		 * getRequestingProtocol: SOCKS5
   121  		 * getRequestingScheme: null
   122  		 * getRequestingSite: /85.234.138.2
   123  		 * getRequestingURL: null
   124  		 * getRequestorType: SERVER
   125  		 */
   126  
        		 /* 
    P/P 		  *  Method: PasswordAuthentication getPasswordAuthentication()
        		  * 
        		  *  Preconditions:
        		  *    this.replies != null
        		  * 
        		  *  Presumptions:
        		  *    com.dmdirc.parser.irc.IRCAuthenticator:getRequestingHost(...)@127 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   127  		final String fullhost = getRequestingHost().toLowerCase()+":"+getRequestingPort();
   128  		return replies.get(fullhost);
   129  	}
   130  }








SofCheck Inspector Build Version : 2.17854
IRCAuthenticator.java 2009-Jun-25 01:54:24
IRCAuthenticator.class 2009-Sep-02 17:04:16