Ejemplo n.º 1
0
void setAttachmentResponse(const http::Request& request,
                           const std::string& filename,
                           const FilePath& attachmentPath,
                           http::Response* pResponse)
{
   if (request.headerValue("User-Agent").find("MSIE") == std::string::npos)
   {
      pResponse->setNoCacheHeaders();
   }
   else
   {
      // Can't set full no-cache headers because this breaks downloads in IE
      pResponse->setHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT");
      pResponse->setHeader("Cache-Control", "private");
   }
   // Can't rely on "filename*" in Content-Disposition header because not all
   // browsers support non-ASCII characters here (e.g. Safari 5.0.5). If
   // possible, make the requesting URL contain the UTF-8 byte escaped filename
   // as the last path element.
   pResponse->setHeader("Content-Disposition",
                        "attachment; filename*=UTF-8''"
                        + http::util::urlEncode(filename, false));
   pResponse->setHeader("Content-Type", "application/octet-stream");
   pResponse->setBody(attachmentPath);
}
Ejemplo n.º 2
0
void EtagStoreResource::handle_etag(const Http::Request& request,
                                    Http::Response& response) {
    // TODO http://redmine.webtoolkit.eu/issues/2471
    // const std::string* cookie_value = request.getCookieValue(cookie_name_);
    std::string cookies = request.headerValue("Cookie");
    if (cookies.empty()) {
        return;
    }
    std::string pattern = cookie_name_ + "=";
    int cookie_begin = cookies.find(pattern);
    if (cookie_begin == std::string::npos) {
        return;
    }
    cookie_begin += pattern.length();
    int cookie_end = cookies.find(';', cookie_begin);
    int cookie_length = (cookie_end == -1) ? -1 : (cookie_end - cookie_begin);
    std::string cookie_value = cookies.substr(cookie_begin, cookie_length);
    //
    std::string etag_value = request.headerValue(receive_header());
    boost::mutex::scoped_lock lock(cookie_to_etag_mutex_);
    Map::iterator it = cookie_to_etag_.find(cookie_value);
    if (it == cookie_to_etag_.end()) {
        return;
    }
    Etag& etag = it->second;
    if (etag_value.empty()) {
        etag_value = etag.def;
    }
    etag.from_client = etag_value;
    if (!etag.to_client.empty()) {
        response.addHeader(send_header(), etag.to_client);
        etag.to_client.clear();
    } else {
        etag.handler(etag.from_client);
    }
    if (!etag.from_client.empty()) {
        response.addHeader(send_header(), etag.from_client);
    }
}
Ejemplo n.º 3
0
void setAttachmentResponse(const http::Request& request,
                           const std::string& filename,
                           const FilePath& attachmentPath,
                           http::Response* pResponse)
{
   if (request.headerValue("User-Agent").find("MSIE") == std::string::npos)
   {
      pResponse->setNoCacheHeaders();
   }
   else
   {
      // Can't set full no-cache headers because this breaks downloads in IE
      pResponse->setHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT");
      pResponse->setHeader("Cache-Control", "private");
   }
   pResponse->setHeader("Content-Disposition",
                        "attachment; filename=" + 
                        http::util::urlEncode(filename, false));
   pResponse->setBody(attachmentPath);
}
Ejemplo n.º 4
0
void OAuthTokenEndpoint::handleRequest(const Http::Request &request, Http::Response &response)
{
#ifdef WT_TARGET_JAVA
  try {
#endif // WT_TARGET_JAVA
  response.setMimeType("application/json");
  response.addHeader("Cache-Control", "no-store");
  response.addHeader("Pragma", "no-cache");

  const std::string *grantType = request.getParameter("grant_type");
  const std::string *redirectUri = request.getParameter("redirect_uri");
  const std::string *code = request.getParameter("code");
  std::string clientId;
  std::string clientSecret;
  ClientSecretMethod authMethod = HttpAuthorizationBasic;

  // Preferred method: get authorization information from
  // Http Basic authentication
  std::string headerSecret;
  std::string authHeader = request.headerValue("Authorization");
  if (authHeader.length() > AUTH_TYPE.length() + 1) {
#ifndef WT_TARGET_JAVA
    headerSecret = Utils::base64Decode(authHeader.substr(AUTH_TYPE.length() + 1));
#else
    headerSecret = Utils::base64DecodeS(authHeader.substr(AUTH_TYPE.length() + 1));
#endif // WT_TARGET_JAVA
    std::vector<std::string> tokens;
    boost::split(tokens, headerSecret, boost::is_any_of(":"));
    if (tokens.size() == 2) {
      clientId = Utils::urlDecode(tokens[0]);
      clientSecret = Utils::urlDecode(tokens[1]);
      authMethod = HttpAuthorizationBasic;
    }
  }

  // Alternative method: pass authorization information as parameters
  // (only allowed for post methods)
  if (clientId.empty() && clientSecret.empty()) {
    const std::string *clientIdParam = request.getParameter("client_id");
    const std::string *clientSecretParam = request.getParameter("client_secret");
    if (clientIdParam && clientSecretParam) {
      clientId = *clientIdParam;
      clientSecret = *clientSecretParam;
      authMethod = RequestBodyParameter;
    }
  }

  if (!code || clientId.empty() || clientSecret.empty() || !grantType || !redirectUri) {
    response.setStatus(400);
    response.out() << "{\"error\": \"invalid_request\"}" << std::endl;
    return;
  }
  OAuthClient client = db_->idpClientFindWithId(clientId);
  if (!client.checkValid() || !client.verifySecret(clientSecret)
      || client.authMethod() != authMethod) {
    response.setStatus(401);
    if (!authHeader.empty()) {
      if (client.authMethod() == HttpAuthorizationBasic)
        response.addHeader("WWW-Authenticate", AUTH_TYPE);
      else
        response.addHeader("WWW-Authenticate",
            methodToString(client.authMethod()));
    }
    response.out() << "{\n\"error\": \"invalid_client\"\n}" << std::endl;
    return;
  }
  if (*grantType != GRANT_TYPE) {
    response.setStatus(400);
    response.out() << "{\n\"error\": \"unsupported_grant_type\"\n}" << std::endl;
    return;
  }
  IssuedToken authCode = db_->idpTokenFindWithValue(GRANT_TYPE, *code);
  if (!authCode.checkValid() || authCode.redirectUri() != *redirectUri
      || WDateTime::currentDateTime() > authCode.expirationTime()) {
    response.setStatus(400);
    response.out() << "{\n\"error\": \"invalid_grant\"\n}" << std::endl;
    return;
  }
  std::string accessTokenValue = WRandom::generateId();
  WDateTime expirationTime = WDateTime::currentDateTime().addSecs(accessExpSecs_);
  const User &user = authCode.user();
  const OAuthClient &authClient = authCode.authClient();
  const std::string scope = authCode.scope();
  db_->idpTokenAdd(accessTokenValue, expirationTime, "access_token", scope,
                   authCode.redirectUri(), user, authClient);
  db_->idpTokenRemove(authCode);
  response.setStatus(200);
  Json::Object root;
  root["access_token"] = Json::Value(accessTokenValue);
  root["token_type"] = Json::Value("Bearer");
  root["expires_in"] = Json::Value(accessExpSecs_);
  if (authCode.scope().find("openid") != std::string::npos) {
    std::string header;
    std::string signature;
    std::string payload = Utils::base64Encode(idTokenPayload(authClient.clientId(), scope, user), false);
#ifndef WT_TARGET_JAVA
#ifdef WT_WITH_SSL
    if (privateKey) {
      header    = Utils::base64Encode("{\n\"typ\": \"JWT\",\n\"alg\": \"RS256\"\n}", false);
      signature = Utils::base64Encode(rs256(header + "." + payload), false);
    } else {
#endif // WT_WITH_SSL
#endif // WT_TARGET_JAVA
      header    = Utils::base64Encode("{\n\"typ\": \"JWT\",\n\"alg\": \"none\"\n}", false);
      signature = Utils::base64Encode("", false);
#ifndef WT_TARGET_JAVA
#ifdef WT_WITH_SSL
    }
#endif // WT_WITH_SSL
#endif // WT_TARGET_JAVA
    root["id_token"] = Json::Value(header + "." + payload + "." + signature);
  }
  response.out() << Json::serialize(root);
#ifdef WT_TARGET_JAVA
  } catch (std::io_exception ioe) {
    LOG_ERROR(ioe.message());
  }
#endif
}