1 package be
.nikiroo
.utils
;
3 import java
.util
.ArrayList
;
7 * A simple login facility using cookies.
11 public class LoginResult
{
12 private boolean success
;
13 private String cookie
;
14 private boolean badLogin
;
15 private boolean badCookie
;
16 private String option
;
19 * Generate a failed login.
22 * TRUE if the login failed because of a who/key/subkey error
24 * TRUE if the login failed because of a bad cookie
27 public LoginResult(boolean badLogin
, boolean badCookie
) {
28 this.badLogin
= badLogin
;
29 this.badCookie
= badCookie
;
33 * Generate a successful login for the given user.
36 * the user (can be NULL)
38 * the password (can be NULL)
40 * a sub-password (can be NULL)
42 * an option assigned to this user (can be NULL)
44 public LoginResult(String who
, String key
, String subkey
, String option
) {
45 String wookie
= CookieUtils
.generateCookie(who
+ key
, 0);
48 this.cookie
= wookie
+ "~"
49 + CookieUtils
.generateCookie(
50 wookie
+ (subkey
== null ?
"" : subkey
) + option
, 0)
56 * Generate a login via this token and checks its validity.
58 * Will fail with a NULL <tt>token</tt>, but
59 * {@link LoginResult#isBadCookie()} will still be false.
62 * the token to check (if NULL, will simply fail but
63 * {@link LoginResult#isBadCookie()} will still be false)
65 * the user (can be NULL)
67 * the password (can be NULL)
69 public LoginResult(String cookie
, String who
, String key
) {
70 this(cookie
, who
, key
, null, true);
74 * Generate a login via this token and checks its validity.
76 * Will fail with a NULL <tt>token</tt>, but
77 * {@link LoginResult#isBadCookie()} will still be false.
80 * the token to check (if NULL, will simply fail but
81 * {@link LoginResult#isBadCookie()} will still be false)
83 * the user (can be NULL)
85 * the password (can be NULL)
87 * the list of candidate subkey (can be NULL)
88 * @param allowNoSubkey
89 * allow the login if no subkey was present in the token
91 public LoginResult(String cookie
, String who
, String key
,
92 List
<String
> subkeys
, boolean allowNoSubkey
) {
94 String hashes
[] = cookie
.split("~");
95 if (hashes
.length
>= 2) {
96 String wookie
= hashes
[0];
97 String rehashed
= hashes
[1];
98 String opts
= hashes
.length
> 2 ? hashes
[2] : "";
100 if (CookieUtils
.validateCookie(who
+ key
, wookie
)) {
101 if (subkeys
== null) {
102 subkeys
= new ArrayList
<String
>();
106 subkeys
= new ArrayList
<String
>(subkeys
);
110 for (String subkey
: subkeys
) {
111 if (CookieUtils
.validateCookie(wookie
+ subkey
+ opts
,
113 wookie
= CookieUtils
.generateCookie(who
+ key
, 0);
114 this.cookie
= CookieUtils
115 .generateCookie(wookie
+ subkey
+ opts
, 0);
123 this.badCookie
= !success
;
126 // No token -> no bad token
130 * The login wa successful.
132 * @return TRUE if it is
134 public boolean isSuccess() {
139 * The refreshed token if the login is successful (NULL if not).
141 * @return the token, or NULL
143 public String
getCookie() {
148 * An option that was used to generate this login (always NULL if the login
149 * was not successful).
151 * It can come from a manually generated {@link LoginResult}, but also from
152 * a {@link LoginResult} generated with a token.
156 public String
getOption() {
161 * The login failed because of a who/key/subkey error.
163 * @return TRUE if it failed because of a who/key/subkey error
165 public boolean isBadLogin() {
170 * The login failed because the cookie was not accepted
172 * @return TRUE if it failed because the cookie was not accepted
174 public boolean isBadCookie() {
179 public String
toString() {
181 return "Login succeeded";
183 if (badLogin
&& badCookie
)
184 return "Login failed because of bad login and bad cookie";
187 return "Login failed because of bad login";
190 return "Login failed because of bad cookie";
192 return "Login failed without giving a reason";