53 {
55
57
58 const std::string authorizationServerUri{"http://localhost:8082"};
59
61
62 app.get(
"/access", [authorizationServerUri]
APPLICATION(req, res) {
63 res->set("Access-Control-Allow-Origin", "*");
64 const std::string queryAccessToken{req->query("access_token")};
65 const std::string queryClientId{req->query("client_id")};
66 if (queryAccessToken.empty() || queryClientId.empty()) {
67 VLOG(1) << "Missing access_token or client_id in body";
68 res->sendStatus(401);
69 return;
70 }
71
74 VLOG(1) << "OnConnect";
75
76 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
77 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
78 },
80 VLOG(1) << "OnConnected";
81 },
83 VLOG(1) << "OnDisconnect";
84
85 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
86 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
87 },
88 [queryAccessToken, queryClientId, res](const std::shared_ptr<web::http::client::MasterRequest>& request) {
89 VLOG(1) << "OnRequestBegin";
90 request->url = "/oauth2/token/validate?client_id=" + queryClientId;
91 request->method = "POST";
92 VLOG(1) << "ClientId: " << queryClientId;
93 VLOG(1) << "AccessToken: " << queryAccessToken;
94 const nlohmann::json requestJson = {{"access_token", queryAccessToken}, {"client_id", queryClientId}};
95 const std::string requestJsonString{requestJson.dump(4)};
96 request->send(
97 requestJsonString,
98 [res]([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& request,
99 const std::shared_ptr<web::http::client::Response>& response) {
100 VLOG(1) << "OnResponse";
101 VLOG(1) << "Response: " << std::string(response->body.begin(), response->body.end());
102 if (std::stoi(response->statusCode) != 200) {
103 const nlohmann::json errorJson = {{"error", "Invalid access token"}};
104 res->status(401).send(errorJson.dump(4));
105 } else {
106 const nlohmann::json successJson = {{"content", "🦆"}};
107 res->status(200).send(successJson.dump(4));
108 }
109 },
110 [](const std::shared_ptr<web::http::client::Request>&, const std::string& message) {
111 VLOG(1) << "OAuth2ResourceServer: Request parse error: " << message;
112 });
113 },
114 []([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& req) {
115 LOG(INFO) << " -- OnRequestEnd";
116 });
117
118 legacyClient.connect(
120 switch (state) {
122 VLOG(1) << "OAuth2ResourceServer: connected to '" << socketAddress.toString() << "'";
123 break;
125 VLOG(1) << "OAuth2ResourceServer: disabled";
126 break;
128 VLOG(1) << "OAuth2ResourceServer: error occurred";
129 break;
131 VLOG(1) << "OAuth2ResourceServer: fatal error occurred";
132 break;
133 }
134 });
135 });
136
138 switch (state) {
140 VLOG(1) << "app: listening on '" << socketAddress.toString() << "'";
141 break;
143 VLOG(1) << "app: disabled";
144 break;
146 VLOG(1) << "app: error occurred";
147 break;
149 VLOG(1) << "app: fatal error occurred";
150 break;
151 }
152 });
154}
#define APPLICATION(req, res)
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
typename Server::SocketAddress SocketAddress
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
typename Super::SocketAddress SocketAddress
typename Super::SocketConnection SocketConnection