From f0e3bdbb99c3aad401b9fd4249f97273dbfc31b0 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 13 May 2020 20:42:43 +0200 Subject: [PATCH] new LoginResult helper class --- CookieUtils.java | 5 ++ LoginResult.java | 194 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 LoginResult.java diff --git a/CookieUtils.java b/CookieUtils.java index f0820260..8d307a22 100644 --- a/CookieUtils.java +++ b/CookieUtils.java @@ -2,6 +2,11 @@ package be.nikiroo.utils; import java.util.Date; +/** + * Some utilities for cookie management. + * + * @author niki + */ public class CookieUtils { /** * The number of seconds for the period (we accept the current or the diff --git a/LoginResult.java b/LoginResult.java new file mode 100644 index 00000000..dadd16bc --- /dev/null +++ b/LoginResult.java @@ -0,0 +1,194 @@ +package be.nikiroo.utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * A simple login facility using cookies. + * + * @author niki + */ +public class LoginResult { + private boolean success; + private String cookie; + private boolean badLogin; + private boolean badCookie; + private String option; + + /** + * Generate a failed login. + * + * @param badLogin + * TRUE if the login failed because of a who/key/subkey error + * @param badCookie + * TRUE if the login failed because of a bad cookie + * + */ + public LoginResult(boolean badLogin, boolean badCookie) { + this.badLogin = badLogin; + this.badCookie = badCookie; + } + + /** + * Generate a successful login for the given user. + * + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + * @param subkey + * a sub-password (can be NULL) + * @param option + * an option assigned to this user (can be NULL) + */ + public LoginResult(String who, String key, String subkey, String option) { + String wookie = CookieUtils.generateCookie(who + key, 0); + + this.option = option; + this.cookie = wookie + "~" + + CookieUtils.generateCookie( + wookie + (subkey == null ? "" : subkey) + option, 0) + + "~" + option; + this.success = true; + } + + /** + * Generate a login via this token and checks its validity. + *

+ * Will fail with a NULL token, but + * {@link LoginResult#isBadCookie()} will still be false. + * + * @param cookie + * the token to check (if NULL, will simply fail but + * {@link LoginResult#isBadCookie()} will still be false) + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + */ + public LoginResult(String cookie, String who, String key) { + this(cookie, who, key, null, true); + } + + /** + * Generate a login via this token and checks its validity. + *

+ * Will fail with a NULL token, but + * {@link LoginResult#isBadCookie()} will still be false. + * + * @param cookie + * the token to check (if NULL, will simply fail but + * {@link LoginResult#isBadCookie()} will still be false) + * @param who + * the user (can be NULL) + * @param key + * the password (can be NULL) + * @param subkeys + * the list of candidate subkey (can be NULL) + * @param allowNoSubkey + * allow the login if no subkey was present in the token + */ + public LoginResult(String cookie, String who, String key, + List subkeys, boolean allowNoSubkey) { + if (cookie != null) { + String hashes[] = cookie.split("~"); + if (hashes.length >= 2) { + String wookie = hashes[0]; + String rehashed = hashes[1]; + String opts = hashes.length > 2 ? hashes[2] : ""; + + if (CookieUtils.validateCookie(who + key, wookie)) { + if (subkeys == null) { + subkeys = new ArrayList(); + } + + if (allowNoSubkey) { + subkeys = new ArrayList(subkeys); + subkeys.add(""); + } + + for (String subkey : subkeys) { + if (CookieUtils.validateCookie(wookie + subkey + opts, + rehashed)) { + wookie = CookieUtils.generateCookie(who + key, 0); + this.cookie = CookieUtils + .generateCookie(wookie + subkey + opts, 0); + this.option = opts; + this.success = true; + } + } + } + } + + this.badCookie = !success; + } + + // No token -> no bad token + } + + /** + * The login wa successful. + * + * @return TRUE if it is + */ + public boolean isSuccess() { + return success; + } + + /** + * The refreshed token if the login is successful (NULL if not). + * + * @return the token, or NULL + */ + public String getCookie() { + return cookie; + } + + /** + * An option that was used to generate this login (always NULL if the login + * was not successful). + *

+ * It can come from a manually generated {@link LoginResult}, but also from + * a {@link LoginResult} generated with a token. + * + * @return the option + */ + public String getOption() { + return option; + } + + /** + * The login failed because of a who/key/subkey error. + * + * @return TRUE if it failed because of a who/key/subkey error + */ + public boolean isBadLogin() { + return badLogin; + } + + /** + * The login failed because the cookie was not accepted + * + * @return TRUE if it failed because the cookie was not accepted + */ + public boolean isBadCookie() { + return badCookie; + } + + @Override + public String toString() { + if (success) + return "Login succeeded"; + + if (badLogin && badCookie) + return "Login failed because of bad login and bad cookie"; + + if (badLogin) + return "Login failed because of bad login"; + + if (badCookie) + return "Login failed because of bad cookie"; + + return "Login failed without giving a reason"; + } +} \ No newline at end of file -- 2.27.0