@@ -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 }
0 commit comments