File tree Expand file tree Collapse file tree
modules/openapi-generator/src/main/resources/cpp-pistache-server Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,11 +16,13 @@ namespace {{apiNamespace}}
1616
1717
1818 {{#authMethods} }{ {#isBasicBasic} }
19- struct HttpBasicCredentials
19+ typedef struct
2020 {
21- std::string userid ;
21+ std::string user ;
2222 std::string password;
23- } ;
23+ } HttpBasicCredentials;
24+
25+ typedef std::function<bool (const HttpBasicCredentials &) > BasicCredentialsAuthenticator;
2426 { {/isBasicBasic} }{ {/authMethods} }
2527
2628
@@ -31,8 +33,21 @@ public:
3133 virtual ~ApiBase() = default;
3234 virtual void init() = 0;
3335
36+ { {#authMethods} }{ {#isBasicBasic} }
37+ bool canCredentialsBeAccepted(const HttpBasicCredentials& credentials) const;
38+ void setBasicCredentialsAuthenticator( const BasicCredentialsAuthenticator &newBasicCredentialsAuthenticator)
39+ {
40+ basicCredentialsAuthenticator = newBasicCredentialsAuthenticator;
41+ }
42+
43+ { {/isBasicBasic} }{ {/authMethods} }
44+
45+
3446protected:
3547 const std::shared_ptr<Pistache::Rest::Router > router;
48+ { {#authMethods} }{ {#isBasicBasic} }std::optional<BasicCredentialsAuthenticator > basicCredentialsAuthenticator;{ {/isBasicBasic} }{ {/authMethods} }
49+
50+
3651};
3752
3853} // namespace { {apiNamespace} }
Original file line number Diff line number Diff line change @@ -13,6 +13,37 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
1313{ {classname} }Impl::{ {classname} }Impl(const std::shared_ptr<Pistache::Rest::Router >& rtr)
1414 : { {classname} }(rtr)
1515{
16+ {{#authMethods} }{ {#isBasicBasic} }/*
17+
18+ Http Basic Auth
19+
20+ Do this in the individual classes in the constructor
21+
22+ this->setBasicCredentialsAuthenticator(
23+ [](const HttpBasicCredentials &credentials)->bool
24+ {
25+ return credentials.user == " foo" && credentials.password == " bar" ;
26+ }
27+ );
28+
29+ or in main:
30+
31+ for (auto api : apiImpls) {
32+ api-> init ();
33+
34+ setBasicCredentialsAuthenticator(
35+ [](const HttpBasicCredentials &credentials)-> bool
36+ {
37+ return credentials.user == " foo" && credentials.password == " bar" ;
38+ }
39+ );
40+ }
41+
42+ or a mix.
43+
44+ Until you do either, protected resources will result in a 401.
45+ */{ {/isBasicBasic} }{ {/authMethods} }
46+
1647}
1748
1849{ {#operation} }
Original file line number Diff line number Diff line change @@ -15,8 +15,7 @@ const std::string {{classname}}::base = "{{basePathWithoutHost}}";
1515
1616{ {classname} }::{ {classname} }(const std::shared_ptr<Pistache::Rest::Router >& rtr)
1717 : ApiBase(rtr)
18- {
19- }
18+ { }
2019
2120void { {classname} }::init() {
2221 setupRoutes();
@@ -122,7 +121,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
122121
123122 {{/bodyParam} }
124123 { {/hasBodyParam} }
125- { {#authMethods} }{ {#isBasicBasic} }
124+ { {#authMethods} }{ {#isBasicBasic} }
126125 auto basicAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization >();
127126
128127 if( (!basicAuthHeader) || (basicAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Basic))
@@ -131,6 +130,18 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
131130 return;
132131 }
133132 HttpBasicCredentials credentials{ basicAuthHeader-> getBasicUser (), basicAuthHeader-> getBasicPassword ()} ;
133+ if( ! this->basicCredentialsAuthenticator.has_value())
134+ {
135+ response.send(Pistache::Http::Code::Unauthorized, " " );
136+ return;
137+ }
138+
139+ if( ! this->basicCredentialsAuthenticator.value()(credentials))
140+ {
141+ response.send(Pistache::Http::Code::Unauthorized, " " );
142+ return;
143+ }
144+
134145 { {/isBasicBasic} }{ {/authMethods} }
135146
136147 { {#authMethods} }{ {#isBasicBearer} }
You can’t perform that action at this time.
0 commit comments