lavender/server/server.go

77 lines
1.7 KiB
Go
Raw Normal View History

2023-10-01 21:44:49 +01:00
package server
import (
"fmt"
2023-10-03 23:20:28 +01:00
"github.com/1f349/cache"
2023-10-01 21:44:49 +01:00
"github.com/1f349/lavender/issuer"
"github.com/MrMelon54/mjwt"
"github.com/julienschmidt/httprouter"
2023-10-08 15:24:59 +01:00
"log"
2023-10-01 21:44:49 +01:00
"net/http"
"time"
)
type HttpServer struct {
2023-10-08 15:24:59 +01:00
r *httprouter.Router
conf Conf
manager *issuer.Manager
signer mjwt.Signer
flowState *cache.Cache[string, flowStateData]
services map[string]struct{}
2023-10-03 23:20:28 +01:00
}
type flowStateData struct {
2023-10-04 14:51:38 +01:00
sso *issuer.WellKnownOIDC
targetOrigin string
2023-10-01 21:44:49 +01:00
}
2023-10-08 15:24:59 +01:00
func NewHttpServer(conf Conf, signer mjwt.Signer) *http.Server {
2023-10-01 21:44:49 +01:00
r := httprouter.New()
// remove last slash from baseUrl
{
2023-10-08 15:24:59 +01:00
l := len(conf.BaseUrl)
if conf.BaseUrl[l-1] == '/' {
conf.BaseUrl = conf.BaseUrl[:l-1]
}
}
2023-10-08 15:24:59 +01:00
manager, err := issuer.NewManager(conf.SsoServices)
if err != nil {
log.Fatal("[Lavender] Failed to create SSO service manager: ", err)
}
2023-10-04 14:51:38 +01:00
services := make(map[string]struct{})
2023-10-08 15:24:59 +01:00
for _, i := range conf.AllowedClients {
services[i.String()] = struct{}{}
2023-10-04 14:51:38 +01:00
}
2023-10-01 21:44:49 +01:00
hs := &HttpServer{
2023-10-08 15:24:59 +01:00
r: r,
conf: conf,
manager: manager,
signer: signer,
flowState: cache.New[string, flowStateData](),
services: services,
2023-10-01 21:44:49 +01:00
}
r.GET("/", func(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
rw.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(rw, "What is this?")
})
r.POST("/verify", hs.verifyHandler)
r.GET("/popup", hs.flowPopup)
r.POST("/popup", hs.flowPopupPost)
r.GET("/callback", hs.flowCallback)
return &http.Server{
2023-10-08 15:24:59 +01:00
Addr: conf.Listen,
2023-10-01 21:44:49 +01:00
Handler: r,
ReadTimeout: time.Minute,
ReadHeaderTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Minute,
MaxHeaderBytes: 2500,
}
}