Java Code Examples for io.vertx.core.http.Cookie#getValue()

The following examples show how to use io.vertx.core.http.Cookie#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormAuthenticationMechanism.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected void handleRedirectBack(final RoutingContext exchange) {
    Cookie redirect = exchange.getCookie(locationCookie);
    String location;
    if (redirect != null) {
        location = redirect.getValue();
        exchange.response().addCookie(redirect.setMaxAge(0));
    } else {
        location = exchange.request().scheme() + "://" + exchange.request().host() + landingPage;
    }
    exchange.response().setStatusCode(302);
    exchange.response().headers().add(HttpHeaderNames.LOCATION, location);
    exchange.response().end();
}
 
Example 2
Source File: CookieAttribute.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public String readAttribute(final RoutingContext exchange) {
    Cookie cookie = exchange.getCookie(cookieName);
    if (cookie == null) {
        return null;
    }
    return cookie.getValue();
}
 
Example 3
Source File: SessionHandlerImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
private String getSessionId(RoutingContext  context) {
  if (cookieless) {
    // cookieless sessions store the session on the path or the request
    // a session is identified by a sequence of characters between braces
    String path = context.normalizedPath();
    int s = -1;
    int e = -1;
    for (int i = 0; i < path.length(); i++) {
      if (path.charAt(i) == '(') {
        s = i + 1;
        continue;
      }
      if (path.charAt(i) == ')') {
        // if not open parenthesis yet
        // this is a false end, continue looking
        if (s != -1) {
          e = i;
          break;
        }
      }
    }
    if (s != -1 && e != -1 && s < e) {
      return path.substring(s, e);
    }
  } else {
    Cookie cookie = context.getCookie(sessionCookieName);
    if (cookie != null) {
      // Look up sessionId
      return cookie.getValue();
    }
  }

  return null;
}
 
Example 4
Source File: CookieConverter.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
public static HttpCookie toHttpCookie(Cookie cookie) {
    return new HttpCookie(cookie.getName(), cookie.getValue());
}
 
Example 5
Source File: PersistentLoginManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public RestoreResult restore(RoutingContext context) {
    Cookie existing = context.getCookie(cookieName);
    // If there is no credential cookie, we have nothing to restore.
    if (existing == null) {
        // Enforce new login.
        return null;
    }
    String val = existing.getValue();
    try {
        Cipher cipher = Cipher.getInstance(ENC_ALGORITHM);
        ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.getDecoder().decode(val.getBytes(StandardCharsets.UTF_8)));
        int ivLength = byteBuffer.get();
        byte[] iv = new byte[ivLength];
        byteBuffer.get(iv);
        byte[] encrypted = new byte[byteBuffer.remaining()];
        byteBuffer.get(encrypted);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(ENC_TAG_LENGTH, iv));
        String result = new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8);
        int sep = result.indexOf(":");
        // If parsing fails, something is wrong and we need to enforce a new login.
        if (sep == -1) {
            // Enforce new login.
            log.debugf("%s cookie parsing failed. Is encryption-key set for all instances?", cookieName);
            return null;
        }
        long expireIdle = Long.parseLong(result.substring(0, sep));
        long now = System.currentTimeMillis();
        log.debugf("Current time: %s, Expire idle timeout: %s, expireIdle - now is: %d - %d = %d",
                new Date(now).toString(), new Date(expireIdle).toString(), expireIdle, now, expireIdle - now);
        // We don't attempt renewal, idle timeout already expired.
        if (now > expireIdle) {
            // Enforce new login.
            return null;
        }
        boolean newCookieNeeded = (timeoutMillis - (expireIdle - now)) > newCookieIntervalMillis;
        log.debugf("Is new cookie needed? ( %d - ( %d - %d)) > %d : %b", timeoutMillis, expireIdle, now,
                newCookieIntervalMillis, newCookieNeeded);
        return new RestoreResult(result.substring(sep + 1), newCookieNeeded);
    } catch (Exception e) {
        log.debug("Failed to restore persistent user session", e);
        return null;
    }
}