BasicCredentialsProvider.java
2.89 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Credentials
* javax.jcr.GuestCredentials
* javax.jcr.LoginException
* javax.jcr.SimpleCredentials
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.jackrabbit.util.Base64
*/
package com.day.crx.explorer.impl.j2ee;
import com.day.crx.explorer.impl.j2ee.CredentialsProvider;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.jcr.Credentials;
import javax.jcr.GuestCredentials;
import javax.jcr.LoginException;
import javax.jcr.SimpleCredentials;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.jackrabbit.util.Base64;
public class BasicCredentialsProvider
implements CredentialsProvider {
public static final String EMPTY_DEFAULT_HEADER_VALUE = "";
public static final String GUEST_DEFAULT_HEADER_VALUE = "guestcredentials";
private final String defaultHeaderValue;
public BasicCredentialsProvider(String defaultHeaderValue) {
this.defaultHeaderValue = defaultHeaderValue;
}
public Credentials getCredentials(HttpServletRequest request) throws LoginException, ServletException {
try {
String authHeader = request.getHeader("Authorization");
if (authHeader != null) {
String[] authStr = authHeader.split(" ");
if (authStr.length >= 2 && authStr[0].equalsIgnoreCase("BASIC")) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Base64.decode((char[])authStr[1].toCharArray(), (OutputStream)out);
String decAuthStr = out.toString("ISO-8859-1");
int pos = decAuthStr.indexOf(58);
String userid = decAuthStr.substring(0, pos);
String passwd = decAuthStr.substring(pos + 1);
return new SimpleCredentials(userid, passwd.toCharArray());
}
throw new ServletException("Unable to decode authorization.");
}
if (this.defaultHeaderValue == null) {
throw new LoginException();
}
if ("".equals(this.defaultHeaderValue)) {
return null;
}
if ("guestcredentials".equals(this.defaultHeaderValue)) {
return new GuestCredentials();
}
int pos = this.defaultHeaderValue.indexOf(58);
if (pos < 0) {
return new SimpleCredentials(this.defaultHeaderValue, new char[0]);
}
return new SimpleCredentials(this.defaultHeaderValue.substring(0, pos), this.defaultHeaderValue.substring(pos + 1).toCharArray());
}
catch (IOException e) {
throw new ServletException("Unable to decode authorization: " + e.toString());
}
}
}