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 LoginAuthenticator implements ClientAuthenticator {
25  
26      // constants for the authentication stages
27      protected static final int USERNAME = 0;
28  
29      protected static final int PASSWORD = 1;
30  
31      protected static final int COMPLETE = 2;
32  
33      // the user we're authenticating
34      protected String username;
35  
36      // the user's password (the "shared secret")
37      protected String password;
38  
39      // indicates whether we've gone through the entire challenge process.
40      protected int stage = USERNAME;
41  
42      /**
43       * Main constructor.
44       * 
45       * @param username
46       *            The login user name.
47       * @param password
48       *            The login password.
49       */
50      public LoginAuthenticator(String username, String password) {
51          this.username = username;
52          this.password = password;
53      }
54  
55      /**
56       * Respond to the hasInitialResponse query. This mechanism does not have an
57       * initial response.
58       * 
59       * @return Always returns false;
60       */
61      public boolean hasInitialResponse() {
62          return false;
63      }
64  
65      /**
66       * Indicate whether the challenge/response process is complete.
67       * 
68       * @return True if the last challenge has been processed, false otherwise.
69       */
70      public boolean isComplete() {
71          return stage == COMPLETE;
72      }
73  
74      /**
75       * Retrieve the authenticator mechanism name.
76       * 
77       * @return Always returns the string "LOGIN"
78       */
79      public String getMechanismName() {
80          return "LOGIN";
81      }
82  
83      /**
84       * Evaluate a PLAIN login challenge, returning the a result string that
85       * should satisfy the clallenge.
86       * 
87       * @param challenge
88       *            The decoded challenge data, as a byte array
89       * 
90       * @return A formatted challege response, as an array of bytes.
91       * @exception MessagingException
92       */
93      public byte[] evaluateChallenge(byte[] challenge) throws MessagingException {
94  
95          // process the correct stage for the challenge
96          switch (stage) {
97          // should never happen
98          case COMPLETE:
99              throw new MessagingException("Invalid LOGIN challenge");
100 
101         case USERNAME: {
102             byte[] userBytes;
103 
104             try {
105                 // get the username and password in an UTF-8 encoding to create
106                 // the token
107                 userBytes = username.getBytes("UTF-8");
108             } catch (UnsupportedEncodingException e) {
109                 // got an error, fail this (this should never happen).
110                 throw new MessagingException("Invalid encoding");
111             }
112 
113             // next time through we're looking for a password.
114             stage = PASSWORD;
115             // the user bytes are the entire challenge respose.
116             return userBytes;
117         }
118 
119         case PASSWORD: {
120             byte[] passBytes;
121 
122             try {
123                 // get the username and password in an UTF-8 encoding to create
124                 // the token
125                 passBytes = password.getBytes("UTF-8");
126             } catch (UnsupportedEncodingException e) {
127                 // got an error, fail this (this should never happen).
128                 throw new MessagingException("Invalid encoding");
129             }
130             // we're finished
131             stage = COMPLETE;
132             return passBytes;
133         }
134         }
135         // should never get here.
136         throw new MessagingException("Invalid LOGIN challenge");
137     }
138 }