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 login (can be NULL)
44 public LoginResult(String who
, String key
, String subkey
, String option
) {
46 this.cookie
= generateCookie(who
, key
, subkey
, option
);
51 * Generate a login via this token and checks its validity.
53 * Will fail with a NULL <tt>token</tt>, but
54 * {@link LoginResult#isBadCookie()} will still be false.
57 * the token to check (if NULL, will simply fail but
58 * {@link LoginResult#isBadCookie()} will still be false)
60 * the user (can be NULL)
62 * the password (can be NULL)
64 public LoginResult(String cookie
, String who
, String key
) {
65 this(cookie
, who
, key
, null, true);
69 * Generate a login via this token and checks its validity.
71 * Will fail with a NULL <tt>token</tt>, but
72 * {@link LoginResult#isBadCookie()} will still be false.
75 * the token to check (if NULL, will simply fail but
76 * {@link LoginResult#isBadCookie()} will still be false)
78 * the user (can be NULL)
80 * the password (can be NULL)
82 * the list of candidate subkey (can be NULL)
83 * @param allowNoSubkey
84 * allow the login if no subkey was present in the token
86 public LoginResult(String cookie
, String who
, String key
,
87 List
<String
> subkeys
, boolean allowNoSubkey
) {
89 String hashes
[] = cookie
.split("~");
90 if (hashes
.length
>= 2) {
91 String wookie
= hashes
[0];
92 String rehashed
= hashes
[1];
93 String opts
= hashes
.length
> 2 ? hashes
[2] : "";
95 if (CookieUtils
.validateCookie(who
+ key
, wookie
)) {
96 if (subkeys
== null) {
97 subkeys
= new ArrayList
<String
>();
101 subkeys
= new ArrayList
<String
>(subkeys
);
105 for (String subkey
: subkeys
) {
106 if (CookieUtils
.validateCookie(wookie
+ subkey
+ opts
,
108 this.cookie
= generateCookie(who
, key
, subkey
,
117 this.badCookie
= !success
;
120 // No token -> no bad token
124 * The login wa successful.
126 * @return TRUE if it is
128 public boolean isSuccess() {
133 * The refreshed token if the login is successful (NULL if not).
135 * @return the token, or NULL
137 public String
getCookie() {
142 * An option that was used to generate this login (always NULL if the login
143 * was not successful).
145 * It can come from a manually generated {@link LoginResult}, but also from
146 * a {@link LoginResult} generated with a token.
150 public String
getOption() {
155 * The login failed because of a who/key/subkey error.
157 * @return TRUE if it failed because of a who/key/subkey error
159 public boolean isBadLogin() {
164 * The login failed because the cookie was not accepted
166 * @return TRUE if it failed because the cookie was not accepted
168 public boolean isBadCookie() {
173 public String
toString() {
175 return "Login succeeded";
177 if (badLogin
&& badCookie
)
178 return "Login failed because of bad login and bad cookie";
181 return "Login failed because of bad login";
184 return "Login failed because of bad cookie";
186 return "Login failed without giving a reason";
193 * the user name (can be NULL)
195 * the password (can be NULL)
197 * a subkey (can be NULL)
199 * an option linked to the login (can be NULL)
201 * @return a fresh cookie
203 private String
generateCookie(String who
, String key
, String subkey
,
205 String wookie
= CookieUtils
.generateCookie(who
+ key
, 0);
207 + CookieUtils
.generateCookie(
208 wookie
+ (subkey
== null ?
"" : subkey
) + option
, 0)