|
| 1 | +# SciTokens Offline Cache Support |
| 2 | + |
| 3 | +This document describes the offline cache functionality added to the scitokens-cpp library. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The scitokens library now supports offline operation through a direct SQLite cache file that can be pre-populated with JWKS data. This enables environments where external network access to fetch public keys is not available or desired. |
| 8 | + |
| 9 | +## New Features |
| 10 | + |
| 11 | +### 1. SCITOKENS_KEYCACHE_FILE Environment Variable |
| 12 | + |
| 13 | +When set, this environment variable points directly to a SQLite database file that will be used for the key cache, bypassing the normal cache location resolution (XDG_CACHE_HOME, ~/.cache, etc.). |
| 14 | + |
| 15 | +```bash |
| 16 | +export SCITOKENS_KEYCACHE_FILE=/path/to/offline.db |
| 17 | +``` |
| 18 | + |
| 19 | +### 2. scitokens-keycache Command Line Tool |
| 20 | + |
| 21 | +A new command-line utility for creating and managing offline cache files. |
| 22 | + |
| 23 | +#### Usage |
| 24 | +```bash |
| 25 | +scitokens-keycache --cache-file <cache_file> --jwks <jwks_file> --issuer <issuer> --valid-for <seconds> |
| 26 | +``` |
| 27 | + |
| 28 | +#### Options |
| 29 | +- `--cache-file <file>`: Path to the keycache SQLite database file |
| 30 | +- `--jwks <file>`: Path to the JWKS file to store |
| 31 | +- `--issuer <issuer>`: Issuer URL for the JWKS |
| 32 | +- `--valid-for <seconds>`: How long the key should be valid (in seconds) |
| 33 | +- `--help`: Show help message |
| 34 | + |
| 35 | +#### Example |
| 36 | +```bash |
| 37 | +scitokens-keycache --cache-file /opt/scitokens/offline.db \ |
| 38 | + --jwks issuer_keys.json \ |
| 39 | + --issuer https://tokens.example.com \ |
| 40 | + --valid-for 86400 |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. New API Function |
| 44 | + |
| 45 | +A new C API function allows programmatic storage of JWKS with explicit expiration times: |
| 46 | + |
| 47 | +```c |
| 48 | +int keycache_set_jwks_with_expiry(const char *issuer, const char *jwks, |
| 49 | + int64_t expires_at, char **err_msg); |
| 50 | +``` |
| 51 | +
|
| 52 | +Where `expires_at` is the expiration time as a Unix timestamp (seconds since epoch). |
| 53 | +
|
| 54 | +## Usage Workflow |
| 55 | +
|
| 56 | +### Setting up an Offline Cache |
| 57 | +
|
| 58 | +1. **Create JWKS file**: Save the issuer's public keys in a JSON Web Key Set format |
| 59 | + ```json |
| 60 | + { |
| 61 | + "keys": [ |
| 62 | + { |
| 63 | + "kty": "EC", |
| 64 | + "kid": "key-1", |
| 65 | + "use": "sig", |
| 66 | + "alg": "ES256", |
| 67 | + "x": "...", |
| 68 | + "y": "...", |
| 69 | + "crv": "P-256" |
| 70 | + } |
| 71 | + ] |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | +2. **Create cache file**: Use the scitokens-keycache tool |
| 76 | + ```bash |
| 77 | + scitokens-keycache --cache-file /opt/tokens/cache.db \ |
| 78 | + --jwks issuer_keys.json \ |
| 79 | + --issuer https://tokens.example.com \ |
| 80 | + --valid-for 2592000 # 30 days |
| 81 | + ``` |
| 82 | + |
| 83 | +3. **Configure application**: Set the environment variable |
| 84 | + ```bash |
| 85 | + export SCITOKENS_KEYCACHE_FILE=/opt/tokens/cache.db |
| 86 | + ``` |
| 87 | + |
| 88 | +### Using the Offline Cache |
| 89 | + |
| 90 | +Once configured, the existing scitokens API functions work normally: |
| 91 | + |
| 92 | +```c |
| 93 | +char *jwks = NULL; |
| 94 | +char *err_msg = NULL; |
| 95 | +int result = keycache_get_cached_jwks("https://tokens.example.com", &jwks, &err_msg); |
| 96 | +if (result == 0 && jwks) { |
| 97 | + // Process the JWKS |
| 98 | + free(jwks); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Backward Compatibility |
| 103 | + |
| 104 | +All existing functionality remains unchanged. The new features are: |
| 105 | +- Additive API extensions |
| 106 | +- Optional environment variable |
| 107 | +- New command-line tool |
| 108 | + |
| 109 | +Existing code will continue to work without modification. |
| 110 | + |
| 111 | +## Cache Location Priority |
| 112 | + |
| 113 | +The cache file location is determined in this order: |
| 114 | +1. `SCITOKENS_KEYCACHE_FILE` environment variable (highest priority - for offline use) |
| 115 | +2. User-configured cache home via config API |
| 116 | +3. `XDG_CACHE_HOME` environment variable |
| 117 | +4. `~/.cache` directory (lowest priority) |
| 118 | + |
| 119 | +## Security Considerations |
| 120 | + |
| 121 | +- Ensure offline cache files have appropriate file permissions (600 or 640) |
| 122 | +- Regularly update offline caches with fresh keys before expiration |
| 123 | +- Consider key rotation policies when setting expiration times |
| 124 | +- Validate JWKS content before adding to offline caches |
0 commit comments