lavender/server/auth.go

133 lines
3.4 KiB
Go
Raw Normal View History

2024-02-07 01:18:17 +00:00
package server
import (
2024-09-13 15:31:40 +01:00
"database/sql"
2024-05-31 13:51:44 +01:00
"errors"
2024-09-13 15:31:40 +01:00
"github.com/1f349/lavender/auth"
2024-02-07 01:18:17 +00:00
"github.com/1f349/lavender/database"
2024-09-13 15:31:40 +01:00
"github.com/1f349/lavender/role"
2024-02-07 01:18:17 +00:00
"github.com/julienschmidt/httprouter"
"net/http"
"net/url"
"strings"
)
type UserHandler func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth)
type UserAuth struct {
2024-09-13 15:31:40 +01:00
Subject string
2024-10-06 21:30:39 +01:00
Factor auth.Factor
2024-09-13 15:31:40 +01:00
UserInfo auth.UserInfoFields
2024-02-07 01:18:17 +00:00
}
func (u UserAuth) IsGuest() bool { return u.Subject == "" }
2024-02-07 01:18:17 +00:00
2024-09-13 15:31:40 +01:00
func (u UserAuth) NextFlowUrl(origin *url.URL) *url.URL {
2024-10-06 21:30:39 +01:00
if u.Factor < auth.FactorAuthorized {
return PrepareRedirectUrl("/login", origin)
2024-09-13 15:31:40 +01:00
}
return nil
}
2024-05-31 13:51:44 +01:00
var ErrAuthHttpError = errors.New("auth http error")
2024-09-13 15:31:40 +01:00
func (h *httpServer) RequireAdminAuthentication(next UserHandler) httprouter.Handle {
2024-02-07 01:18:17 +00:00
return h.RequireAuthentication(func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth) {
2024-09-13 15:31:40 +01:00
var hasRole bool
if h.DbTx(rw, func(tx *database.Queries) (err error) {
2024-09-13 15:31:40 +01:00
err = tx.UserHasRole(req.Context(), database.UserHasRoleParams{
Role: role.LavenderAdmin,
Subject: auth.Subject,
})
switch {
case err == nil:
hasRole = true
case errors.Is(err, sql.ErrNoRows):
hasRole = false
err = nil
}
2024-02-07 01:18:17 +00:00
return
}) {
return
}
2024-09-13 15:31:40 +01:00
if !hasRole {
2024-02-07 01:18:17 +00:00
http.Error(rw, "403 Forbidden", http.StatusForbidden)
return
}
next(rw, req, params, auth)
})
}
2024-09-13 15:31:40 +01:00
func (h *httpServer) RequireAuthentication(next UserHandler) httprouter.Handle {
return h.OptionalAuthentication(false, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth) {
2024-02-07 01:18:17 +00:00
if auth.IsGuest() {
redirectUrl := PrepareRedirectUrl("/login", req.URL)
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
return
}
next(rw, req, params, auth)
})
}
func (h *httpServer) OptionalAuthentication(flowPart bool, next UserHandler) httprouter.Handle {
2024-02-07 01:18:17 +00:00
return func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
authData, err := h.internalAuthenticationHandler(rw, req)
2024-02-07 01:18:17 +00:00
if err != nil {
2024-05-31 13:51:44 +01:00
if !errors.Is(err, ErrAuthHttpError) {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
2024-02-07 01:18:17 +00:00
return
}
if n := authData.NextFlowUrl(req.URL); n != nil && !flowPart {
http.Redirect(rw, req, n.String(), http.StatusFound)
return
}
next(rw, req, params, authData)
2024-02-07 01:18:17 +00:00
}
}
2024-09-13 15:31:40 +01:00
func (h *httpServer) internalAuthenticationHandler(rw http.ResponseWriter, req *http.Request) (UserAuth, error) {
2024-05-31 13:51:44 +01:00
// Delete previous login data cookie
http.SetCookie(rw, &http.Cookie{
Name: "lavender-login-data",
Path: "/",
MaxAge: -1,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
var u UserAuth
2024-05-31 13:51:44 +01:00
err := h.readLoginAccessCookie(rw, req, &u)
if err != nil {
// not logged in
return UserAuth{}, nil
2024-02-07 01:18:17 +00:00
}
return u, nil
2024-02-07 01:18:17 +00:00
}
func PrepareRedirectUrl(targetPath string, origin *url.URL) *url.URL {
// find start of query parameters in target path
n := strings.IndexByte(targetPath, '?')
v := url.Values{}
// parse existing query parameters
if n != -1 {
q, err := url.ParseQuery(targetPath[n+1:])
if err != nil {
panic("PrepareRedirectUrl: invalid hardcoded target path query parameters")
}
v = q
targetPath = targetPath[:n]
}
// add path of origin as a new query parameter
orig := origin.Path
if origin.RawQuery != "" || origin.ForceQuery {
orig += "?" + origin.RawQuery
}
if orig != "" {
v.Set("redirect", orig)
}
return &url.URL{Path: targetPath, RawQuery: v.Encode()}
}