| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | package security;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 13:22:18 -07:00
										 |  |  | import com.google.common.base.Preconditions;
 | 
					
						
							| 
									
										
										
										
											2022-05-06 16:18:20 -05:00
										 |  |  | import java.util.Collections;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import javax.annotation.Nonnull;
 | 
					
						
							| 
									
										
										
										
											2017-04-06 22:01:29 -07:00
										 |  |  | import javax.naming.AuthenticationException;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import javax.security.auth.callback.Callback;
 | 
					
						
							|  |  |  | import javax.security.auth.callback.CallbackHandler;
 | 
					
						
							|  |  |  | import javax.security.auth.callback.NameCallback;
 | 
					
						
							|  |  |  | import javax.security.auth.callback.PasswordCallback;
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  | import javax.security.auth.login.LoginContext;
 | 
					
						
							|  |  |  | import javax.security.auth.login.LoginException;
 | 
					
						
							| 
									
										
										
										
											2022-05-06 16:18:20 -05:00
										 |  |  | import org.apache.commons.lang3.StringUtils;
 | 
					
						
							|  |  |  | import org.eclipse.jetty.jaas.JAASLoginService;
 | 
					
						
							|  |  |  | import org.eclipse.jetty.jaas.PropertyUserStoreManager;
 | 
					
						
							|  |  |  | import play.Logger;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 10:10:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | public class AuthenticationManager {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 18:42:19 -05:00
										 |  |  |   private AuthenticationManager(boolean verbose) {
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 14:21:55 -07:00
										 |  |  |   public static void authenticateJaasUser(@Nonnull String userName, @Nonnull String password) throws Exception {
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     Preconditions.checkArgument(!StringUtils.isAnyEmpty(userName), "Username cannot be empty");
 | 
					
						
							| 
									
										
										
										
											2022-09-21 14:21:55 -07:00
										 |  |  |     JAASLoginService jaasLoginService = new JAASLoginService("WHZ-Authentication");
 | 
					
						
							|  |  |  |     PropertyUserStoreManager propertyUserStoreManager = new PropertyUserStoreManager();
 | 
					
						
							|  |  |  |     propertyUserStoreManager.start();
 | 
					
						
							|  |  |  |     jaasLoginService.setBeans(Collections.singletonList(propertyUserStoreManager));
 | 
					
						
							|  |  |  |     JAASLoginService.INSTANCE.set(jaasLoginService);
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  |     try {
 | 
					
						
							| 
									
										
										
										
											2018-07-31 10:10:52 -07:00
										 |  |  |       LoginContext lc = new LoginContext("WHZ-Authentication", new WHZCallbackHandler(userName, password));
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  |       lc.login();
 | 
					
						
							|  |  |  |     } catch (LoginException le) {
 | 
					
						
							| 
									
										
										
										
											2023-06-07 18:42:19 -05:00
										 |  |  |       AuthenticationException authenticationException = new AuthenticationException(le.getMessage());
 | 
					
						
							|  |  |  |       authenticationException.setRootCause(le);
 | 
					
						
							|  |  |  |       throw authenticationException;
 | 
					
						
							| 
									
										
										
										
											2017-04-06 22:01:29 -07:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2016-10-13 14:27:02 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 15:39:24 -07:00
										 |  |  |   private static class WHZCallbackHandler implements CallbackHandler {
 | 
					
						
							| 
									
										
										
										
											2021-07-02 06:31:01 -07:00
										 |  |  |     private String password;
 | 
					
						
							|  |  |  |     private String username;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private WHZCallbackHandler(@Nonnull String username, @Nonnull String password) {
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  |       this.username = username;
 | 
					
						
							|  |  |  |       this.password = password;
 | 
					
						
							| 
									
										
										
										
											2016-10-13 14:27:02 -07:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2018-07-31 10:10:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     public void handle(@Nonnull Callback[] callbacks) {
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  |       NameCallback nc = null;
 | 
					
						
							|  |  |  |       PasswordCallback pc = null;
 | 
					
						
							|  |  |  |       for (Callback callback : callbacks) {
 | 
					
						
							| 
									
										
										
										
											2022-11-29 10:44:55 -06:00
										 |  |  |         Logger.debug("The submitted callback is of type: " + callback.getClass() + " : " + callback);
 | 
					
						
							| 
									
										
										
										
											2018-07-27 16:33:52 -07:00
										 |  |  |         if (callback instanceof NameCallback) {
 | 
					
						
							|  |  |  |           nc = (NameCallback) callback;
 | 
					
						
							|  |  |  |           nc.setName(this.username);
 | 
					
						
							|  |  |  |         } else if (callback instanceof PasswordCallback) {
 | 
					
						
							|  |  |  |           pc = (PasswordCallback) callback;
 | 
					
						
							|  |  |  |           pc.setPassword(this.password.toCharArray());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2016-10-13 14:27:02 -07:00
										 |  |  |       }
 | 
					
						
							| 
									
										
										
										
											2016-03-10 16:32:37 -08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2016-10-13 14:27:02 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | }
 |