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
This commit is contained in:
Mechiel Lukkien
2024-01-04 13:10:48 +01:00
parent c930a400be
commit 0f8bf2f220
50 changed files with 3560 additions and 832 deletions

View File

@ -34,9 +34,22 @@ type JSON struct {
// HandlerOpts are options for creating a new handler.
type HandlerOpts struct {
Collector Collector // Holds functions for collecting metrics about function calls and other incoming HTTP requests. May be nil.
LaxParameterParsing bool // If enabled, incoming sherpa function calls will ignore unrecognized fields in struct parameters, instead of failing.
AdjustFunctionNames string // If empty, only the first character of function names are lower cased. For "lowerWord", the first string of capitals is lowercased, for "none", the function name is left as is.
// Holds functions for collecting metrics about function calls and other incoming
// HTTP requests. May be nil.
Collector Collector
// If enabled, incoming sherpa function calls will ignore unrecognized fields in
// struct parameters, instead of failing.
LaxParameterParsing bool
// If empty, only the first character of function names are lower cased. For
// "lowerWord", the first string of capitals is lowercased, for "none", the
// function name is left as is.
AdjustFunctionNames string
// Don't send any CORS headers, and respond to OPTIONS requests with 405 "bad
// method".
NoCORS bool
}
// Raw signals a raw JSON response.
@ -463,13 +476,16 @@ func validCallback(cb string) bool {
// - sherpa.js, a small stand-alone client JavaScript library that makes it trivial to start using this API from a browser.
// - functionName, for function invocations on this API.
//
// HTTP response will have CORS-headers set, and support the OPTIONS HTTP method.
// HTTP response will have CORS-headers set, and support the OPTIONS HTTP method,
// unless the NoCORS option was set.
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
hdr := w.Header()
hdr.Set("Access-Control-Allow-Origin", "*")
hdr.Set("Access-Control-Allow-Methods", "GET, POST")
hdr.Set("Access-Control-Allow-Headers", "Content-Type")
if !h.opts.NoCORS {
hdr.Set("Access-Control-Allow-Origin", "*")
hdr.Set("Access-Control-Allow-Methods", "GET, POST")
hdr.Set("Access-Control-Allow-Headers", "Content-Type")
}
collector := h.opts.Collector
@ -489,10 +505,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
case r.URL.Path == "sherpa.json":
switch r.Method {
case "OPTIONS":
switch {
case !h.opts.NoCORS && r.Method == "OPTIONS":
w.WriteHeader(204)
case "GET":
case r.Method == "GET":
collector.JSON()
hdr.Set("Content-Type", "application/json; charset=utf-8")
hdr.Set("Cache-Control", "no-cache")
@ -531,11 +547,11 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
default:
name := r.URL.Path
fn, ok := h.functions[name]
switch r.Method {
case "OPTIONS":
switch {
case !h.opts.NoCORS && r.Method == "OPTIONS":
w.WriteHeader(204)
case "POST":
case r.Method == "POST":
hdr.Set("Cache-Control", "no-store")
if !ok {
@ -593,7 +609,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
respondJSON(w, 200, v)
}
case "GET":
case r.Method == "GET":
hdr.Set("Cache-Control", "no-store")
jsonp := false