-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAuthenticationFilter.cpp
91 lines (79 loc) · 2.64 KB
/
AuthenticationFilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Created by victor on 11/15/17.
//
#include <proxygen/httpserver/Filters.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include "AuthenticationFilter.h"
#include "UserManager.h"
namespace restdbxx {
void AuthenticationFilter::onRequest(std::unique_ptr<proxygen::HTTPMessage> headers) noexcept {
using std::string;
bool auth_headers_present = headers->getHeaders().exists(RESTDBXX_TOKEN_HEADER_NAME());
auth_headers_present = auth_headers_present && headers->getHeaders().exists(RESTDBXX_USERNAME_HEADER_NAME());
if (auth_headers_present) {
string username = headers->getHeaders().getSingleOrEmpty(RESTDBXX_USERNAME_HEADER_NAME());
string token = headers->getHeaders().getSingleOrEmpty(RESTDBXX_TOKEN_HEADER_NAME());
auto user_manager = UserManager::get_instance();
bool token_valid = user_manager->validate_access_token(username, token);
if (!token_valid) {
VLOG(google::GLOG_INFO) << "invalid token, rejecting request";
upstream_->onError(proxygen::ProxygenError::kErrorMalformedInput);
upstream_ = nullptr;
proxygen::ResponseBuilder(downstream_)
.status(401, "Authentication Failed")
.body("invalid token")
.sendWithEOM();
return;
}
VLOG(google::GLOG_INFO) << "token is valid, proceed";
}
if (upstream_)
upstream_->onRequest(std::move(headers));
}
AuthenticationFilter::AuthenticationFilter(proxygen::RequestHandler *upstream) : Filter(upstream) {}
const std::string &AuthenticationFilter::RESTDBXX_TOKEN_HEADER_NAME() {
static const std::string value = "RESTDBXX_AUTH_TOKEN";
return value;
}
const std::string &AuthenticationFilter::RESTDBXX_USERNAME_HEADER_NAME() {
static const std::string value = "RESTDBXX_AUTH_USERNAME";
return value;
}
void AuthenticationFilter::requestComplete() noexcept {
downstream_ = nullptr;
if (upstream_)
upstream_->requestComplete();
delete this;
}
void AuthenticationFilter::onError(proxygen::ProxygenError err) noexcept {
downstream_ = nullptr;
if (upstream_)
upstream_->onError(err);
delete this;
}
void AuthenticationFilter::onEgressPaused() noexcept {
if (upstream_) {
upstream_->onEgressPaused();
}
}
void AuthenticationFilter::onEgressResumed() noexcept {
if (upstream_) {
upstream_->onEgressResumed();
}
}
void AuthenticationFilter::onBody(std::unique_ptr<folly::IOBuf> body) noexcept {
if (upstream_) {
upstream_->onBody(std::move(body));
}
}
void AuthenticationFilter::onUpgrade(proxygen::UpgradeProtocol protocol)noexcept {
if (upstream_) {
upstream_->onUpgrade(protocol);
}
}
void AuthenticationFilter::onEOM() noexcept {
if (upstream_) {
upstream_->onEOM();
}
}
}