Cedarling C API
Loading...
Searching...
No Matches
authorize_unsigned.c

Unsigned authorization with custom principals, in more detail.

/*
* This software is available under the Apache-2.0 license.
* See https://www.apache.org/licenses/LICENSE-2.0.txt for full text.
*
* Copyright (c) 2025, Gluu, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../target/include/cedarling_c.h"
// Configuration JSON - uses policy-store_ok_2.yaml for unsigned authorization
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"
"}";
// Unsigned authorization request with custom principals
// Uses TestPrincipal1/TestPrincipal2 entity types defined in policy-store_ok_2.yaml
// Note: attributes must match the schema (is_ok: Bool)
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) {
char* error_msg = cedarling_get_last_error();
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");
// Initialize the library
printf("1. Initializing Cedarling library...\n");
if (cedarling_init() != 0) {
printf(" Failed to initialize Cedarling library\n");
return 1;
}
printf(" Library initialized successfully\n\n");
// Print version
printf("2. Library version: %s\n\n", cedarling_version());
// Create a new instance
printf("3. Creating Cedarling instance...\n");
CedarlingInstanceResult instance_result;
int result = cedarling_new(BOOTSTRAP_CONFIG, &instance_result);
if (result != 0) {
printf(" Failed to create instance (return=%d, error_code=%d)\n", result,
(int)instance_result.error_code);
printf(" %s\n",
instance_result.error_message ? instance_result.error_message : "(no message)");
cedarling_free_instance_result(&instance_result);
print_error("cedarling_new");
return 1;
}
uint64_t instance_id = instance_result.instance_id;
printf(" Instance created successfully (ID: %llu)\n\n", (unsigned long long)instance_id);
cedarling_free_instance_result(&instance_result);
// Perform unsigned authorization
printf("4. Performing unsigned authorization...\n");
printf(" Using custom principals (TestPrincipal1, TestPrincipal2)\n");
CedarlingResult auth_result;
result = cedarling_authorize_unsigned(instance_id, REQUEST_JSON, &auth_result);
if (result != 0) {
printf(" Authorization failed (return=%d, error_code=%d)\n", result,
(int)auth_result.error_code);
printf(" %s\n",
auth_result.error_message ? auth_result.error_message : "(no message)");
print_error("cedarling_authorize_unsigned");
cedarling_free_result(&auth_result);
cedarling_drop(instance_id);
return 1;
}
printf(" Authorization completed successfully\n");
char* response = (char*)auth_result.data;
printf(" Response: %.200s%s\n\n", response, strlen(response) > 200 ? "..." : "");
// Check the overall decision
if (strstr(response, "\"decision\":true") != NULL) {
printf(" >> ACCESS GRANTED (overall) <<\n\n");
} else {
printf(" >> ACCESS DENIED (overall) <<\n");
}
cedarling_free_result(&auth_result);
// Get logs
printf("5. Retrieving logs...\n");
result = cedarling_pop_logs(instance_id, &logs);
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 ? "..." : "");
}
if (logs.count > 3) {
printf(" ... and %zu more\n", logs.count - 3);
}
} else {
printf(" Failed to retrieve logs\n");
}
printf("\n");
// Shutdown and cleanup
printf("6. Shutting down...\n");
cedarling_shutdown(instance_id);
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)
int cedarling_init(void)
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