Skip to content

Commit

Permalink
Merge pull request #95 from djw8605/add-enforcer-test
Browse files Browse the repository at this point in the history
Add enforcer test
  • Loading branch information
djw8605 authored Aug 9, 2022
2 parents 64f4c42 + a99adf9 commit 66f1a76
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,58 @@ TEST_F(SerializeTest, EnforcerTest) {
ASSERT_STREQ(err_msg, "token verification failed: 'scope' claim verification failed.");
ASSERT_TRUE(rv == -1) << err_msg;

}

TEST_F(SerializeTest, EnforcerScopeTest) {
char *err_msg = nullptr;

auto rv = scitoken_set_claim_string(m_token.get(), "aud",
"https://demo.scitokens.org/", &err_msg);
ASSERT_TRUE(rv == 0);

std::vector<const char *> audiences_array;
audiences_array.push_back("https://demo.scitokens.org/");
audiences_array.push_back(nullptr);

auto enforcer = enforcer_create("https://demo.scitokens.org/gtest", &audiences_array[0], &err_msg);
ASSERT_TRUE(enforcer != nullptr);

scitoken_set_serialize_profile(m_token.get(), SciTokenProfile::WLCG_1_0);

rv = scitoken_set_claim_string(m_token.get(), "scope",
"storage.modify:/ storage.read:/ openid offline_access", &err_msg);
ASSERT_TRUE(rv == 0);

char *token_value = nullptr;
rv = scitoken_serialize(m_token.get(), &token_value, &err_msg);
ASSERT_TRUE(rv == 0);

rv = scitoken_deserialize_v2(token_value, m_read_token.get(), nullptr, &err_msg);
ASSERT_TRUE(rv == 0);

Acl *acls;
enforcer_generate_acls(enforcer, m_read_token.get(), &acls, &err_msg);
ASSERT_TRUE(acls != nullptr);
int idx = 0;
bool found_read = false;
bool found_write = false;
while (acls[idx].resource && acls[idx++].authz) {
auto resource = acls[idx-1].resource;
auto authz = acls[idx-1].authz;
if (strcmp(authz, "read") == 0) {
found_read = true;
ASSERT_STREQ(resource, "/");
} else if (strcmp(authz, "write") == 0) {
found_write = true;
ASSERT_STREQ(resource, "/");
}
}
ASSERT_TRUE(found_read);
ASSERT_TRUE(found_write);




}

}
Expand Down

0 comments on commit 66f1a76

Please sign in to comment.