You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.0 KiB
121 lines
4.0 KiB
package de.srsoftware.widerhall; |
|
|
|
import de.srsoftware.tools.translations.Translation; |
|
import de.srsoftware.widerhall.data.MailingList; |
|
import de.srsoftware.widerhall.data.User; |
|
|
|
import javax.mail.internet.AddressException; |
|
import javax.mail.internet.InternetAddress; |
|
import javax.servlet.http.HttpServletRequest; |
|
import java.net.URLEncoder; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.util.Map; |
|
import java.util.stream.Collectors; |
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
|
|
public class Util { |
|
|
|
private static final MessageDigest SHA256 = getSha256(); |
|
private static final String EMAIL_PATTERN = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; |
|
|
|
public static String urlEncode(Map<String, Object> data) { |
|
String params = data.entrySet() |
|
.stream() |
|
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) |
|
.collect(Collectors.joining("&")); |
|
return params; |
|
} |
|
|
|
private static String encode(Object value) { |
|
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8); |
|
} |
|
|
|
|
|
public static MessageDigest getSha256() { |
|
try { |
|
return MessageDigest.getInstance("SHA-256"); |
|
} catch (NoSuchAlgorithmException e) { |
|
e.printStackTrace(); |
|
return null; |
|
} |
|
} |
|
|
|
public static String sha256(String s) { |
|
byte[] bytes = SHA256.digest(s.getBytes(StandardCharsets.UTF_8)); |
|
return hex(bytes); |
|
} |
|
|
|
private static String hex(byte[] bytes) { |
|
StringBuffer buf = new StringBuffer(bytes.length*2); |
|
for (var byt : bytes) buf.append(hex(byt)); |
|
return buf.toString(); |
|
} |
|
|
|
public static String hex(int b){ |
|
int lower = b & 0x0F; |
|
int upper = (b & 0xF0) >> 4; |
|
return (char)(upper < 10 ? '0'+upper : 'A'+upper-10)+""+(char)(lower < 10 ? '0'+lower : 'A'+lower-10); |
|
} |
|
|
|
public static String t(String tx, Object ... fills){ |
|
return Translation.get(Application.class,tx,fills); |
|
} |
|
|
|
public static boolean isEmail(String email) { |
|
return email.matches(EMAIL_PATTERN); |
|
} |
|
|
|
public static boolean simplePassword(String pass) { |
|
if (pass.length() < 6) return true; |
|
if (pass.length() < 8){ |
|
for (int i=0; i<pass.length();i++){ |
|
if (!Character.isLetterOrDigit(pass.charAt(i))) return false; // password contains special character |
|
} |
|
return true; |
|
} |
|
if (pass.length() < 10){ |
|
var digit = false; |
|
var letter = false; |
|
for (int i=0; i<pass.length();i++){ |
|
char c = pass.charAt(i); |
|
if (!Character.isLetterOrDigit(c)) return false; // password contains special character |
|
digit |= Character.isDigit(c); |
|
letter |= Character.isLetter(c); |
|
if (letter && digit) return false; // password contains letters and digits |
|
} |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
public static int unset(int value, int...flags) { |
|
for (int flag : flags){ |
|
if ((value & flag) > 0) value ^= flag; |
|
} |
|
return value; |
|
} |
|
|
|
public static User getUser(HttpServletRequest req) { |
|
var o = req.getSession().getAttribute(USER); |
|
return o instanceof User ? (User) o : null; |
|
} |
|
|
|
public static String getPath(HttpServletRequest req) { |
|
var path = req.getPathInfo(); |
|
return path == null ? INDEX : path.substring(1); |
|
|
|
} |
|
|
|
public static MailingList getMailingList(HttpServletRequest req) { |
|
var listEmail = req.getParameter(LIST); |
|
if (listEmail == null || listEmail.isBlank()) return null; |
|
return MailingList.load(listEmail); |
|
} |
|
|
|
public static boolean getCheckbox(HttpServletRequest req, String key) { |
|
return "on".equals(req.getParameter(key)); |
|
} |
|
}
|
|
|