|
| 1 | +#ifndef JUICEBOX_FFI_H_ |
| 2 | +#define JUICEBOX_FFI_H_ |
| 3 | + |
| 4 | +/* This file was automatically generated by cbindgen */ |
| 5 | + |
| 6 | +#include <stdarg.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * Error returned during `Client.delete` |
| 13 | + */ |
| 14 | +typedef enum { |
| 15 | + /** |
| 16 | + * A realm rejected the `Client`'s auth token. |
| 17 | + */ |
| 18 | + JuiceboxDeleteErrorInvalidAuth = 0, |
| 19 | + /** |
| 20 | + * The SDK software is too old to communicate with this realm |
| 21 | + * and must be upgraded. |
| 22 | + */ |
| 23 | + JuiceboxDeleteErrorUpgradeRequired = 1, |
| 24 | + /** |
| 25 | + * A software error has occurred. This request should not be retried |
| 26 | + * with the same parameters. Verify your inputs, check for software |
| 27 | + * updates and try again. |
| 28 | + */ |
| 29 | + JuiceboxDeleteErrorAssertion = 2, |
| 30 | + /** |
| 31 | + * A transient error in sending or receiving requests to a realm. |
| 32 | + * This request may succeed by trying again with the same parameters. |
| 33 | + */ |
| 34 | + JuiceboxDeleteErrorTransient = 3, |
| 35 | +} JuiceboxDeleteError; |
| 36 | + |
| 37 | +typedef enum { |
| 38 | + JuiceboxHttpRequestMethodGet = 0, |
| 39 | + JuiceboxHttpRequestMethodPut, |
| 40 | + JuiceboxHttpRequestMethodPost, |
| 41 | + JuiceboxHttpRequestMethodDelete, |
| 42 | +} JuiceboxHttpRequestMethod; |
| 43 | + |
| 44 | +typedef enum { |
| 45 | + /** |
| 46 | + * A tuned hash, secure for use on modern devices as of 2019 with low-entropy PINs. |
| 47 | + */ |
| 48 | + JuiceboxPinHashingModeStandard2019 = 0, |
| 49 | + /** |
| 50 | + * A fast hash used for testing. Do not use in production. |
| 51 | + */ |
| 52 | + JuiceboxPinHashingModeFastInsecure = 1, |
| 53 | +} JuiceboxPinHashingMode; |
| 54 | + |
| 55 | +/** |
| 56 | + * Error returned during `Client.recover` |
| 57 | + */ |
| 58 | +typedef enum { |
| 59 | + /** |
| 60 | + * The secret could not be unlocked, but you can try again |
| 61 | + * with a different PIN if you have guesses remaining. If no |
| 62 | + * guesses remain, this secret is locked and inaccessible. |
| 63 | + */ |
| 64 | + JuiceboxRecoverErrorReasonInvalidPin = 0, |
| 65 | + /** |
| 66 | + * The secret was not registered or not fully registered with the |
| 67 | + * provided realms. |
| 68 | + */ |
| 69 | + JuiceboxRecoverErrorReasonNotRegistered = 1, |
| 70 | + /** |
| 71 | + * A realm rejected the `Client`'s auth token. |
| 72 | + */ |
| 73 | + JuiceboxRecoverErrorReasonInvalidAuth = 2, |
| 74 | + /** |
| 75 | + * The SDK software is too old to communicate with this realm |
| 76 | + * and must be upgraded. |
| 77 | + */ |
| 78 | + JuiceboxRecoverErrorReasonUpgradeRequired = 3, |
| 79 | + /** |
| 80 | + * A software error has occurred. This request should not be retried |
| 81 | + * with the same parameters. Verify your inputs, check for software |
| 82 | + * updates and try again. |
| 83 | + */ |
| 84 | + JuiceboxRecoverErrorReasonAssertion = 4, |
| 85 | + /** |
| 86 | + * A transient error in sending or receiving requests to a realm. |
| 87 | + * This request may succeed by trying again with the same parameters. |
| 88 | + */ |
| 89 | + JuiceboxRecoverErrorReasonTransient = 5, |
| 90 | +} JuiceboxRecoverErrorReason; |
| 91 | + |
| 92 | +/** |
| 93 | + * Error returned during `Client.register` |
| 94 | + */ |
| 95 | +typedef enum { |
| 96 | + /** |
| 97 | + * A realm rejected the `Client`'s auth token. |
| 98 | + */ |
| 99 | + JuiceboxRegisterErrorInvalidAuth = 0, |
| 100 | + /** |
| 101 | + * The SDK software is too old to communicate with this realm |
| 102 | + * and must be upgraded. |
| 103 | + */ |
| 104 | + JuiceboxRegisterErrorUpgradeRequired = 1, |
| 105 | + /** |
| 106 | + * A software error has occurred. This request should not be retried |
| 107 | + * with the same parameters. Verify your inputs, check for software |
| 108 | + * updates and try again. |
| 109 | + */ |
| 110 | + JuiceboxRegisterErrorAssertion = 2, |
| 111 | + /** |
| 112 | + * A transient error in sending or receiving requests to a realm. |
| 113 | + * This request may succeed by trying again with the same parameters. |
| 114 | + */ |
| 115 | + JuiceboxRegisterErrorTransient = 3, |
| 116 | +} JuiceboxRegisterError; |
| 117 | + |
| 118 | +typedef struct JuiceboxAuthToken JuiceboxAuthToken; |
| 119 | + |
| 120 | +typedef struct JuiceboxAuthTokenGenerator JuiceboxAuthTokenGenerator; |
| 121 | + |
| 122 | +typedef struct JuiceboxAuthTokenManager JuiceboxAuthTokenManager; |
| 123 | + |
| 124 | +typedef struct JuiceboxClient JuiceboxClient; |
| 125 | + |
| 126 | +typedef struct JuiceboxConfiguration JuiceboxConfiguration; |
| 127 | + |
| 128 | +typedef struct JuiceboxHttpClient JuiceboxHttpClient; |
| 129 | + |
| 130 | +typedef struct { |
| 131 | + JuiceboxConfiguration *const *data; |
| 132 | + size_t length; |
| 133 | +} JuiceboxUnmanagedConfigurationArray; |
| 134 | + |
| 135 | +typedef void (*JuiceboxAuthTokenGetCallbackFn)(JuiceboxAuthTokenManager *context, |
| 136 | + uint64_t context_id, |
| 137 | + const JuiceboxAuthToken *auth_token); |
| 138 | + |
| 139 | +typedef void (*JuiceboxAuthTokenGetFn)(const JuiceboxAuthTokenManager *context, |
| 140 | + uint64_t context_id, |
| 141 | + const uint8_t (*realm_id)[16], |
| 142 | + JuiceboxAuthTokenGetCallbackFn callback); |
| 143 | + |
| 144 | +typedef struct { |
| 145 | + const char *name; |
| 146 | + const char *value; |
| 147 | +} JuiceboxHttpHeader; |
| 148 | + |
| 149 | +typedef struct { |
| 150 | + const JuiceboxHttpHeader *data; |
| 151 | + size_t length; |
| 152 | +} JuiceboxUnmanagedHttpHeaderArray; |
| 153 | + |
| 154 | +typedef struct { |
| 155 | + const uint8_t *data; |
| 156 | + size_t length; |
| 157 | +} JuiceboxUnmanagedDataArray; |
| 158 | + |
| 159 | +typedef struct { |
| 160 | + uint8_t id[16]; |
| 161 | + JuiceboxHttpRequestMethod method; |
| 162 | + const char *url; |
| 163 | + JuiceboxUnmanagedHttpHeaderArray headers; |
| 164 | + JuiceboxUnmanagedDataArray body; |
| 165 | +} JuiceboxHttpRequest; |
| 166 | + |
| 167 | +typedef struct { |
| 168 | + uint8_t id[16]; |
| 169 | + uint16_t status_code; |
| 170 | + JuiceboxUnmanagedHttpHeaderArray headers; |
| 171 | + JuiceboxUnmanagedDataArray body; |
| 172 | +} JuiceboxHttpResponse; |
| 173 | + |
| 174 | +typedef void (*JuiceboxHttpResponseFn)(JuiceboxHttpClient *context, |
| 175 | + const JuiceboxHttpResponse *response); |
| 176 | + |
| 177 | +typedef void (*JuiceboxHttpSendFn)(const JuiceboxHttpClient *context, |
| 178 | + const JuiceboxHttpRequest *request, |
| 179 | + JuiceboxHttpResponseFn callback); |
| 180 | + |
| 181 | +typedef struct { |
| 182 | + uint8_t id[16]; |
| 183 | + const char *address; |
| 184 | + const JuiceboxUnmanagedDataArray *public_key; |
| 185 | +} JuiceboxRealm; |
| 186 | + |
| 187 | +typedef struct { |
| 188 | + const JuiceboxRealm *data; |
| 189 | + size_t length; |
| 190 | +} JuiceboxUnmanagedRealmArray; |
| 191 | + |
| 192 | +typedef struct { |
| 193 | + JuiceboxRecoverErrorReason reason; |
| 194 | + /** |
| 195 | + * If non-NULL, the number of guesses remaining after an Unsuccessful attempt. |
| 196 | + */ |
| 197 | + const uint16_t *guesses_remaining; |
| 198 | +} JuiceboxRecoverError; |
| 199 | + |
| 200 | +typedef struct { |
| 201 | + uint8_t realm_id[16]; |
| 202 | + uint8_t secret_id[16]; |
| 203 | +} JuiceboxAuthTokenParameters; |
| 204 | + |
| 205 | +typedef void (*JuiceboxAuthTokenStringCallbackFn)(JuiceboxAuthToken *auth_token, |
| 206 | + const void *context, |
| 207 | + const char *string); |
| 208 | + |
| 209 | +/** |
| 210 | + * Constructs a new opaque `JuiceboxClient`. |
| 211 | + * |
| 212 | + * # Arguments |
| 213 | + * |
| 214 | + * * `configuration` – Represents the current configuration. The configuration |
| 215 | + * provided must include at least one `JuiceboxRealm`. |
| 216 | + * * `previous_configurations` – Represents any other configurations you have |
| 217 | + * previously registered with that you may not yet have migrated the data from. |
| 218 | + * During `juicebox_client_recover`, they will be tried if the current user has not yet |
| 219 | + * registered on the current configuration. These should be ordered from most recently |
| 220 | + * to least recently used. |
| 221 | + * * `auth_token` – Represents the authority to act as a particular user |
| 222 | + * and should be valid for the lifetime of the `JuiceboxClient`. |
| 223 | + * * `http_send` – A function pointer `http_send` that will be called when the client |
| 224 | + * wishes to make a network request. The appropriate request should be executed by you, |
| 225 | + * and the the response provided to the response function pointer. This send |
| 226 | + * should be performed asynchronously. `http_send` should not block on |
| 227 | + * performing the request, and the response should be returned to the |
| 228 | + * `response` function pointer argument when the asynchronous work has |
| 229 | + * completed. The request parameter is only valid for the lifetime of the |
| 230 | + * `http_send` function and should not be accessed after returning from the |
| 231 | + * function. |
| 232 | + */ |
| 233 | +JuiceboxClient *juicebox_client_create(JuiceboxConfiguration *configuration, |
| 234 | + JuiceboxUnmanagedConfigurationArray previous_configurations, |
| 235 | + JuiceboxAuthTokenGetFn auth_token_get, |
| 236 | + JuiceboxHttpSendFn http_send); |
| 237 | + |
| 238 | +void juicebox_client_destroy(JuiceboxClient *client); |
| 239 | + |
| 240 | +const char *juicebox_sdk_version(void); |
| 241 | + |
| 242 | +JuiceboxConfiguration *juicebox_configuration_create(JuiceboxUnmanagedRealmArray realms, |
| 243 | + uint32_t register_threshold, |
| 244 | + uint32_t recover_threshold, |
| 245 | + JuiceboxPinHashingMode pin_hashing_mode); |
| 246 | + |
| 247 | +JuiceboxConfiguration *juicebox_configuration_create_from_json(const char *json); |
| 248 | + |
| 249 | +void juicebox_configuration_destroy(JuiceboxConfiguration *configuration); |
| 250 | + |
| 251 | +bool juicebox_configurations_are_equal(JuiceboxConfiguration *configuration1, |
| 252 | + JuiceboxConfiguration *configuration2); |
| 253 | + |
| 254 | +/** |
| 255 | + * Stores a new PIN-protected secret on the configured realms. |
| 256 | + * |
| 257 | + * # Note |
| 258 | + * |
| 259 | + * The provided secret must have a maximum length of 128-bytes. |
| 260 | + */ |
| 261 | +void juicebox_client_register(JuiceboxClient *client, |
| 262 | + const void *context, |
| 263 | + JuiceboxUnmanagedDataArray pin, |
| 264 | + JuiceboxUnmanagedDataArray secret, |
| 265 | + JuiceboxUnmanagedDataArray info, |
| 266 | + uint16_t num_guesses, |
| 267 | + void (*response)(const void *context, |
| 268 | + const JuiceboxRegisterError *error)); |
| 269 | + |
| 270 | +/** |
| 271 | + * Retrieves a PIN-protected secret from the configured realms, or falls |
| 272 | + * back to the previous realms if the current realms do not have a secret |
| 273 | + * registered. |
| 274 | + */ |
| 275 | +void juicebox_client_recover(JuiceboxClient *client, |
| 276 | + const void *context, |
| 277 | + JuiceboxUnmanagedDataArray pin, |
| 278 | + JuiceboxUnmanagedDataArray info, |
| 279 | + void (*response)(const void *context, |
| 280 | + JuiceboxUnmanagedDataArray secret, |
| 281 | + const JuiceboxRecoverError *error)); |
| 282 | + |
| 283 | +/** |
| 284 | + * Deletes the registered secret for this user, if any. |
| 285 | + */ |
| 286 | +void juicebox_client_delete(JuiceboxClient *client, |
| 287 | + const void *context, |
| 288 | + void (*response)(const void *context, const JuiceboxDeleteError *error)); |
| 289 | + |
| 290 | +JuiceboxAuthTokenGenerator *juicebox_auth_token_generator_create_from_json(const char *json); |
| 291 | + |
| 292 | +void juicebox_auth_token_generator_destroy(JuiceboxAuthTokenGenerator *generator); |
| 293 | + |
| 294 | +const JuiceboxAuthToken *juicebox_auth_token_generator_vend(JuiceboxAuthTokenGenerator *generator, |
| 295 | + JuiceboxAuthTokenParameters parameters); |
| 296 | + |
| 297 | +const JuiceboxAuthToken *juicebox_auth_token_create(const char *token_cstr); |
| 298 | + |
| 299 | +void juicebox_auth_token_destroy(JuiceboxAuthToken *token); |
| 300 | + |
| 301 | +void juicebox_auth_token_string(JuiceboxAuthToken *token, |
| 302 | + const void *context, |
| 303 | + JuiceboxAuthTokenStringCallbackFn callback); |
| 304 | + |
| 305 | +#endif /* JUICEBOX_FFI_H_ */ |
0 commit comments