PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sk/ibm/sa/utils/LoginUtils.java

https://gitlab.com/unique_steve/Ivo_app
Java | 59 lines | 44 code | 11 blank | 4 comment | 3 complexity | 8d8321bfddcd232599458635b1c74c36 MD5 | raw file
  1. package sk.ibm.sa.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import javax.faces.context.FacesContext;
  8. import javax.servlet.http.HttpSession;
  9. public class LoginUtils {
  10. public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
  11. Pattern.CASE_INSENSITIVE);
  12. public static final String NICK_NAME = "nickName";
  13. public static void setHttpSessionEmailAttribute(String nickName) {
  14. getHttpSession().setAttribute(NICK_NAME, nickName);
  15. }
  16. /**
  17. *
  18. * @return String email if user is logged, otherwise return null
  19. */
  20. public static String getHttpSessionEmailAttribute() {
  21. HttpSession httpSession = getHttpSession();
  22. if (httpSession == null) {
  23. return null;
  24. } else {
  25. return (String) httpSession.getAttribute(NICK_NAME);
  26. }
  27. }
  28. public static boolean isEmailValid(String email) {
  29. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email);
  30. return matcher.find();
  31. }
  32. public static String hash(String s) {
  33. try {
  34. MessageDigest md = MessageDigest.getInstance("md5");
  35. md.reset();
  36. md.update(s.getBytes());
  37. byte[] digest = md.digest();
  38. BigInteger bigInt = new BigInteger(1, digest);
  39. return bigInt.toString(16);
  40. } catch (NoSuchAlgorithmException e) {
  41. e.printStackTrace();
  42. return null;
  43. }
  44. }
  45. public static HttpSession getHttpSession() {
  46. return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
  47. }
  48. }