Commit | Line | Data |
---|---|---|
c8ee85f1 NR |
1 | package be.nikiroo.utils; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | /** | |
7 | * A simple login facility using cookies. | |
8 | * | |
9 | * @author niki | |
10 | */ | |
11 | public 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 | |
42 | * an option assigned to this user (can be NULL) | |
43 | */ | |
44 | public LoginResult(String who, String key, String subkey, String option) { | |
45 | String wookie = CookieUtils.generateCookie(who + key, 0); | |
46 | ||
47 | this.option = option; | |
48 | this.cookie = wookie + "~" | |
49 | + CookieUtils.generateCookie( | |
50 | wookie + (subkey == null ? "" : subkey) + option, 0) | |
51 | + "~" + option; | |
52 | this.success = true; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Generate a login via this token and checks its validity. | |
57 | * <p> | |
58 | * Will fail with a NULL <tt>token</tt>, but | |
59 | * {@link LoginResult#isBadCookie()} will still be false. | |
60 | * | |
61 | * @param cookie | |
62 | * the token to check (if NULL, will simply fail but | |
63 | * {@link LoginResult#isBadCookie()} will still be false) | |
64 | * @param who | |
65 | * the user (can be NULL) | |
66 | * @param key | |
67 | * the password (can be NULL) | |
68 | */ | |
69 | public LoginResult(String cookie, String who, String key) { | |
70 | this(cookie, who, key, null, true); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Generate a login via this token and checks its validity. | |
75 | * <p> | |
76 | * Will fail with a NULL <tt>token</tt>, but | |
77 | * {@link LoginResult#isBadCookie()} will still be false. | |
78 | * | |
79 | * @param cookie | |
80 | * the token to check (if NULL, will simply fail but | |
81 | * {@link LoginResult#isBadCookie()} will still be false) | |
82 | * @param who | |
83 | * the user (can be NULL) | |
84 | * @param key | |
85 | * the password (can be NULL) | |
86 | * @param subkeys | |
87 | * the list of candidate subkey (can be NULL) | |
88 | * @param allowNoSubkey | |
89 | * allow the login if no subkey was present in the token | |
90 | */ | |
91 | public LoginResult(String cookie, String who, String key, | |
92 | List<String> subkeys, boolean allowNoSubkey) { | |
93 | if (cookie != null) { | |
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] : ""; | |
99 | ||
100 | if (CookieUtils.validateCookie(who + key, wookie)) { | |
101 | if (subkeys == null) { | |
102 | subkeys = new ArrayList<String>(); | |
103 | } | |
104 | ||
105 | if (allowNoSubkey) { | |
106 | subkeys = new ArrayList<String>(subkeys); | |
107 | subkeys.add(""); | |
108 | } | |
109 | ||
110 | for (String subkey : subkeys) { | |
111 | if (CookieUtils.validateCookie(wookie + subkey + opts, | |
112 | rehashed)) { | |
113 | wookie = CookieUtils.generateCookie(who + key, 0); | |
114 | this.cookie = CookieUtils | |
115 | .generateCookie(wookie + subkey + opts, 0); | |
116 | this.option = opts; | |
117 | this.success = true; | |
118 | } | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | this.badCookie = !success; | |
124 | } | |
125 | ||
126 | // No token -> no bad token | |
127 | } | |
128 | ||
129 | /** | |
130 | * The login wa successful. | |
131 | * | |
132 | * @return TRUE if it is | |
133 | */ | |
134 | public boolean isSuccess() { | |
135 | return success; | |
136 | } | |
137 | ||
138 | /** | |
139 | * The refreshed token if the login is successful (NULL if not). | |
140 | * | |
141 | * @return the token, or NULL | |
142 | */ | |
143 | public String getCookie() { | |
144 | return cookie; | |
145 | } | |
146 | ||
147 | /** | |
148 | * An option that was used to generate this login (always NULL if the login | |
149 | * was not successful). | |
150 | * <p> | |
151 | * It can come from a manually generated {@link LoginResult}, but also from | |
152 | * a {@link LoginResult} generated with a token. | |
153 | * | |
154 | * @return the option | |
155 | */ | |
156 | public String getOption() { | |
157 | return option; | |
158 | } | |
159 | ||
160 | /** | |
161 | * The login failed because of a who/key/subkey error. | |
162 | * | |
163 | * @return TRUE if it failed because of a who/key/subkey error | |
164 | */ | |
165 | public boolean isBadLogin() { | |
166 | return badLogin; | |
167 | } | |
168 | ||
169 | /** | |
170 | * The login failed because the cookie was not accepted | |
171 | * | |
172 | * @return TRUE if it failed because the cookie was not accepted | |
173 | */ | |
174 | public boolean isBadCookie() { | |
175 | return badCookie; | |
176 | } | |
177 | ||
178 | @Override | |
179 | public String toString() { | |
180 | if (success) | |
181 | return "Login succeeded"; | |
182 | ||
183 | if (badLogin && badCookie) | |
184 | return "Login failed because of bad login and bad cookie"; | |
185 | ||
186 | if (badLogin) | |
187 | return "Login failed because of bad login"; | |
188 | ||
189 | if (badCookie) | |
190 | return "Login failed because of bad cookie"; | |
191 | ||
192 | return "Login failed without giving a reason"; | |
193 | } | |
194 | } |