View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.shared.controllers;
17  
18  import org.kuali.mobility.shared.Constants;
19  import org.kuali.mobility.shared.LoginService;
20  import org.kuali.mobility.shared.entity.MockUser;
21  import org.springframework.beans.factory.annotation.Autowired;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.ui.Model;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.validation.Errors;
26  import org.springframework.web.bind.annotation.ModelAttribute;
27  import org.springframework.web.bind.annotation.RequestMapping;
28  import org.springframework.web.bind.annotation.RequestMethod;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.security.MessageDigest;
33  import java.util.Formatter;
34  
35  @Controller
36  @RequestMapping("/mocklogin")
37  public class MockLoginController {
38  
39  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MockLoginController.class);
40  
41  	@Autowired
42  	private LoginService loginService;
43  
44  	@RequestMapping(method = RequestMethod.GET)
45  	public String mockLogin(HttpServletRequest request, HttpServletResponse response, Model uiModel) {
46  		MockUser mockUser = (MockUser) request.getSession().getAttribute(Constants.KME_MOCK_USER_KEY);
47  
48  		if (mockUser != null) {
49  			uiModel.addAttribute("mockuser", mockUser);
50  		} else {
51  			uiModel.addAttribute("mockuser", new MockUser());
52  		}
53  		return "mocklogin";
54  	}
55  
56  	@RequestMapping(method = RequestMethod.POST)
57  	public String submit(HttpServletRequest request, HttpServletResponse response, Model uiModel, @ModelAttribute("mockuser") MockUser mockUser, BindingResult result) {
58  		if (isValidQuery(mockUser, result)) {
59  			request.getSession().setAttribute(Constants.KME_MOCK_USER_KEY, mockUser);
60  
61  			String userName = mockUser.getUserId();
62  			String password = mockUser.getPassword();
63  
64  			if (loginService.isValidLogin(userName, password)) {
65  				return "redirect:/home";
66  			} else {
67  				return "mocklogin";
68  			}
69  
70  		} else {
71  			return "mocklogin";
72  		}
73  	}
74  
75  	private boolean isValidQuery(MockUser mockUser, BindingResult result) {
76  		boolean hasErrors = false;
77  		Errors errors = ((Errors) result);
78  		if (!loginService.isValidUser(mockUser.getUserId())) {
79  			errors.rejectValue("userId", "", "Please enter a registered username");
80  			hasErrors = true;
81  		}
82  		if (mockUser.getUserId() == null || "".equals(mockUser.getUserId().trim())) {
83  			errors.rejectValue("userId", "", "Please enter a username");
84  			hasErrors = true;
85  		}
86  		if (mockUser.getPassword() == null || "".equals(mockUser.getPassword().trim())) {
87  			errors.rejectValue("password", "", "Please enter a Password");
88  			hasErrors = true;
89  		}
90  		return !hasErrors;
91  	}
92  
93  	public static String calculateHash(MessageDigest algorithm,
94  	                                   String message) throws Exception {
95  
96  		algorithm.update(message.getBytes());
97  
98  		byte[] hash = algorithm.digest();
99  
100 		return byteArray2Hex(hash);
101 	}
102 
103 	private static String byteArray2Hex(byte[] hash) {
104 		Formatter formatter = new Formatter();
105 		for (byte b : hash) {
106 			formatter.format("%02x", b);
107 		}
108 		return formatter.toString();
109 	}
110 
111 	public LoginService getLoginService() {
112 		return loginService;
113 	}
114 
115 	public void setLoginService(LoginService loginService) {
116 		this.loginService = loginService;
117 	}
118 
119 }