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