de.dpunkt.security.jaas.UserFileLoginModule: 35.48%

Overview

Search Parameters

EvoSuite Parameters

Old Parameters

Statistics

Test suite

Test case 1

  1: UserFileLoginModule var0 = new de.dpunkt.security.jaas.UserFileLoginModule();;
  2: byte[] var1 = new byte[16];;
  3: byte var2 = -37;;
  4: var1[5] = var2;;
  5: byte var4 = 81;;
  6: var1[12] = var4;;
  7: String var6 = var6 = var0.getDigestAsHexString(var1);;

Test case 2

  1: UserFileLoginModule var0 = new de.dpunkt.security.jaas.UserFileLoginModule();;
  2: boolean var1 = null;
  3: try {
  4:   var1 = var0.commit();
  5: } catch(LoginException e) {} // Raised exception
  6: boolean var2 = null;
  7: try {
  8:   var2 = var0.login();
  9: } catch(LoginException e) {} // Raised exception
 10: boolean var3 = null;
 11: try {
 12:   var3 = var0.abort();
 13: } catch(LoginException e) {} // Raised exception
 14: boolean var4 = null;
 15: try {
 16:   var4 = var0.logout();
 17: } catch(LoginException e) {} // Raised exception
 18: var0.initialize((Subject) null, (CallbackHandler) null, (Map) null, (Map) null);;
 19: try {
 20:   var0.readUserFile();
 21: } catch(ArrayIndexOutOfBoundsException e) {} // Raised exceptioncatch(LoginException e) {} // Declared exception

