tulip/server/auth.go

121 lines
3.1 KiB
Go
Raw Normal View History

2023-09-06 22:20:09 +01:00
package server
import (
2024-05-31 14:57:54 +01:00
"errors"
"github.com/1f349/tulip/database"
2024-03-11 12:39:52 +00:00
"github.com/1f349/tulip/database/types"
2023-09-06 22:20:09 +01:00
"github.com/julienschmidt/httprouter"
"net/http"
"net/url"
2023-09-24 18:24:16 +01:00
"strings"
2023-09-06 22:20:09 +01:00
)
type UserHandler func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth)
type UserAuth struct {
2024-05-31 14:57:54 +01:00
Subject string
NeedOtp bool
}
func (u UserAuth) NextFlowUrl(origin *url.URL) *url.URL {
if u.NeedOtp {
return PrepareRedirectUrl("/login/otp", origin)
}
return nil
2023-09-06 22:20:09 +01:00
}
func (u UserAuth) IsGuest() bool {
2024-05-31 14:57:54 +01:00
return u.Subject == ""
2023-09-06 22:20:09 +01:00
}
2024-05-31 14:57:54 +01:00
var ErrAuthHttpError = errors.New("auth http error")
func (h *HttpServer) RequireAdminAuthentication(next UserHandler) httprouter.Handle {
return h.RequireAuthentication(func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth) {
2024-03-11 12:39:52 +00:00
var role types.UserRole
if h.DbTx(rw, func(tx *database.Queries) (err error) {
2024-05-31 14:57:54 +01:00
role, err = tx.GetUserRole(req.Context(), auth.Subject)
return
}) {
return
}
2024-03-11 12:39:52 +00:00
if role != types.RoleAdmin {
http.Error(rw, "403 Forbidden", http.StatusForbidden)
return
}
next(rw, req, params, auth)
})
}
func (h *HttpServer) RequireAuthentication(next UserHandler) httprouter.Handle {
return h.OptionalAuthentication(false, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth) {
2023-09-06 22:20:09 +01:00
if auth.IsGuest() {
redirectUrl := PrepareRedirectUrl("/login", req.URL)
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
2023-09-06 22:20:09 +01:00
return
}
next(rw, req, params, auth)
})
}
func (h *HttpServer) OptionalAuthentication(flowPart bool, next UserHandler) httprouter.Handle {
2023-09-06 22:20:09 +01:00
return func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
2024-05-31 14:57:54 +01:00
authData, err := h.internalAuthenticationHandler(rw, req)
if err != nil {
if !errors.Is(err, ErrAuthHttpError) {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
2024-05-31 14:57:54 +01: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)
2023-09-06 22:20:09 +01:00
}
}
2024-05-31 14:57:54 +01:00
func (h *HttpServer) internalAuthenticationHandler(rw http.ResponseWriter, req *http.Request) (UserAuth, error) {
http.SetCookie(rw, &http.Cookie{
Name: "tulip-login-data",
Path: "/",
MaxAge: -1,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
var u UserAuth
err := h.readLoginAccessCookie(rw, req, &u)
if err != nil {
// not logged in
return UserAuth{}, nil
2023-09-06 22:20:09 +01:00
}
2024-05-31 14:57:54 +01:00
return u, nil
}
func PrepareRedirectUrl(targetPath string, origin *url.URL) *url.URL {
2023-09-24 18:24:16 +01:00
// find start of query parameters in target path
n := strings.IndexByte(targetPath, '?')
v := url.Values{}
2023-09-24 18:24:16 +01:00
// 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()}
2023-09-06 22:20:09 +01:00
}