linstrom/server/server.go
mstar 391d8b1b48 Access tokens, server, moving things
- Added placeholder funcs for access tokens
- Moved an error definition and added another constant
- Changed (passkey) auth fail to return a json error for now
- TODO: Change that into checking for a provided access token before
  failing
2024-10-15 20:41:23 +02:00

58 lines
1.5 KiB
Go

package server
import (
"fmt"
"io/fs"
"net/http"
"github.com/mstarongithub/passkey"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/goutils/other"
"gitlab.com/mstarongitlab/linstrom/storage"
)
type Server struct {
store *storage.Storage
router http.Handler
}
func NewServer(store *storage.Storage, pkey *passkey.Passkey, reactiveFS, staticFS fs.FS) *Server {
handler := buildRootHandler(pkey, reactiveFS, staticFS)
handler = ChainMiddlewares(handler, LoggingMiddleware, ContextValsMiddleware(map[any]any{
ContextKeyStorage: store,
}))
return &Server{
store: store,
router: handler,
}
}
func buildRootHandler(pkey *passkey.Passkey, reactiveFS, staticFS fs.FS) http.Handler {
mux := http.NewServeMux()
pkey.MountRoutes(mux, "/webauthn/")
mux.Handle("/", setupFrontendRouter(reactiveFS, staticFS))
mux.Handle("/pk/", http.StripPrefix("/pk", http.FileServer(http.Dir("pk-auth"))))
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, true) })
mux.Handle(
"/authonly/",
pkey.Auth(
ContextKeyPasskeyUsername,
nil,
func(w http.ResponseWriter, r *http.Request) {
other.HttpErr(
w,
HttpErrIdNotAuthenticated,
"Not authenticated",
http.StatusUnauthorized,
)
},
)(ChainMiddlewares(setupTestEndpoints(), passkeyIdToAccountIdTransformerMiddleware)),
)
return mux
}
func (s *Server) Start(addr string) error {
log.Info().Str("addr", addr).Msg("Starting server")
return http.ListenAndServe(addr, s.router)
}