Skip to content

Commit 0302640

Browse files
mowijoKRJ-RTX
authored andcommitted
[cpp][pistache-server] Add extraction and forwarding of credentials for HTTP Basic protected endpoints.
1 parent 64c8711 commit 0302640

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private:
7979
{{#allParams}}
8080
/// <param name="{{paramName}}">{{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}</param>
8181
{{/allParams}}
82-
virtual void {{operationIdSnakeCase}}({{#allParams}}const {{#isModel}}{{^isOptional}}{{modelNamespace}}::{{/isOptional}}{{/isModel}}{{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) = 0;
82+
virtual void {{operationIdSnakeCase}}({{#authMethods}}{{#isBasicBasic}}const std::string &username, const std::string &password, {{/isBasicBasic}}{{/authMethods}} {{#allParams}}const {{#isModel}}{{^isOptional}}{{modelNamespace}}::{{/isOptional}}{{/isModel}}{{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) = 0;
8383
{{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}
8484
{{^vendorExtensions.x-codegen-pistache-is-parsing-supported}}
8585
virtual void {{operationIdSnakeCase}}(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response) = 0;

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public:
3535

3636
{{#operation}}
3737
{{#vendorExtensions.x-codegen-pistache-is-parsing-supported}}
38-
void {{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response);
38+
void {{operationIdSnakeCase}}({{#authMethods}}{{#isBasicBasic}}const std::string &username, const std::string &password,{{/isBasicBasic}}{{/authMethods}}{{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response);
3939
{{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}
4040
{{^vendorExtensions.x-codegen-pistache-is-parsing-supported}}
4141
void {{operationIdSnakeCase}}(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response);

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-source.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
1717

1818
{{#operation}}
1919
{{#vendorExtensions.x-codegen-pistache-is-parsing-supported}}
20-
void {{classname}}Impl::{{operationIdSnakeCase}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) {
20+
void {{classname}}Impl::{{operationIdSnakeCase}}({{#authMethods}}{{#isBasicBasic}}const std::string &username, const std::string &password,{{/isBasicBasic}}{{/authMethods}}{{#allParams}}const {{{dataType}}} &{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}Pistache::Http::ResponseWriter &response) {
2121
response.send(Pistache::Http::Code::Ok, "Do some magic\n");
2222
}
2323
{{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,26 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
119119
}
120120

121121
try {
122+
122123
{{/bodyParam}}
123124
{{/hasBodyParam}}
124-
this->{{operationIdSnakeCase}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}response);
125+
{{#authMethods}}{{#isBasicBasic}}
126+
auto basicAuthHeader = request.headers().tryGet<Pistache::Http::Header::Authorization>();
127+
128+
if( (!basicAuthHeader) || (basicAuthHeader->getMethod() != Pistache::Http::Header::Authorization::Method::Basic))
129+
{
130+
response.send(Pistache::Http::Code::Unauthorized, "");
131+
return;
132+
}
133+
const std::string username(basicAuthHeader->getBasicUser());
134+
const std::string password(basicAuthHeader->getBasicPassword());
135+
{{/isBasicBasic}}{{/authMethods}}
136+
137+
{{#authMethods}}{{#isBasicBearer}}
138+
/* BASIC BEARER */
139+
{{/isBasicBearer}}{{/authMethods}}
140+
141+
this->{{operationIdSnakeCase}}({{#authMethods}}{{#isBasicBasic}}username, password,{{/isBasicBasic}}{{/authMethods}}{{#allParams}}{{paramName}}, {{/allParams}}response);
125142
{{/vendorExtensions.x-codegen-pistache-is-parsing-supported}}
126143
{{^vendorExtensions.x-codegen-pistache-is-parsing-supported}}
127144
try {

0 commit comments

Comments
 (0)