use of java.util.regex.Matcher in project jetty.project by eclipse.
the class ConstraintTest method testDigest.
@Test
public void testDigest() throws Exception {
DigestAuthenticator authenticator = new DigestAuthenticator();
authenticator.setMaxNonceCount(5);
_security.setAuthenticator(authenticator);
_server.start();
String response;
response = _connector.getResponse("GET /ctx/noauth/info HTTP/1.0\r\n\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
response = _connector.getResponse("GET /ctx/forbid/info HTTP/1.0\r\n\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 403 Forbidden"));
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 401 Unauthorized"));
Assert.assertThat(response, Matchers.containsString("WWW-Authenticate: Digest realm=\"TestRealm\""));
Pattern nonceP = Pattern.compile("nonce=\"([^\"]*)\",");
Matcher matcher = nonceP.matcher(response);
Assert.assertTrue(matcher.find());
String nonce = matcher.group(1);
//wrong password
String digest = digest(nonce, "user", "WRONG", "/ctx/auth/info", "1");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=1, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 401 Unauthorized"));
// right password
digest = digest(nonce, "user", "password", "/ctx/auth/info", "2");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=2, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
// once only
digest = digest(nonce, "user", "password", "/ctx/auth/info", "2");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=2, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 401 Unauthorized"));
// increasing
digest = digest(nonce, "user", "password", "/ctx/auth/info", "4");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=4, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
// out of order
digest = digest(nonce, "user", "password", "/ctx/auth/info", "3");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=3, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
// stale
digest = digest(nonce, "user", "password", "/ctx/auth/info", "5");
response = _connector.getResponse("GET /ctx/auth/info HTTP/1.0\r\n" + "Authorization: Digest username=\"user\", qop=auth, cnonce=\"1234567890\", uri=\"/ctx/auth/info\", realm=\"TestRealm\", " + "nc=5, " + "nonce=\"" + nonce + "\", " + "response=\"" + digest + "\"\r\n" + "\r\n");
Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 401 Unauthorized"));
Assert.assertThat(response, Matchers.containsString("stale=true"));
}
use of java.util.regex.Matcher in project jetty.project by eclipse.
the class RegexRule method matchAndApply.
/* ------------------------------------------------------------ */
@Override
public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException {
Matcher matcher = _regex.matcher(target);
boolean matches = matcher.matches();
if (matches)
return apply(target, request, response, matcher);
return null;
}
use of java.util.regex.Matcher in project jetty.project by eclipse.
the class CommandLineConfigSource method findJettyHomePath.
private final Path findJettyHomePath() {
// If a jetty property is defined, use it
Prop prop = this.props.getProp(BaseHome.JETTY_HOME, false);
if (prop != null && !Utils.isBlank(prop.value)) {
return FS.toPath(prop.value);
}
// If a system property is defined, use it
String val = System.getProperty(BaseHome.JETTY_HOME);
if (!Utils.isBlank(val)) {
return FS.toPath(val);
}
// Attempt to find path relative to content in jetty's start.jar
// based on lookup for the Main class (from jetty's start.jar)
String classRef = "org/eclipse/jetty/start/Main.class";
URL jarfile = this.getClass().getClassLoader().getResource(classRef);
if (jarfile != null) {
Matcher m = Pattern.compile("jar:(file:.*)!/" + classRef).matcher(jarfile.toString());
if (m.matches()) {
// ${jetty.home} is relative to found BaseHome class
try {
return new File(new URI(m.group(1))).getParentFile().toPath();
} catch (URISyntaxException e) {
throw new UsageException(UsageException.ERR_UNKNOWN, e);
}
}
}
// Lastly, fall back to ${user.dir} default
Path home = FS.toPath(System.getProperty("user.dir", "."));
setProperty(BaseHome.JETTY_HOME, home.toString(), ORIGIN_INTERNAL_FALLBACK);
return home;
}
use of java.util.regex.Matcher in project jetty.project by eclipse.
the class Module method writeIniSection.
public void writeIniSection(BufferedWriter writer, Props props) {
PrintWriter out = new PrintWriter(writer);
out.println("# --------------------------------------- ");
out.println("# Module: " + getName());
for (String line : getDescription()) out.append("# ").println(line);
out.println("# --------------------------------------- ");
out.println("--module=" + getName());
out.println();
for (String line : getIniTemplate()) {
Matcher m = SET_PROPERTY.matcher(line);
if (m.matches() && m.groupCount() == 3) {
String name = m.group(2);
Prop p = props.getProp(name);
if (p != null && p.origin.startsWith(CommandLineConfigSource.ORIGIN_CMD_LINE)) {
StartLog.info("%-15s property set %s=%s", this._name, name, p.value);
out.printf("%s=%s%n", name, p.value);
} else
out.println(line);
} else
out.println(line);
}
out.println();
out.flush();
}
use of java.util.regex.Matcher in project jetty.project by eclipse.
the class Props method expand.
private String expand(String str, Stack<String> seenStack) {
if (str == null) {
return str;
}
if (str.indexOf("${") < 0) {
// Contains no potential expressions.
return str;
}
Matcher mat = __propertyPattern.matcher(str);
StringBuilder expanded = new StringBuilder();
int offset = 0;
String property;
String value;
while (mat.find(offset)) {
property = mat.group(1);
// Loop detection
if (seenStack.contains(property)) {
StringBuilder err = new StringBuilder();
err.append("Property expansion loop detected: ");
int idx = seenStack.lastIndexOf(property);
for (int i = idx; i < seenStack.size(); i++) {
err.append(seenStack.get(i));
err.append(" -> ");
}
err.append(property);
throw new PropsException(err.toString());
}
// find property name
expanded.append(str.subSequence(offset, mat.start()));
// get property value
value = getString(property);
if (value == null) {
StartLog.trace("Unable to expand: %s", property);
expanded.append(mat.group());
} else {
// recursively expand
seenStack.push(property);
value = expand(value, seenStack);
seenStack.pop();
expanded.append(value);
}
// update offset
offset = mat.end();
}
// leftover
expanded.append(str.substring(offset));
// special case for "$$"
if (expanded.indexOf("$$") >= 0) {
return expanded.toString().replaceAll("\\$\\$", "\\$");
}
return expanded.toString();
}
Aggregations