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::Request>& 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(requestJsonString,
97 [res]([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& request,
98 const std::shared_ptr<web::http::client::Response>& response) {
99 VLOG(1) << "OnResponse";
100 VLOG(1) << "Response: " << std::string(response->body.begin(), response->body.end());
101 if (std::stoi(response->statusCode) != 200) {
102 const nlohmann::json errorJson = {{"error", "Invalid access token"}};
103 res->status(401).send(errorJson.dump(4));
104 } else {
105 const nlohmann::json successJson = {{"content", "🦆"}};
106 res->status(200).send(successJson.dump(4));
107 }
108 });
109 },
110 []([[maybe_unused]] const std::shared_ptr<web::http::client::Request>& req) {
111 LOG(INFO) << " -- OnRequestEnd";
112 });
113
114 legacyClient.connect(
116 switch (state) {
118 VLOG(1) << "OAuth2ResourceServer: connected to '" << socketAddress.toString() << "'";
119 break;
121 VLOG(1) << "OAuth2ResourceServer: disabled";
122 break;
124 VLOG(1) << "OAuth2ResourceServer: error occurred";
125 break;
127 VLOG(1) << "OAuth2ResourceServer: fatal error occurred";
128 break;
129 }
130 });
131 });
132
134 switch (state) {
136 VLOG(1) << "app: listening on '" << socketAddress.toString() << "'";
137 break;
139 VLOG(1) << "app: disabled";
140 break;
142 VLOG(1) << "app: error occurred";
143 break;
145 VLOG(1) << "app: fatal error occurred";
146 break;
147 }
148 });
150}
#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