12 {
14
16
17 const std::string authorizationServerUri{"http://localhost:8082"};
18
20
21 app.get(
"/access", [authorizationServerUri]
APPLICATION(req, res) {
22 res->set("Access-Control-Allow-Origin", "*");
23 const std::string queryAccessToken{req->query("access_token")};
24 const std::string queryClientId{req->query("client_id")};
25 if (queryAccessToken.empty() || queryClientId.empty()) {
26 VLOG(1) << "Missing access_token or client_id in body";
27 res->sendStatus(401);
28 return;
29 }
30
33 VLOG(1) << "OnConnect";
34
35 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
36 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
37 },
39 VLOG(1) << "OnConnected";
40 },
42 VLOG(1) << "OnDisconnect";
43
44 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
45 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
46 },
47 [queryAccessToken, queryClientId, res](const std::shared_ptr<web::http::client::Request>& request) {
48 VLOG(1) << "OnRequestBegin";
49 request->url = "/oauth2/token/validate?client_id=" + queryClientId;
50 request->method = "POST";
51 VLOG(1) << "ClientId: " << queryClientId;
52 VLOG(1) << "AccessToken: " << queryAccessToken;
53 const nlohmann::json requestJson = {{"access_token", queryAccessToken}, {"client_id", queryClientId}};
54 const std::string requestJsonString{requestJson.dump(4)};
55 request->send(requestJsonString,
56 [res]([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& request,
57 const std::shared_ptr<web::http::client::Response>& response) {
58 VLOG(1) << "OnResponse";
59 VLOG(1) << "Response: " << std::string(response->body.begin(), response->body.end());
60 if (std::stoi(response->statusCode) != 200) {
61 const nlohmann::json errorJson = {{"error", "Invalid access token"}};
62 res->status(401).send(errorJson.dump(4));
63 } else {
64 const nlohmann::json successJson = {{"content", "🦆"}};
65 res->status(200).send(successJson.dump(4));
66 }
67 });
68 },
69 []([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& req) {
70 LOG(INFO) << " -- OnRequestEnd";
71 });
72
73 legacyClient.connect(
75 switch (state) {
77 VLOG(1) << "OAuth2ResourceServer: connected to '" << socketAddress.toString() << "'";
78 break;
80 VLOG(1) << "OAuth2ResourceServer: disabled";
81 break;
83 VLOG(1) << "OAuth2ResourceServer: error occurred";
84 break;
86 VLOG(1) << "OAuth2ResourceServer: fatal error occurred";
87 break;
88 }
89 });
90 });
91
93 switch (state) {
95 VLOG(1) << "app: listening on '" << socketAddress.toString() << "'";
96 break;
98 VLOG(1) << "app: disabled";
99 break;
101 VLOG(1) << "app: error occurred";
102 break;
104 VLOG(1) << "app: fatal error occurred";
105 break;
106 }
107 });
109}
#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::SocketConnection SocketConnection
typename Super::SocketAddress SocketAddress