|
| 1 | +package security; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Properties; |
| 8 | +import javax.security.auth.Subject; |
| 9 | +import javax.security.auth.callback.Callback; |
| 10 | +import javax.security.auth.callback.CallbackHandler; |
| 11 | +import javax.security.auth.callback.NameCallback; |
| 12 | +import javax.security.auth.callback.PasswordCallback; |
| 13 | +import javax.security.auth.callback.UnsupportedCallbackException; |
| 14 | +import javax.security.auth.login.LoginException; |
| 15 | +import javax.security.auth.spi.LoginModule; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +public class PropertyFileLoginModule implements LoginModule { |
| 20 | + private static final Logger log = LoggerFactory.getLogger(PropertyFileLoginModule.class); |
| 21 | + |
| 22 | + private Subject subject; |
| 23 | + private CallbackHandler callbackHandler; |
| 24 | + private boolean debug = false; |
| 25 | + private String file; |
| 26 | + private boolean succeeded = false; |
| 27 | + private String username; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void initialize( |
| 31 | + Subject subject, |
| 32 | + CallbackHandler callbackHandler, |
| 33 | + Map<String, ?> sharedState, |
| 34 | + Map<String, ?> options) { |
| 35 | + this.subject = subject; |
| 36 | + this.callbackHandler = callbackHandler; |
| 37 | + |
| 38 | + // Get configuration options |
| 39 | + this.debug = "true".equalsIgnoreCase((String) options.get("debug")); |
| 40 | + this.file = (String) options.get("file"); |
| 41 | + |
| 42 | + if (debug) { |
| 43 | + log.debug("PropertyFileLoginModule initialized with file: {}", file); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean login() throws LoginException { |
| 49 | + // If no file specified, this module can't authenticate |
| 50 | + if (file == null) { |
| 51 | + if (debug) log.debug("No property file specified"); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + // Get username and password from callbacks |
| 56 | + NameCallback nameCallback = new NameCallback("Username: "); |
| 57 | + PasswordCallback passwordCallback = new PasswordCallback("Password: ", false); |
| 58 | + |
| 59 | + try { |
| 60 | + callbackHandler.handle(new Callback[] {nameCallback, passwordCallback}); |
| 61 | + } catch (IOException | UnsupportedCallbackException e) { |
| 62 | + if (debug) log.debug("Error getting callbacks", e); |
| 63 | + throw new LoginException("Error during callback handling: " + e.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + this.username = nameCallback.getName(); |
| 67 | + char[] password = passwordCallback.getPassword(); |
| 68 | + passwordCallback.clearPassword(); |
| 69 | + |
| 70 | + if (username == null || username.isEmpty() || password == null) { |
| 71 | + if (debug) log.debug("Username or password is empty"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Load properties file |
| 76 | + Properties props = new Properties(); |
| 77 | + File propsFile = new File(file); |
| 78 | + |
| 79 | + if (!propsFile.exists() || !propsFile.isFile() || !propsFile.canRead()) { |
| 80 | + if (debug) log.debug("Cannot read property file: {}", file); |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + try (FileInputStream fis = new FileInputStream(propsFile)) { |
| 85 | + props.load(fis); |
| 86 | + } catch (IOException e) { |
| 87 | + if (debug) log.debug("Failed to load property file", e); |
| 88 | + throw new LoginException("Error loading property file: " + e.getMessage()); |
| 89 | + } |
| 90 | + |
| 91 | + // Check if username exists and password matches |
| 92 | + String storedPassword = props.getProperty(username); |
| 93 | + if (storedPassword == null) { |
| 94 | + if (debug) log.debug("User not found: {}", username); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + // Compare passwords |
| 99 | + succeeded = storedPassword.equals(new String(password)); |
| 100 | + |
| 101 | + if (debug) { |
| 102 | + if (succeeded) { |
| 103 | + log.debug("Authentication succeeded for user: {}", username); |
| 104 | + } else { |
| 105 | + log.debug("Authentication failed for user: {}", username); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return succeeded; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean commit() throws LoginException { |
| 114 | + if (!succeeded) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + // Add principal to the subject if authentication succeeded |
| 119 | + subject.getPrincipals().add(new DataHubUserPrincipal(username)); |
| 120 | + |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean abort() throws LoginException { |
| 126 | + succeeded = false; |
| 127 | + username = null; |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean logout() throws LoginException { |
| 133 | + // Remove principals that were added by this module |
| 134 | + subject.getPrincipals().removeIf(p -> p instanceof DataHubUserPrincipal); |
| 135 | + succeeded = false; |
| 136 | + username = null; |
| 137 | + return true; |
| 138 | + } |
| 139 | +} |
0 commit comments