r/rust • • 4h ago

🙋 seeking help & advice Login and key encryption solution for and offline+online application

I’m building a desktop application primarily using Rust and Tauri. The application requires an internet connection initially to fetch data from a server. However, once the initial data has been downloaded, it should work entirely offline, without requiring an active internet connection. Any changes made locally can then be synced back to the server when connectivity is restored.
I want to implement authentication and encryption to make the application more secure, but I’m having trouble figuring out the right architecture for this.
Here’s the approach I was initially considering:

Online authentication

  1. The user enters their username and password.
  2. The Rust backend sends the credentials to the server for verification.
  3. Upon successful authentication, the server returns the user’s profile along with access and refresh tokens.
  4. I use Tauri Stronghold to create a separate vault for the user and securely store the authentication tokens and database encryption key.
  5. The vault itself is protected using a key derived from the user’s password and a randomly generated salt, using Argon2id.

Offline authentication

  1. After the user’s first successful online login, their vault and local profile are already stored on the machine.
  2. When the user subsequently opens the application without an internet connection, they enter their password.
  3. The application derives the vault key again using Argon2id.
  4. If the derived key successfully unlocks the vault, the user can access their locally stored database and continue working offline.

This is the general architecture I have in mind, but I’m not entirely confident about it. It feels somewhat clunky, and I’m wondering if there is a better or more established approach to handling this.

My questions are:

  • Is there a better architecture or authentication flow for an application that needs to support both online and offline authentication?
  • Are there any established libraries, frameworks, or architectural patterns in Rust/Tauri that could simplify this implementation?

I’d appreciate any suggestions, particularly around security best practices and how to structure the authentication, key management, and offline access flows.

0 Upvotes

4 comments sorted by

1

u/Immediate-Tailor-175 4h ago

That's basically how I'd sketch it out too. The password-derived vault key approach is pretty standard for offline-first stuff, so I wouldn't call it clunky. One thing I'd double-check is how you're handling the refresh token expiry while offline, that's where these setups tend to get annoying. If their token dies mid-offline session, you'll need a clean fallback that doesn't lock them out of local data until they reconnect.

1

u/BraveCake_ 2h ago

Yeah that token expiry issue was the one I encountered when i actually created a small poc around it. If the tokens expires, the application isn’t able to sync the data back to the server even with network connectivity. The only way I was able to get it working was for the user to log out and log back in the application with active internet access which sends a new request to the server and new tokens gets generated and then the stored data in the device gets synced back to server.

1

u/muffinluff 4h ago

I would suggest integrating into tpm for the local encryption part as that reduces an attack vector. This way you can use a pin or even biometric authentication to open the local vault

1

u/ElvishJerricco 2h ago

I think you should seriously consider whether encryption actually makes the application more secure. Ask yourself what the threat model you're protecting against is, and whether that threat model has any obvious ways around this. For a threat model like the device being stolen, generally the user needs full disk encryption, so application level encryption doesn't add anything. Generally application level encryption is about software threat models. But it's pretty unusual that this actually works, at least on mainstream desktop operating systems. If malicious software on the system has access to the ciphertext files, it's pretty unusual for that software to have no way to acquire the encryption key. It can do that by just waiting for the user to enter the passphrase and either keylogging or presenting a convincing fake prompt for it, for instance. Or if there's a keyring involved, it can just access that keyring. Even with something like a yubikey, it can just race the real app to ask the yubikey to decrypt something at the right time, and the user will just see the blinking light and assume it's the app they meant to use it for, not the malicious one winning the race.

I'm not trying to say all those problems with the software threat model aren't solvable, but when you really think it through, pretty much all the attack vectors lead you to the same conclusion: You fix it with sandboxing. And A) no mainstream desktop OS is sandboxing everything by default (well, macOS does to some extent but not the extent you really need like iOS does), so you're probably not actually going to stand a chance against the software threat model, and B) sandboxing just makes the encryption redundant in the first place; if everything is sandboxed, nothing else has access to the ciphertext, so it might as well be plaintext. So really the answer, whether you want application level encryption or not, is that you need everything else to be sandboxed first.

There's obviously use cases that don't fit this general philosophy, like if there's a good reason the user wouldn't be using full disk encryption or if you've actually got some scheme to keep the key properly exclusive to the intended app somehow. But my point is that this is surprisingly rare, and I think the benefit of leaving things plaintext often outweighs the fairly niche benefit of having encryption in those use cases.