Search in sources :

Example 36 with Matcher

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"));
}
Also used : Pattern(java.util.regex.Pattern) DigestAuthenticator(org.eclipse.jetty.security.authentication.DigestAuthenticator) Matcher(java.util.regex.Matcher) Test(org.junit.Test)

Example 37 with Matcher

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;
}
Also used : Matcher(java.util.regex.Matcher)

Example 38 with Matcher

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;
}
Also used : Path(java.nio.file.Path) UsageException(org.eclipse.jetty.start.UsageException) Prop(org.eclipse.jetty.start.Props.Prop) Matcher(java.util.regex.Matcher) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 39 with Matcher

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();
}
Also used : Matcher(java.util.regex.Matcher) Prop(org.eclipse.jetty.start.Props.Prop) PrintWriter(java.io.PrintWriter)

Example 40 with Matcher

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();
}
Also used : Matcher(java.util.regex.Matcher)

Aggregations

Matcher (java.util.regex.Matcher)12640 Pattern (java.util.regex.Pattern)5059 ArrayList (java.util.ArrayList)1525 IOException (java.io.IOException)913 HashMap (java.util.HashMap)575 File (java.io.File)490 Test (org.junit.Test)448 BufferedReader (java.io.BufferedReader)433 Map (java.util.Map)369 List (java.util.List)292 InputStreamReader (java.io.InputStreamReader)268 HashSet (java.util.HashSet)237 MalformedURLException (java.net.MalformedURLException)164 URL (java.net.URL)157 Date (java.util.Date)153 InputStream (java.io.InputStream)148 Field (java.lang.reflect.Field)130 ParseException (java.text.ParseException)130 PatternSyntaxException (java.util.regex.PatternSyntaxException)128 LinkedHashMap (java.util.LinkedHashMap)122