2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "express/legacy/in/WebApp.h"
43#include "express/middleware/JsonMiddleware.h"
44#include "log/Logger.h"
45#include "web/http/legacy/in/Client.h"
47#include <nlohmann/json.hpp>
53int main(
int argc,
char* argv[]) {
56 const express::legacy::in::WebApp app
("OAuth2ResourceServer");
58 const std::string authorizationServerUri{
"http://localhost:8082"};
60 app.use(express::middleware::JsonMiddleware());
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";
72 const web::http::legacy::
in::Client legacyClient
(
73 [](web::http::legacy::
in::Client::SocketConnection* socketConnection) {
74 VLOG(1) <<
"OnConnect";
79 []([[maybe_unused]] web::http::legacy::
in::Client::SocketConnection* socketConnection) {
80 VLOG(1) <<
"OnConnected";
82 [](web::http::legacy::
in::Client::SocketConnection* socketConnection) {
83 VLOG(1) <<
"OnDisconnect";
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;
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());
102 const nlohmann::json errorJson = {{
"error",
"Invalid access token"}};
105 const nlohmann::json successJson = {{
"content",
"🦆"}};
110 []([[maybe_unused]]
const std::shared_ptr<web::http::client::
Request>& req) {
111 LOG(INFO) <<
" -- OnRequestEnd";
115 "localhost", 8082
, [](
const web::http::legacy::
in::Client::SocketAddress& socketAddress,
const core::socket::
State& state) {
118 VLOG(1) <<
"OAuth2ResourceServer: connected to '" << socketAddress
.toString() <<
"'";
121 VLOG(1) <<
"OAuth2ResourceServer: disabled";
124 VLOG(1) <<
"OAuth2ResourceServer: error occurred";
127 VLOG(1) <<
"OAuth2ResourceServer: fatal error occurred";
133 app
.listen(8083
, [](
const express::legacy::in::WebApp::SocketAddress& socketAddress,
const core::socket::
State& state) {
136 VLOG(1) <<
"app: listening on '" << socketAddress
.toString() <<
"'";
139 VLOG(1) <<
"app: disabled";
142 VLOG(1) <<
"app: error occurred";
145 VLOG(1) <<
"app: fatal error occurred";
#define APPLICATION(req, res)
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
const SocketAddress & getRemoteAddress() const final
const SocketAddress & getLocalAddress() const final
const std::string & query(const std::string &key) const
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
void send(const std::string &chunk)
Response & status(int status)
void sendStatus(int state)
WebAppT(const std::string &name)
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
std::string toString(bool expanded=true) const override
void connect(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
void listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
Client(const std::function< void(SocketConnection *)> &onConnect, const std::function< void(SocketConnection *)> &onConnected, const std::function< void(SocketConnection *)> &onDisconnect, std::function< void(const std::shared_ptr< Request > &)> &&onRequestBegin, std::function< void(const std::shared_ptr< Request > &)> &&onRequestEnd)
bool send(const std::string &chunk, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError=responseParseError)
int main(int argc, char *argv[])