View Javadoc

1   /**
2    *
3    * Copyright 2003-2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.javamail.authentication;
19  
20  import java.io.UnsupportedEncodingException;
21  
22  import javax.mail.MessagingException;
23  
24  public class PlainAuthenticator implements ClientAuthenticator {
25  
26      // the user we're authenticating
27      protected String username;
28  
29      // the user's password (the "shared secret")
30      protected String password;
31  
32      // indicates whether we've gone through the entire challenge process.
33      protected boolean complete = false;
34  
35      /**
36       * Main constructor.
37       * 
38       * @param username
39       *            The login user name.
40       * @param password
41       *            The login password.
42       */
43      public PlainAuthenticator(String username, String password) {
44          this.username = username;
45          this.password = password;
46      }
47  
48      /**
49       * Respond to the hasInitialResponse query. This mechanism does have an
50       * initial response, which is the entire challenge sequence.
51       * 
52       * @return Always returns true.
53       */
54      public boolean hasInitialResponse() {
55          return true;
56      }
57  
58      /**
59       * Indicate whether the challenge/response process is complete.
60       * 
61       * @return True if the last challenge has been processed, false otherwise.
62       */
63      public boolean isComplete() {
64          return complete;
65      }
66  
67      /**
68       * Retrieve the authenticator mechanism name.
69       * 
70       * @return Always returns the string "PLAIN"
71       */
72      public String getMechanismName() {
73          return "PLAIN";
74      }
75  
76      /**
77       * Evaluate a PLAIN login challenge, returning the a result string that
78       * should satisfy the clallenge.
79       * 
80       * @param challenge
81       *            The decoded challenge data, as byte array.
82       * 
83       * @return A formatted challege response, as an array of bytes.
84       * @exception MessagingException
85       */
86      public byte[] evaluateChallenge(byte[] challenge) throws MessagingException {
87          try {
88              // get the username and password in an UTF-8 encoding to create the
89              // token
90              byte[] userBytes = username.getBytes("UTF-8");
91              byte[] passBytes = password.getBytes("UTF-8");
92  
93              // our token has two copies of the username, one copy of the
94              // password, and nulls
95              // between
96              byte[] tokenBytes = new byte[(userBytes.length * 2) + passBytes.length + 2];
97  
98              System.arraycopy(userBytes, 0, tokenBytes, 0, userBytes.length);
99              System.arraycopy(userBytes, 0, tokenBytes, userBytes.length + 1, userBytes.length);
100             System.arraycopy(passBytes, 0, tokenBytes, (userBytes.length * 2) + 2, passBytes.length);
101 
102             complete = true;
103             return tokenBytes;
104 
105         } catch (UnsupportedEncodingException e) {
106             // got an error, fail this
107             throw new MessagingException("Invalid encoding");
108         }
109     }
110 }