Secrets.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef secrets_h | ||
| 2 | #define secrets_h | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | // Stored secrets (WiFi passphrase, MQTT password, login password/PIN) are | ||
| 6 | // write-only: responses carry hasSecret() flags and logs carry maskSecret(). | ||
| 7 | |||
| 8 | 7 | inline bool hasSecret(const char *s) { return s && s[0] != '\0'; } | |
| 9 | |||
| 10 | 3 | inline const char *maskSecret(const char *s) { return hasSecret(s) ? "<set>" : "<empty>"; } | |
| 11 | |||
| 12 | // Updates dest from an incoming value, truncating to size. Pass | ||
| 13 | // obj["key"].as<const char *>(), which is null when the key is absent. | ||
| 14 | // absent -> keep the stored secret. | ||
| 15 | // "" -> keep it too, unless identityChanged (a new SSID or username in | ||
| 16 | // the same request), then clear it. UIs older than write-only | ||
| 17 | // secrets send "" for a field the device no longer fills in. | ||
| 18 | // value -> replace it. | ||
| 19 | // Returns true when dest changed. | ||
| 20 | 11 | inline bool applySecret(const char *incoming, char *dest, size_t size, bool identityChanged = false) { | |
| 21 | 11 | if(!incoming || size == 0) return false; | |
| 22 | 9 | if(incoming[0] == '\0' && !identityChanged) return false; | |
| 23 | 8 | size_t len = strlen(incoming); | |
| 24 | 8 | if(len >= size) len = size - 1; | |
| 25 | 8 | if(strncmp(dest, incoming, len) == 0 && dest[len] == '\0') return false; | |
| 26 | 5 | memcpy(dest, incoming, len); | |
| 27 | 5 | dest[len] = '\0'; | |
| 28 | 5 | return true; | |
| 29 | } | ||
| 30 | #endif | ||
| 31 |