Skip to content

Commit 1670a09

Browse files
committed
Implement ACL generation for the enforcer.
1 parent 70a88bd commit 1670a09

File tree

6 files changed

+102
-8
lines changed

6 files changed

+102
-8
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ target_link_libraries(scitokens-verify SciTokens)
3737
add_executable(scitokens-test-access src/test_access.cpp)
3838
target_link_libraries(scitokens-test-access SciTokens)
3939

40+
add_executable(scitokens-list-access src/list_access.cpp)
41+
target_link_libraries(scitokens-list-access SciTokens)
42+
4043
if (NOT DEFINED LIB_INSTALL_DIR)
4144
SET(LIB_INSTALL_DIR "lib")
4245
endif()

src/list_access.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
3+
#include "scitokens.h"
4+
5+
int main(int argc, const char** argv) {
6+
if (argc < 4) {
7+
std::cerr << "Usage: " << argv[0] << " (TOKEN) (ISSUER) (AUDIENCE)" << std::endl;
8+
return 1;
9+
}
10+
std::string token(argv[1]);
11+
std::string issuer(argv[2]);
12+
std::string audience(argv[3]);
13+
14+
const char *aud_list[2];
15+
aud_list[0] = audience.c_str();
16+
aud_list[1] = nullptr;
17+
18+
SciToken scitoken;
19+
char *err_msg = nullptr;
20+
if (scitoken_deserialize(token.c_str(), &scitoken, nullptr, &err_msg)) {
21+
std::cout << "Failed to deserialize a token: " << err_msg << std::endl;
22+
return 1;
23+
}
24+
std::cout << "Token deserialization successful. Checking authorizations." << std::endl;
25+
Enforcer enf;
26+
if (!(enf = enforcer_create(issuer.c_str(), aud_list, &err_msg))) {
27+
std::cout << "Failed to create a new enforcer object: " << err_msg << std::endl;
28+
return 1;
29+
}
30+
Acl *acls;
31+
if (enforcer_generate_acls(enf, scitoken, &acls, &err_msg)) {
32+
std::cout << "ACL generation failed: " << err_msg << std::endl;
33+
return 1;
34+
}
35+
std::cout << "Start of ACLs:" << std::endl;
36+
for (int idx=0; acls[idx].authz && acls[idx].resource; idx++) {
37+
std::cout << "ACL: " << acls[idx].authz << ":" << acls[idx].resource << std::endl;
38+
}
39+
std::cout << "End of ACLs:" << std::endl;
40+
41+
enforcer_destroy(enf);
42+
return 0;
43+
}
44+

src/scitokens.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,49 @@ void enforcer_destroy(Enforcer enf) {
218218
delete real_enf;
219219
}
220220

221+
void enforcer_acl_free(Acl *acls) {
222+
for (int idx=0; acls[idx].authz == nullptr && acls[idx].resource == nullptr; idx++) {
223+
free(const_cast<char *>(acls[idx].authz));
224+
free(const_cast<char *>(acls[idx].resource));
225+
}
226+
free(acls);
227+
}
221228

222-
int enforcer_generate_acls(const Enforcer enf, const SciToken sci, char **Acl, char **err_msg) {
223-
if (err_msg) {
224-
*err_msg = strdup("This function is not implemented");
229+
230+
int enforcer_generate_acls(const Enforcer enf, const SciToken scitoken, Acl **acls, char **err_msg) {
231+
if (enf == nullptr) {
232+
if (err_msg) {*err_msg = strdup("Enforcer may not be a null pointer");}
233+
return -1;
225234
}
226-
return -1;
235+
auto real_enf = reinterpret_cast<scitokens::Enforcer*>(enf);
236+
if (scitoken == nullptr) {
237+
if (err_msg) {*err_msg = strdup("SciToken may not be a null pointer");}
238+
return -1;
239+
}
240+
auto real_scitoken = reinterpret_cast<scitokens::SciToken*>(scitoken);
241+
242+
scitokens::Enforcer::AclsList acls_list;
243+
try {
244+
acls_list = real_enf->generate_acls(*real_scitoken);
245+
} catch (std::exception &exc) {
246+
if (err_msg) {*err_msg = strdup(exc.what());}
247+
return -1;
248+
}
249+
Acl *acl_result = static_cast<Acl*>(malloc((acls_list.size() + 1)*sizeof(Acl)));
250+
size_t idx = 0;
251+
for (const auto &acl : acls_list) {
252+
acl_result[idx].authz = strdup(acl.first.c_str());
253+
acl_result[idx].resource = strdup(acl.second.c_str());
254+
if (acl_result[idx].authz == nullptr || acl_result[idx].resource == nullptr) {
255+
enforcer_acl_free(acl_result);
256+
return -1;
257+
}
258+
idx++;
259+
}
260+
acl_result[idx].authz = nullptr;
261+
acl_result[idx].resource = nullptr;
262+
*acls = acl_result;
263+
return 0;
227264
}
228265

229266

@@ -245,7 +282,7 @@ int enforcer_test(const Enforcer enf, const SciToken scitoken, const Acl *acl, c
245282

246283
try {
247284
return real_enf->test(*real_scitoken, acl->authz, acl->resource) == true ? 0 : -1;
248-
} catch (std::exception exc) {
285+
} catch (std::exception &exc) {
249286
if (err_msg) {*err_msg = strdup(exc.what());}
250287
return -1;
251288
}

src/scitokens.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Enforcer enforcer_create(const char *issuer, const char **audience, char **err_m
5050

5151
void enforcer_destroy(Enforcer);
5252

53-
int enforcer_generate_acls(const Enforcer enf, const SciToken sci, char **Acl, char **err_msg);
53+
int enforcer_generate_acls(const Enforcer enf, const SciToken scitokens, Acl **acls, char **err_msg);
5454

5555
int enforcer_test(const Enforcer enf, const SciToken sci, const Acl *acl, char **err_msg);
5656

src/scitokens_internal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,13 @@ scitokens::Enforcer::scope_validator(const jwt::claim &claim, void *myself) {
465465
path = normalize_absolute_path(path);
466466

467467
if (me->m_test_authz.empty()) {
468-
return false; // TODO: implement ACL generation.
468+
me->m_gen_acls.emplace_back(authz, path);
469469
} else if ((me->m_test_authz == authz) &&
470470
(requested_path.substr(0, path.size()) == path)) {
471471
return true;
472472
}
473473

474474
scope_iter = next_scope_iter;
475475
}
476-
return false;
476+
return me->m_test_authz.empty();
477477
}

src/scitokens_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ class Validator {
326326
class Enforcer {
327327

328328
public:
329+
typedef std::vector<std::pair<std::string, std::string>> AclsList;
330+
329331
Enforcer(std::string issuer, std::vector<std::string> audience_list)
330332
: m_issuer(issuer), m_audiences(audience_list)
331333
{
@@ -351,6 +353,12 @@ class Enforcer {
351353
}
352354
}
353355

356+
AclsList generate_acls(const SciToken &scitoken) {
357+
reset_state();
358+
m_validator.verify(scitoken);
359+
return m_gen_acls;
360+
}
361+
354362
private:
355363

356364
static bool all_validator(const jwt::claim &, void *) {return true;}
@@ -386,10 +394,12 @@ class Enforcer {
386394
void reset_state() {
387395
m_test_path = "";
388396
m_test_authz = "";
397+
m_gen_acls.clear();
389398
}
390399

391400
std::string m_test_path;
392401
std::string m_test_authz;
402+
AclsList m_gen_acls;
393403

394404
std::string m_issuer;
395405
std::vector<std::string> m_audiences;

0 commit comments

Comments
 (0)