-
Notifications
You must be signed in to change notification settings - Fork 195
/
PsBasic.java
363 lines (340 loc) · 11.4 KB
/
PsBasic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
import lombok.EqualsAndHashCode;
import org.cactoos.text.TrimmedText;
import org.takes.Request;
import org.takes.Response;
import org.takes.facets.flash.RsFlash;
import org.takes.facets.forward.RsForward;
import org.takes.misc.Opt;
import org.takes.misc.Utf8String;
import org.takes.rq.RqHeaders;
import org.takes.rq.RqHref;
import org.takes.rs.RsWithHeader;
/**
* Pass that checks the user according RFC-2617.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.20
*/
@EqualsAndHashCode
@SuppressWarnings("PMD.TooManyMethods")
public final class PsBasic implements Pass {
/**
* Pattern for basic authorization name.
*/
private static final Pattern AUTH = Pattern.compile("Basic");
/**
* Entry to validate user information.
*/
private final PsBasic.Entry entry;
/**
* Realm.
*/
private final String realm;
/**
* Ctor.
* @param rlm Realm
* @param basic Entry
*/
public PsBasic(final String rlm, final PsBasic.Entry basic) {
this.realm = rlm;
this.entry = basic;
}
@Override
public Opt<Identity> enter(final Request request) throws IOException {
final Iterator<String> headers = new RqHeaders.Smart(
new RqHeaders.Base(request)
).header("authorization").iterator();
if (!headers.hasNext()) {
throw new RsForward(
new RsWithHeader(
String.format(
"WWW-Authenticate: Basic ream=\"%s\" ",
this.realm
)
),
HttpURLConnection.HTTP_UNAUTHORIZED,
new RqHref.Base(request).href()
);
}
final String decoded = new TrimmedText(new Utf8String(
DatatypeConverter.parseBase64Binary(
PsBasic.AUTH.split(headers.next())[1]
)
)).asString();
final String user = decoded.split(":")[0];
final Opt<Identity> identity = this.entry.enter(
user,
decoded.substring(user.length() + 1)
);
if (!identity.has()) {
throw new RsForward(
new RsWithHeader(
new RsFlash("access denied", Level.WARNING),
String.format(
"WWW-Authenticate: Basic ream=\"%s\"",
this.realm
)
),
HttpURLConnection.HTTP_UNAUTHORIZED,
new RqHref.Base(request).href()
);
}
return identity;
}
@Override
public Response exit(final Response response, final Identity identity) {
return response;
}
/**
* Entry interface that is used to check if the received information is
* valid.
*
* @since 0.20
*/
public interface Entry {
/**
* Check if is a valid user.
* @param user User
* @param pwd Password
* @return Identity.
*/
Opt<Identity> enter(String user, String pwd);
}
/**
* Fake implementation of {@link PsBasic.Entry}.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.20
*/
public static final class Fake implements PsBasic.Entry {
/**
* Should we authenticate a user?
*/
private final boolean condition;
/**
* Ctor.
* @param cond Condition
*/
public Fake(final boolean cond) {
this.condition = cond;
}
@Override
public Opt<Identity> enter(final String usr, final String pwd) {
final Opt<Identity> user;
if (this.condition) {
user = new Opt.Single<>(
new Identity.Simple(
String.format("urn:basic:%s", usr)
)
);
} else {
user = new Opt.Empty<>();
}
return user;
}
}
/**
* Empty check.
*
* @since 0.20
*/
public static final class Empty implements PsBasic.Entry {
@Override
public Opt<Identity> enter(final String user, final String pwd) {
return new Opt.Empty<>();
}
}
/**
* Default entry.
*
* @since 0.22
*/
public static final class Default implements PsBasic.Entry {
/**
* How keys in
* {@link org.takes.facets.auth.PsBasic.Default#usernames} are
* formatted.
*/
private static final String KEY_FORMAT = "%s %s";
/**
* Encoding for URLEncode#encode.
*/
private static final String ENCODING = "UTF-8";
/**
* Map from login/password pairs to URNs.
*/
private final Map<String, String> usernames;
/**
* Public ctor.
* @param users Strings with user's login, password and URN with
* space characters as separators. Each of login, password and urn
* are URL-encoded substrings. For example,
* {@code "mike my%20password urn:jcabi-users:michael"}.
*/
public Default(final String... users) {
this.usernames = Default.converted(users);
}
@Override
public Opt<Identity> enter(final String user, final String pwd) {
final Opt<String> urn = this.urn(user, pwd);
final Opt<Identity> identity;
if (urn.has()) {
try {
identity = new Opt.Single<>(
new Identity.Simple(
URLDecoder.decode(
urn.get(), PsBasic.Default.ENCODING
)
)
);
} catch (final UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
} else {
identity = new Opt.Empty<>();
}
return identity;
}
/**
* Converts Strings with user's login, password and URN to Map.
* @param users Strings with user's login, password and URN with
* space characters as separators. Each of login, password and urn
* are URL-encoded substrings. For example,
* {@code "mike my%20password urn:jcabi-users:michael"}.
* @return Map from login/password pairs to URNs.
*/
private static Map<String, String> converted(final String... users) {
final Map<String, String> result = new HashMap<>(users.length);
for (final String user : users) {
final String unified = user.replace("%20", "+");
PsBasic.Default.validateUser(unified);
result.put(
PsBasic.Default.key(unified),
unified.substring(unified.lastIndexOf(' ') + 1)
);
}
return result;
}
/**
* Returns an URN corresponding to a login-password pair.
* @param user Login.
* @param pwd Password.
* @return Opt with URN or empty if there is no such login-password
* pair.
*/
private Opt<String> urn(final String user, final String pwd) {
final String urn;
try {
urn = this.usernames.get(
String.format(
PsBasic.Default.KEY_FORMAT,
URLEncoder.encode(
user,
PsBasic.Default.ENCODING
),
URLEncoder.encode(
pwd,
PsBasic.Default.ENCODING
)
)
);
} catch (final UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
final Opt<String> opt;
if (urn == null) {
opt = new Opt.Empty<>();
} else {
opt = new Opt.Single<>(urn);
}
return opt;
}
/**
* Creates a key for
* {@link org.takes.facets.auth.PsBasic.Default#usernames} map.
* @param unified User string made of 3 urlencoded substrings
* separated with non-urlencoded space characters.
* @return Login and password parts with <pre>%20</pre> replaced with
* <pre>+</pre>.
*/
private static String key(final String unified) {
return String.format(
PsBasic.Default.KEY_FORMAT,
unified.substring(0, unified.indexOf(' ')),
unified.substring(
unified.indexOf(' ') + 1,
unified.lastIndexOf(' ')
)
);
}
/**
* Checks if a unified user string is correctly formatted.
* @param unified String with urlencoded user login, password and urn
* separated with spaces.
*/
private static void validateUser(final String unified) {
final boolean amount = PsBasic.Default.countSpaces(unified) != 2;
final boolean nearby =
unified.indexOf(' ') + 1 == unified.lastIndexOf(' ');
if (amount || nearby) {
throw new IllegalArgumentException(
String.format(
"One of users was incorrectly formatted: %s",
unified
)
);
}
}
/**
* Counts spaces in a string.
* @param txt Any string.
* @return Amount of spaces in string.
*/
private static int countSpaces(final String txt) {
int spaces = 0;
for (final char character : txt.toCharArray()) {
if (character == ' ') {
spaces += 1;
}
}
return spaces;
}
}
}