Source Code

  1: 
  2: package de.dpunkt.security.jaas;
  3: 
  4: import java.io.BufferedReader;
  5: import java.io.FileReader;
  6: import java.io.IOException;
  7: 
  8: import java.util.Hashtable;
  9: import java.util.Map;
 10: 
 11: import java.security.MessageDigest;
 12: import java.security.NoSuchAlgorithmException;
 13: 
 14: import javax.security.auth.Subject;
 15: 
 16: import javax.security.auth.callback.Callback;
 17: import javax.security.auth.callback.CallbackHandler;
 18: import javax.security.auth.callback.NameCallback;
 19: import javax.security.auth.callback.PasswordCallback;
 20: import javax.security.auth.callback.UnsupportedCallbackException;
 21: 
 22: import javax.security.auth.login.LoginException;
 23: import javax.security.auth.login.FailedLoginException;
 24: 
 25: import javax.security.auth.spi.LoginModule;
 26: 
 27: public class UserFileLoginModule implements LoginModule {
 28:   private String userFileName;
 29:   private Hashtable userTable;
 30: 
 31:   private Subject subject;
 32:   private CallbackHandler callbackHandler;
 33:   private Map sharedState;
 34:   private Map options;
 35:   private boolean debug = true;
 36: 
 37:   private String userID;
 38: 
 39:   private UserFilePrincipal userFilePrincipal;
 40:   private boolean succeeded = false;
 41:   private boolean commitSucceeded = false;
 42: 
 43: 
 44:   protected String getDigestAsHexString(byte[] digestBytes) {
 45:     StringBuffer digestString = new StringBuffer();
 46:     for(int i = 0; i < digestBytes.length; i++) {
 47:       String hexString = Integer.toHexString(digestBytes[i]);
 48:       if (hexString.length() == 1)
 49:         hexString = "0"+hexString;
 50:       else if (hexString.length() == 8)
 51:         hexString = hexString.substring(6);
 52:       digestString.append(hexString);
 53:     }
 54:     return digestString.toString();
 55:   }
 56: 
 57: 
 58:   public void initialize(Subject subject, CallbackHandler callbackHandler,
 59:                          Map sharedState, Map options) {
 60:     this.subject = subject;
 61:     this.callbackHandler = callbackHandler;
 62:     this.sharedState = sharedState;
 63:     this.options = options;
 64: //    userFileName = (String)options.get("userfile");
 65:     userFileName = "moduledemo.conf";
 66:     userTable = new Hashtable();
 67:   }
 68: 
 69: 
 70:   protected void readUserFile() throws LoginException {
 71:     BufferedReader userFile;
 72: 
 73:     try {
 74:       userFile = new BufferedReader(new FileReader(userFileName));
 75:       String line;
 76:       if (debug)
 77:         System.out.println("\t\t[UserFileLoginModule] reading user file:");
 78:       while ((line = userFile.readLine()) != null) {
 79:         String[] splittedLine = line.split(":");
 80:         userTable.put(splittedLine[0], splittedLine[1]);
 81:         if (debug)
 82:           System.out.println("\t\t\t"+splittedLine[0]+":"+splittedLine[1]);
 83:       }
 84:       userFile.close();
 85:     }
 86:     catch(IOException e) {
 87:       throw new LoginException("Error opening/reading user file.");
 88:     }
 89:   }
 90: 
 91: 
 92:   public boolean login() throws LoginException {
 93:     char[] password;
 94: 
 95:     if (callbackHandler == null)
 96:       throw new LoginException("Error: no CallbackHandler available");
 97: 
 98:     if (userFileName == null)
 99:       throw new LoginException("Error: no user file specified");
100:     readUserFile();
101:     if (succeeded) {
102:       throw new LoginException();
103:     }
104:     userID = "duke";
105:     char[] tmpPassword = new char[] {'j','a','v','a'};
106:     if (tmpPassword == null) {
107:       tmpPassword = new char[0];
108:     }
109:     password = new char[tmpPassword.length];
110:     System.arraycopy(tmpPassword, 0,
111:                      password, 0, tmpPassword.length);
112:     MessageDigest md;
113:     String pwdHash, calculatedPwdHashStr;
114:     byte[] calculatedPwdHash;
115:     try {
116:       md = MessageDigest.getInstance("SHA1");
117:     }
118:     catch(NoSuchAlgorithmException e) {
119:       throw new LoginException("no SHA-1 implementation found");
120:     }
121:     md.update(userID.getBytes());
122:     md.update(String.valueOf(password).getBytes());
123:     calculatedPwdHash = md.digest();
124:     for (int i = 0; i < password.length; i++)
125:       password[i] = ' ';
126:     password = null;
127:     
128:     pwdHash = (String)userTable.get(userID);
129:     if (pwdHash == null) {
130:       succeeded = false;
131:     } else {
132:       calculatedPwdHashStr = getDigestAsHexString(calculatedPwdHash);
133:       if (debug) {
134:         System.out.println("\t\t[UserFileLoginModule]");
135:         System.out.println("\t\t  hash from file : "+pwdHash);
136:         System.out.println("\t\t  calculated hash: "+calculatedPwdHashStr);
137:       }
138:       succeeded = pwdHash.equals(calculatedPwdHashStr);
139:     }
140:     if (debug) {
141:       if (succeeded)
142:         System.out.println("\t\t[UserFileLoginModule] authentication succeeded");
143:       else
144:         System.out.println("\t\t[UserFileLoginModule] authentication failed");
145:     }
146:     if (!succeeded)
147:       throw new FailedLoginException("login failed");
148: 
149:     return true;
150:   }
151: 
152:   
153:   public boolean commit() throws LoginException {
154:     if (debug)
155:       System.out.println("\t\t[UserFileLoginModule] commit: "+succeeded);
156:     if (succeeded == false) {
157:       throw new LoginException("Cannot commit because login did not succeed.");
158:     } else if (commitSucceeded == true) {
159:       throw new LoginException("Already logged in.");
160:     } else {     
161:       userFilePrincipal = new UserFilePrincipal(userID);
162:       if (!subject.getPrincipals().contains(userFilePrincipal)) {
163:         subject.getPrincipals().add(userFilePrincipal);
164:         if (debug)
165:           System.out.println("\t\t[UserFileLoginModule] : added principal");
166:       }
167: 
168:       userID = null;    
169:       commitSucceeded = true;
170:       return true;
171:     }
172:   }
173: 
174:   public boolean abort() throws LoginException {
175:     if (debug)
176:       System.out.println("\t\t[UserFileLoginModule] abort: "+succeeded);
177:     if (succeeded == false) {
178:       throw new LoginException("Was not logged in before.");
179:     } else if (succeeded == true && commitSucceeded == false) {
180:       succeeded = false;
181:       userID = null;
182:       userFilePrincipal = null;
183:     } else {
184:       logout();
185:     }
186:     return true;
187:   }
188:   
189:   
190:   public boolean logout() throws LoginException {
191:     if (!succeeded) {
192:       throw new LoginException("Was not logged in before.");
193:     }
194:     subject.getPrincipals().remove(userFilePrincipal);
195:     if (debug)
196:       System.out.println("\t\t[UserFileLoginModule] : removed principal");
197:     commitSucceeded = false;
198:     succeeded = false;
199:     succeeded = commitSucceeded;
200:     userID = null;
201:     userFilePrincipal = null;
202:     return true;
203:   }
204: 
205:   
206: }
207: 
208: