mox/vendor/github.com/mjl-/bstore
Mechiel Lukkien 0f8bf2f220
replace http basic auth for web interfaces with session cookie & csrf-based auth
the http basic auth we had was very simple to reason about, and to implement.
but it has a major downside:

there is no way to logout, browsers keep sending credentials. ideally, browsers
themselves would show a button to stop sending credentials.

a related downside: the http auth mechanism doesn't indicate for which server
paths the credentials are.

another downside: the original password is sent to the server with each
request. though sending original passwords to web servers seems to be
considered normal.

our new approach uses session cookies, along with csrf values when we can. the
sessions are server-side managed, automatically extended on each use. this
makes it easy to invalidate sessions and keeps the frontend simpler (than with
long- vs short-term sessions and refreshing). the cookies are httponly,
samesite=strict, scoped to the path of the web interface. cookies are set
"secure" when set over https. the cookie is set by a successful call to Login.
a call to Logout invalidates a session. changing a password invalidates all
sessions for a user, but keeps the session with which the password was changed
alive. the csrf value is also random, and associated with the session cookie.
the csrf must be sent as header for api calls, or as parameter for direct form
posts (where we cannot set a custom header). rest-like calls made directly by
the browser, e.g. for images, don't have a csrf protection. the csrf value is
returned by the Login api call and stored in localstorage.

api calls without credentials return code "user:noAuth", and with bad
credentials return "user:badAuth". the api client recognizes this and triggers
a login. after a login, all auth-failed api calls are automatically retried.
only for "user:badAuth" is an error message displayed in the login form (e.g.
session expired).

in an ideal world, browsers would take care of most session management. a
server would indicate authentication is needed (like http basic auth), and the
browsers uses trusted ui to request credentials for the server & path. the
browser could use safer mechanism than sending original passwords to the
server, such as scram, along with a standard way to create sessions.  for now,
web developers have to do authentication themselves: from showing the login
prompt, ensuring the right session/csrf cookies/localstorage/headers/etc are
sent with each request.

webauthn is a newer way to do authentication, perhaps we'll implement it in the
future. though hardware tokens aren't an attractive option for many users, and
it may be overkill as long as we still do old-fashioned authentication in smtp
& imap where passwords can be sent to the server.

for issue #58
2024-01-05 10:48:42 +01:00
..
.gitignore update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
default.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
doc.go add webmail 2023-08-07 21:57:03 +02:00
equal.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
exec.go add webmail 2023-08-07 21:57:03 +02:00
export.go replace http basic auth for web interfaces with session cookie & csrf-based auth 2024-01-05 10:48:42 +01:00
format.md update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
gendoc.sh mox! 2023-01-30 14:27:06 +01:00
keys.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
LICENSE mox! 2023-01-30 14:27:06 +01:00
Makefile update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
nonzero.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
pack.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
parse.go update to latest bstore, with fix for a bug that was triggered by an upcoming commit 2023-11-20 11:01:15 +01:00
plan.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
query.go update to latest bstore, with fix for a bug that was triggered by an upcoming commit 2023-11-20 11:01:15 +01:00
README.md update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
register.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
stats.go mox! 2023-01-30 14:27:06 +01:00
store.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00
tags.go mox! 2023-01-30 14:27:06 +01:00
tx.go update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations. 2023-05-22 14:40:36 +02:00

Bstore is a database library for storing and quering Go values.

Bstore is designed as a small, pure Go library that still provides most of the common data consistency requirements for modest database use cases. Bstore aims to make basic use of cgo-based libraries, such as sqlite, unnecessary.

See https://pkg.go.dev/github.com/mjl-/bstore for features, examples and full documentation.

MIT-licensed

FAQ - Frequently Asked Questions

Is bstore an ORM?

No. The API for bstore may look like an ORM. But instead of mapping bstore "queries" (function calls) to an SQL query string, bstore executes them directly without converting to a query language, storing the data itself.

How does bstore store its data?

A bstore database is a single-file BoltDB database. BoltDB provides ACID properties. Bstore uses a BoltDB "bucket" (key/value store) for each Go type stored, with multiple subbuckets: one for type definitions, one for the actual data, and one bucket per index. BoltDB stores data in a B+tree. See format.md for details.

How does bstore compare to sqlite?

Sqlite is a great library, but Go applications that require cgo are hard to cross-compile. With bstore, cross-compiling to most Go-supported platforms stays trivial (though not plan9, unfortunately). Although bstore is much more limited in so many aspects than sqlite, bstore also offers some advantages as well. Some points of comparison:

  • Cross-compilation and reproducibility: Trivial with bstore due to pure Go, much harder with sqlite because of cgo.
  • Code complexity: low with bstore (7k lines including comments/docs), high with sqlite.
  • Query language: mostly-type-checked function calls in bstore, free-form query strings only checked at runtime with sqlite.
  • Functionality: very limited with bstore, much more full-featured with sqlite.
  • Schema management: mostly automatic based on Go type definitions in bstore, manual with ALTER statements in sqlite.
  • Types and packing/parsing: automatic/transparent in bstore based on Go types (including maps, slices, structs and custom MarshalBinary encoding), versus manual scanning and parameter passing with sqlite with limited set of SQL types.
  • Performance: low to good performance with bstore, high performance with sqlite.
  • Database files: single file with bstore, several files with sqlite (due to WAL or journal files).
  • Test coverage: decent coverage but limited real-world for bstore, versus extremely thoroughly tested and with enormous real-world use.