1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
Examples
========
This page provides practical examples of using the SciTokens C++ library for common tasks.
Simple Token Creation
---------------------
This example shows how to create a basic SciToken without signing:
.. code-block:: c
#include <scitokens.h>
#include <stdio.h>
int main() {
// Create a token without a key (for testing)
SciToken token = scitoken_create(NULL);
if (token == NULL) {
fprintf(stderr, "Failed to create token\n");
return 1;
}
// Clean up
scitoken_destroy(token);
printf("Token created successfully\n");
return 0;
}
Creating and Signing a Token
----------------------------
This example demonstrates how to create a key pair and sign a token:
.. code-block:: c
#include <scitokens.h>
#include <stdio.h>
#include <stdlib.h>
// Example EC private key
const char ec_private[] =
"-----BEGIN EC PRIVATE KEY-----\n"
"MHcCAQEEIESSMxT7PLTR9A/aqd+CM0/6vv6fQWqDm0mNx8uE9EbpoAoGCCqGSM49\n"
"AwEHoUQDQgAE1i+ImZ//iQhOPh0OMfZzdbmPH+3G1ouWezolCugQYWIRqNmwq3zR\n"
"EnTbe4EmymTpJ1MJTPP/tCEUP3G/QqQuhA==\n"
"-----END EC PRIVATE KEY-----\n";
// Example EC public key
const char ec_public[] =
"-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1i+ImZ//iQhOPh0OMfZzdbmPH+3G\n"
"1ouWezolCugQYWIRqNmwq3zREnTbe4EmymTpJ1MJTPP/tCEUP3G/QqQuhA==\n"
"-----END PUBLIC KEY-----\n";
int main() {
char *err_msg = NULL;
// Create a key for signing
SciTokenKey key = scitoken_key_create("test-key", "ES256",
ec_public, ec_private, &err_msg);
if (key == NULL) {
fprintf(stderr, "Failed to create key: %s\n", err_msg);
free(err_msg);
return 1;
}
// Create a token with the key
SciToken token = scitoken_create(key);
if (token == NULL) {
fprintf(stderr, "Failed to create token\n");
scitoken_key_destroy(key);
return 1;
}
// Set issuer claim
int rv = scitoken_set_claim_string(token, "iss",
"https://example.org", &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to set issuer: %s\n", err_msg);
free(err_msg);
scitoken_destroy(token);
scitoken_key_destroy(key);
return 1;
}
// Serialize the token
char *serialized_token;
rv = scitoken_serialize(token, &serialized_token, &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to serialize: %s\n", err_msg);
free(err_msg);
} else {
printf("Serialized token: %s\n", serialized_token);
free(serialized_token);
}
// Clean up
scitoken_destroy(token);
scitoken_key_destroy(key);
return 0;
}
Token Validation with Enforcer
------------------------------
This example shows how to use an Enforcer to generate ACLs from a token:
.. code-block:: c
#include <scitokens.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
char *err_msg = NULL;
// First, create and serialize a token (see previous example)
// For this example, we'll assume we have a token
// Create an enforcer
const char *audiences[] = {"https://example.org/", NULL};
Enforcer enforcer = enforcer_create("https://example.org",
audiences, &err_msg);
if (enforcer == NULL) {
fprintf(stderr, "Failed to create enforcer: %s\n", err_msg);
free(err_msg);
return 1;
}
// Assuming we have a valid token from previous steps
SciToken token = NULL; // This would be your deserialized token
// Generate ACLs from the token
Acl *acls = NULL;
int rv = enforcer_generate_acls(enforcer, token, &acls, &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to generate ACLs: %s\n", err_msg);
free(err_msg);
} else {
// Print the ACLs
for (int i = 0; acls[i].authz != NULL || acls[i].resource != NULL; i++) {
printf("ACL %d: %s on %s\n", i,
acls[i].authz ? acls[i].authz : "(null)",
acls[i].resource ? acls[i].resource : "(null)");
}
// Free the ACLs
enforcer_acl_free(acls);
}
// Clean up
enforcer_destroy(enforcer);
if (token) scitoken_destroy(token);
return 0;
}
Complete Example: Create, Sign, and Validate
--------------------------------------------
This comprehensive example demonstrates the full workflow:
.. code-block:: c
#include <scitokens.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
char *err_msg = NULL;
int rv;
// Keys for this example
const char ec_private[] = "-----BEGIN EC PRIVATE KEY-----\n"
"MHcCAQEEIESSMxT7PLTR9A/aqd+CM0/6vv6fQWqDm0mNx8uE9EbpoAoGCCqGSM49\n"
"AwEHoUQDQgAE1i+ImZ//iQhOPh0OMfZzdbmPH+3G1ouWezolCugQYWIRqNmwq3zR\n"
"EnTbe4EmymTpJ1MJTPP/tCEUP3G/QqQuhA==\n"
"-----END EC PRIVATE KEY-----\n";
const char ec_public[] = "-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1i+ImZ//iQhOPh0OMfZzdbmPH+3G\n"
"1ouWezolCugQYWIRqNmwq3zREnTbe4EmymTpJ1MJTPP/tCEUP3G/QqQuhA==\n"
"-----END PUBLIC KEY-----\n";
// Step 1: Create signing key
SciTokenKey key = scitoken_key_create("1", "ES256",
ec_public, ec_private, &err_msg);
if (!key) {
fprintf(stderr, "Failed to create key: %s\n", err_msg);
free(err_msg);
return 1;
}
// Step 2: Create and configure token
SciToken token = scitoken_create(key);
if (!token) {
fprintf(stderr, "Failed to create token\n");
scitoken_key_destroy(key);
return 1;
}
// Set token claims
rv = scitoken_set_claim_string(token, "iss",
"https://demo.scitokens.org/gtest", &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to set issuer: %s\n", err_msg);
goto cleanup;
}
rv = scitoken_set_claim_string(token, "aud",
"https://demo.scitokens.org/", &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to set audience: %s\n", err_msg);
goto cleanup;
}
rv = scitoken_set_claim_string(token, "scope", "read:/data", &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to set scope: %s\n", err_msg);
goto cleanup;
}
// Step 3: Serialize token
char *serialized;
rv = scitoken_serialize(token, &serialized, &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to serialize: %s\n", err_msg);
goto cleanup;
}
printf("Created token: %s\n", serialized);
// Step 4: Store public key for validation
rv = scitoken_store_public_ec_key("https://demo.scitokens.org/gtest",
"1", ec_public, &err_msg);
if (rv != 0) {
fprintf(stderr, "Failed to store public key: %s\n", err_msg);
free(serialized);
goto cleanup;
}
// Step 5: Deserialize and validate
SciToken parsed_token;
const char *allowed_issuers[] = {"https://demo.scitokens.org/gtest", NULL};
rv = scitoken_deserialize(serialized, &parsed_token,
allowed_issuers, &err_msg);
free(serialized);
if (rv != 0) {
fprintf(stderr, "Failed to deserialize: %s\n", err_msg);
goto cleanup;
}
printf("Token validation successful!\n");
// Step 6: Create enforcer and generate ACLs
const char *audiences[] = {"https://demo.scitokens.org/", NULL};
Enforcer enforcer = enforcer_create("https://demo.scitokens.org/gtest",
audiences, &err_msg);
if (enforcer) {
Acl *acls;
rv = enforcer_generate_acls(enforcer, parsed_token, &acls, &err_msg);
if (rv == 0) {
printf("Generated ACLs:\n");
for (int i = 0; acls[i].authz || acls[i].resource; i++) {
printf(" %s: %s\n",
acls[i].authz ? acls[i].authz : "(null)",
acls[i].resource ? acls[i].resource : "(null)");
}
enforcer_acl_free(acls);
}
enforcer_destroy(enforcer);
}
scitoken_destroy(parsed_token);
cleanup:
if (err_msg) free(err_msg);
scitoken_destroy(token);
scitoken_key_destroy(key);
return rv;
}
|