weblib: do not allow click on last image
[fanfix.git] / src / be / nikiroo / utils / LoginResult.java
CommitLineData
c8ee85f1
NR
1package be.nikiroo.utils;
2
3import java.util.ArrayList;
4import java.util.List;
5
6/**
7 * A simple login facility using cookies.
8 *
9 * @author niki
10 */
11public class LoginResult {
12 private boolean success;
13 private String cookie;
14 private boolean badLogin;
15 private boolean badCookie;
16 private String option;
17
18 /**
19 * Generate a failed login.
20 *
21 * @param badLogin
22 * TRUE if the login failed because of a who/key/subkey error
23 * @param badCookie
24 * TRUE if the login failed because of a bad cookie
25 *
26 */
27 public LoginResult(boolean badLogin, boolean badCookie) {
28 this.badLogin = badLogin;
29 this.badCookie = badCookie;
30 }
31
32 /**
33 * Generate a successful login for the given user.
34 *
35 * @param who
36 * the user (can be NULL)
37 * @param key
38 * the password (can be NULL)
39 * @param subkey
40 * a sub-password (can be NULL)
41 * @param option
b6aad7af 42 * an option assigned to this login (can be NULL)
c8ee85f1
NR
43 */
44 public LoginResult(String who, String key, String subkey, String option) {
c8ee85f1 45 this.option = option;
b6aad7af 46 this.cookie = generateCookie(who, key, subkey, option);
c8ee85f1
NR
47 this.success = true;
48 }
49
50 /**
51 * Generate a login via this token and checks its validity.
52 * <p>
53 * Will fail with a NULL <tt>token</tt>, but
54 * {@link LoginResult#isBadCookie()} will still be false.
55 *
56 * @param cookie
57 * the token to check (if NULL, will simply fail but
58 * {@link LoginResult#isBadCookie()} will still be false)
59 * @param who
60 * the user (can be NULL)
61 * @param key
62 * the password (can be NULL)
63 */
64 public LoginResult(String cookie, String who, String key) {
65 this(cookie, who, key, null, true);
66 }
67
68 /**
69 * Generate a login via this token and checks its validity.
70 * <p>
71 * Will fail with a NULL <tt>token</tt>, but
72 * {@link LoginResult#isBadCookie()} will still be false.
73 *
74 * @param cookie
75 * the token to check (if NULL, will simply fail but
76 * {@link LoginResult#isBadCookie()} will still be false)
77 * @param who
78 * the user (can be NULL)
79 * @param key
80 * the password (can be NULL)
81 * @param subkeys
82 * the list of candidate subkey (can be NULL)
83 * @param allowNoSubkey
84 * allow the login if no subkey was present in the token
85 */
86 public LoginResult(String cookie, String who, String key,
87 List<String> subkeys, boolean allowNoSubkey) {
88 if (cookie != null) {
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] : "";
94
95 if (CookieUtils.validateCookie(who + key, wookie)) {
96 if (subkeys == null) {
97 subkeys = new ArrayList<String>();
98 }
99
100 if (allowNoSubkey) {
101 subkeys = new ArrayList<String>(subkeys);
102 subkeys.add("");
103 }
104
105 for (String subkey : subkeys) {
106 if (CookieUtils.validateCookie(wookie + subkey + opts,
107 rehashed)) {
b6aad7af
NR
108 this.cookie = generateCookie(who, key, subkey,
109 opts);
c8ee85f1
NR
110 this.option = opts;
111 this.success = true;
112 }
113 }
114 }
115 }
116
117 this.badCookie = !success;
118 }
119
120 // No token -> no bad token
121 }
122
123 /**
124 * The login wa successful.
125 *
126 * @return TRUE if it is
127 */
128 public boolean isSuccess() {
129 return success;
130 }
131
132 /**
133 * The refreshed token if the login is successful (NULL if not).
134 *
135 * @return the token, or NULL
136 */
137 public String getCookie() {
138 return cookie;
139 }
140
141 /**
142 * An option that was used to generate this login (always NULL if the login
143 * was not successful).
144 * <p>
145 * It can come from a manually generated {@link LoginResult}, but also from
146 * a {@link LoginResult} generated with a token.
147 *
148 * @return the option
149 */
150 public String getOption() {
151 return option;
152 }
153
154 /**
155 * The login failed because of a who/key/subkey error.
156 *
157 * @return TRUE if it failed because of a who/key/subkey error
158 */
159 public boolean isBadLogin() {
160 return badLogin;
161 }
162
163 /**
164 * The login failed because the cookie was not accepted
165 *
166 * @return TRUE if it failed because the cookie was not accepted
167 */
168 public boolean isBadCookie() {
169 return badCookie;
170 }
171
172 @Override
173 public String toString() {
174 if (success)
175 return "Login succeeded";
176
177 if (badLogin && badCookie)
178 return "Login failed because of bad login and bad cookie";
179
180 if (badLogin)
181 return "Login failed because of bad login";
182
183 if (badCookie)
184 return "Login failed because of bad cookie";
185
186 return "Login failed without giving a reason";
187 }
b6aad7af
NR
188
189 /**
190 * Generate a cookie.
191 *
192 * @param who
193 * the user name (can be NULL)
194 * @param key
195 * the password (can be NULL)
196 * @param subkey
197 * a subkey (can be NULL)
198 * @param option
199 * an option linked to the login (can be NULL)
200 *
201 * @return a fresh cookie
202 */
203 private String generateCookie(String who, String key, String subkey,
204 String option) {
205 String wookie = CookieUtils.generateCookie(who + key, 0);
206 return wookie + "~"
207 + CookieUtils.generateCookie(
208 wookie + (subkey == null ? "" : subkey) + option, 0)
209 + "~" + option;
210 }
c8ee85f1 211}