Skip to content

Commit 94ce7ce

Browse files
mowijoKRJ-RTX
authored andcommitted
[cpp][pistache-server] Add void* userdata to HttpBasicCredentials.
This allows for data ft be passed on from the authenticator to the handler implementation. For example a userid that has already been looked up
1 parent df0a1cc commit 94ce7ce

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ namespace {{apiNamespace}}
2020
{
2121
std::string user;
2222
std::string password;
23+
std::unique_ptr<void, std::function<void(void*)>> userdata;
2324
} HttpBasicCredentials;
2425

25-
typedef std::function<bool(const HttpBasicCredentials &)> BasicCredentialsAuthenticator;
26+
typedef std::function<bool(HttpBasicCredentials &)> BasicCredentialsAuthenticator;
2627
{{/isBasicBasic}}{{/authMethods}}
2728

2829

@@ -34,7 +35,7 @@ public:
3435
virtual void init() = 0;
3536

3637
{{#authMethods}}{{#isBasicBasic}}
37-
bool canCredentialsBeAccepted(const HttpBasicCredentials& credentials) const;
38+
bool canCredentialsBeAccepted(HttpBasicCredentials& credentials) const;
3839
void setBasicCredentialsAuthenticator( const BasicCredentialsAuthenticator &newBasicCredentialsAuthenticator)
3940
{
4041
basicCredentialsAuthenticator = newBasicCredentialsAuthenticator;

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,54 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
1616
{{#authMethods}}{{#isBasicBasic}}/*
1717

1818
Http Basic Auth
19+
===============
1920

2021
Do this in the individual classes in the constructor
2122

2223
this->setBasicCredentialsAuthenticator(
23-
[](const HttpBasicCredentials &credentials)->bool
24-
{
25-
return credentials.user == "foo" && credentials.password == "bar";
26-
}
27-
);
24+
[](HttpBasicCredentials &credentials)->bool
25+
{
26+
if(credentials.user == "foo" && credentials.password == "bar")
27+
{
28+
29+
const int userIdOfFoo = 66;
30+
credentials.userdata = std::unique_ptr<void, std::function<void(void*)>> (
31+
reinterpret_cast<void*>(new int(userIdOfFoo)),
32+
[&](void* ptr)
33+
{
34+
int * value = reinterpret_cast<int*>(ptr);
35+
delete value;
36+
}
37+
);
38+
return true;
39+
}
40+
return false;
41+
}
42+
);
2843

2944
or in main:
3045

3146
for (auto api : apiImpls) {
3247
api->init();
3348
34-
setBasicCredentialsAuthenticator(
35-
[](const HttpBasicCredentials &credentials)->bool
49+
api->setBasicCredentialsAuthenticator(
50+
[]( HttpBasicCredentials &credentials)->bool
3651
{
37-
return credentials.user == "foo" && credentials.password == "bar";
52+
if(credentials.user == "foo" && credentials.password == "bar")
53+
{
54+
55+
const int userIdOfFoo = 66;
56+
credentials.userdata = std::unique_ptr<void, std::function<void(void*)>> (
57+
reinterpret_cast<void*>(new int(userIdOfFoo)),
58+
[&](void* ptr)
59+
{
60+
int * value = reinterpret_cast<int*>(ptr);
61+
delete value;
62+
}
63+
);
64+
return true;
65+
}
66+
return false;
3867
}
3968
);
4069
}

0 commit comments

Comments
 (0)