Unsigned authorization with custom principals, in more detail.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../target/include/cedarling_c.h"
const char* BOOTSTRAP_CONFIG = "{\n"
" \"CEDARLING_APPLICATION_NAME\": \"UnsignedAuthExample\",\n"
" \"CEDARLING_LOG_TYPE\": \"std_out\",\n"
" \"CEDARLING_LOG_TTL\": 60,\n"
" \"CEDARLING_LOG_LEVEL\": \"DEBUG\",\n"
" \"CEDARLING_POLICY_STORE_LOCAL_FN\": \"../../../test_files/policy-store_ok_2.yaml\"\n"
"}";
const char* REQUEST_JSON = "{\n"
" \"principals\": [\n"
" {\n"
" \"cedar_entity_mapping\": {\n"
" \"entity_type\": \"Jans::TestPrincipal1\",\n"
" \"id\": \"test_principal_001\"\n"
" },\n"
" \"is_ok\": true\n"
" },\n"
" {\n"
" \"cedar_entity_mapping\": {\n"
" \"entity_type\": \"Jans::TestPrincipal2\",\n"
" \"id\": \"test_principal_002\"\n"
" },\n"
" \"is_ok\": true\n"
" }\n"
" ],\n"
" \"action\": \"Jans::Action::\\\"UpdateForTestPrincipals\\\"\",\n"
" \"resource\": {\n"
" \"cedar_entity_mapping\": {\n"
" \"entity_type\": \"Jans::Issue\",\n"
" \"id\": \"issue_12345\"\n"
" },\n"
" \"org_id\": \"org_engineering_001\",\n"
" \"country\": \"US\"\n"
" },\n"
" \"context\": {}\n"
"}";
void print_error(const char* operation) {
if (error_msg) {
printf("Error in %s: %s\n", operation, error_msg);
}
}
int main() {
printf("Cedarling C Bindings - Unsigned Authorization Example\n");
printf("=====================================================\n\n");
printf("1. Initializing Cedarling library...\n");
printf(" Failed to initialize Cedarling library\n");
return 1;
}
printf(" Library initialized successfully\n\n");
printf("3. Creating Cedarling instance...\n");
if (result != 0) {
printf(" Failed to create instance (return=%d, error_code=%d)\n", result,
printf(" %s\n",
print_error("cedarling_new");
return 1;
}
printf(" Instance created successfully (ID: %llu)\n\n", (unsigned long long)instance_id);
printf("4. Performing unsigned authorization...\n");
printf(" Using custom principals (TestPrincipal1, TestPrincipal2)\n");
if (result != 0) {
printf(" Authorization failed (return=%d, error_code=%d)\n", result,
printf(" %s\n",
print_error("cedarling_authorize_unsigned");
return 1;
}
printf(" Authorization completed successfully\n");
char* response = (
char*)auth_result.
data;
printf(" Response: %.200s%s\n\n", response, strlen(response) > 200 ? "..." : "");
if (strstr(response, "\"decision\":true") != NULL) {
printf(" >> ACCESS GRANTED (overall) <<\n\n");
} else {
printf(" >> ACCESS DENIED (overall) <<\n");
}
printf("5. Retrieving logs...\n");
if (result == 0) {
printf(
" Retrieved %zu log entries\n", logs.
count);
for (
size_t i = 0; i < logs.
count && i < 3; i++) {
printf(
" Log %zu: %.100s%s\n", i + 1, logs.
items[i],
strlen(logs.
items[i]) > 100 ?
"..." :
"");
}
printf(
" ... and %zu more\n", logs.
count - 3);
}
} else {
printf(" Failed to retrieve logs\n");
}
printf("\n");
printf("6. Shutting down...\n");
printf(" Cleanup completed\n\n");
printf("Example completed successfully!\n");
return 0;
}
int cedarling_pop_logs(uint64_t instance_id, struct CedarlingStringArray *result)
void cedarling_free_string(char *str_ptr)
void cedarling_free_result(struct CedarlingResult *result)
const char * cedarling_version(void)
void cedarling_cleanup(void)
void cedarling_free_instance_result(struct CedarlingInstanceResult *result)
int cedarling_new(const char *config_json, struct CedarlingInstanceResult *result)
int cedarling_authorize_unsigned(uint64_t instance_id, const char *request_json, struct CedarlingResult *result)
int cedarling_drop(uint64_t instance_id)
char * cedarling_get_last_error(void)
void cedarling_free_string_array(struct CedarlingStringArray *array)
int cedarling_shutdown(uint64_t instance_id)
Definition cedarling_c.h:58
enum CedarlingErrorCode error_code
Definition cedarling_c.h:62
uint64_t instance_id
Definition cedarling_c.h:66
char * error_message
Definition cedarling_c.h:70
Definition cedarling_c.h:76
char * data
Definition cedarling_c.h:84
char * error_message
Definition cedarling_c.h:88
enum CedarlingErrorCode error_code
Definition cedarling_c.h:80
Definition cedarling_c.h:94
char ** items
Definition cedarling_c.h:98
uintptr_t count
Definition cedarling_c.h:102