Esempio n. 1
0
  /**
   * @param exchange
   * @param context
   * @throws Exception
   */
  @Override
  public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
    if (!ENABLED) {
      return;
    }

    if (exchange.getSecurityContext() == null
        || exchange.getSecurityContext().getAuthenticatedAccount() == null
        || exchange.getSecurityContext().getAuthenticatedAccount().getPrincipal() == null
        || !("/_authtokens/"
                + exchange.getSecurityContext().getAuthenticatedAccount().getPrincipal().getName())
            .equals(exchange.getRequestURI())) {
      ResponseHelper.endExchange(exchange, HttpStatus.SC_FORBIDDEN);
      return;
    }

    if (Methods.GET.equals(exchange.getRequestMethod())) {
      Representation rep =
          new Representation(
              "/_authtokens/"
                  + exchange
                      .getSecurityContext()
                      .getAuthenticatedAccount()
                      .getPrincipal()
                      .getName());

      rep.addProperty(
          "auth_token", exchange.getResponseHeaders().get(AUTH_TOKEN_HEADER).getFirst());
      rep.addProperty(
          "auth_token_valid_until",
          exchange.getResponseHeaders().get(AUTH_TOKEN_VALID_HEADER).getFirst());

      exchange.setResponseCode(HttpStatus.SC_OK);
      exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, HAL_JSON_MEDIA_TYPE);
      exchange.getResponseSender().send(rep.toString());
      exchange.endExchange();
    } else if (Methods.DELETE.equals(exchange.getRequestMethod())) {
      AuthTokenIdentityManager.getInstance()
          .getCachedAccounts()
          .invalidate(
              exchange.getSecurityContext().getAuthenticatedAccount().getPrincipal().getName());
      removeAuthTokens(exchange);
      ResponseHelper.endExchange(exchange, HttpStatus.SC_NO_CONTENT);
    } else {
      ResponseHelper.endExchange(exchange, HttpStatus.SC_METHOD_NOT_ALLOWED);
    }
  }
 protected static METHOD selectRequestMethod(HttpString _method) {
   METHOD method;
   if (Methods.GET.equals(_method)) {
     method = METHOD.GET;
   } else if (Methods.POST.equals(_method)) {
     method = METHOD.POST;
   } else if (Methods.PUT.equals(_method)) {
     method = METHOD.PUT;
   } else if (Methods.DELETE.equals(_method)) {
     method = METHOD.DELETE;
   } else if (PATCH.equals(_method.toString())) {
     method = METHOD.PATCH;
   } else if (Methods.OPTIONS.equals(_method)) {
     method = METHOD.OPTIONS;
   } else {
     method = METHOD.OTHER;
   }
   return method;
 }