1
0
Fork 0

added an teardownfunction to stop goroutines from the sites.

Dieser Commit ist enthalten in:
Sebastian Tobie 2021-01-16 19:06:47 +01:00
Ursprung 5f309aa8e2
Commit 5bf5ac8bb2
2 geänderte Dateien mit 22 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -31,6 +31,7 @@ type Server struct {
router *gin.Engine router *gin.Engine
config *toml.Tree config *toml.Tree
authhf auth.AuthenticationHandler authhf auth.AuthenticationHandler
sites []Site
} }
// StartServer starts the server as configured and sends the errormessage to the log. // StartServer starts the server as configured and sends the errormessage to the log.
@ -95,16 +96,21 @@ func (s *Server) UseAuthBackend(a auth.AuthenticationHandler) {
// Stop Shuts the Server down // Stop Shuts the Server down
func (s *Server) Stop(ctx context.Context) { func (s *Server) Stop(ctx context.Context) {
log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.") log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.")
for _, site := range s.sites {
site.Teardown()
}
} }
// Site is an Interface to abstract the modularized group of pages. // Site is an Interface to abstract the modularized group of pages.
// The Middleware must be able to detect multiple calls byy itself. Deduplication is not performed. // The Middleware must be able to detect multiple calls byy itself. Deduplication is not performed.
type Site interface { type Site interface {
Init(*gin.RouterGroup) Init(*gin.RouterGroup)
Teardown()
} }
// RegisterSite adds an site to the engine as its own grouo // RegisterSite adds an site to the engine as its own grouo
func (s *Server) RegisterSite(path string, site Site) { func (s *Server) RegisterSite(path string, site Site) {
site.Init(s.router.Group(path)) site.Init(s.router.Group(path))
s.sites = append(s.sites, site)
return return
} }

Datei anzeigen

@ -14,6 +14,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
"github.com/phuslu/log" "github.com/phuslu/log"
"go.sebtobie.de/httpserver"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"gopkg.in/dgrijalva/jwt-go.v3" "gopkg.in/dgrijalva/jwt-go.v3"
) )
@ -26,6 +27,7 @@ var (
Domain: "example.com", Domain: "example.com",
Cookiename: "ILOVECOOKIES", Cookiename: "ILOVECOOKIES",
} }
_ httpserver.Site = defaultsaml
) )
type metadata struct{} type metadata struct{}
@ -52,22 +54,22 @@ type SAML struct {
} }
// NewSAMLEndpoint creates an endpoint which handles SAML Requests. // NewSAMLEndpoint creates an endpoint which handles SAML Requests.
func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) { func NewSAMLEndpoint(config *toml.Tree) (s *SAML, err error) {
s = &(*defaultsaml)
s.config = config
log.Trace().Str("config", config.String()).Msg("config") log.Trace().Str("config", config.String()).Msg("config")
var key interface{} var key interface{}
var err error
var s SAML = *defaultsaml
s.config = config s.config = config
if err := config.Unmarshal(&s); err != nil { if err = config.Unmarshal(&s); err != nil {
log.Error().Err(err).Msg("Error while mapping config to struct") log.Error().Err(err).Msg("Error while mapping config to struct")
return nil, err return
} }
key, err = initcert(s.SPPrivatekey, func(key interface{}) bool { key, err = initcert(s.SPPrivatekey, func(key interface{}) bool {
_, ok := key.(*rsa.PrivateKey) _, ok := key.(*rsa.PrivateKey)
return ok return ok
}) })
if err != nil { if err != nil {
return nil, err return
} }
s.spprivatekey = key.(*rsa.PrivateKey) s.spprivatekey = key.(*rsa.PrivateKey)
@ -76,7 +78,7 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) {
return ok return ok
}) })
if err != nil { if err != nil {
return nil, err return
} }
s.sppublickey = key.(*x509.Certificate) s.sppublickey = key.(*x509.Certificate)
@ -85,7 +87,7 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) {
return ok return ok
}) })
if err != nil { if err != nil {
return nil, err return
} }
s.jwtprivatekey = key.(*rsa.PrivateKey) s.jwtprivatekey = key.(*rsa.PrivateKey)
s.sp = &saml.ServiceProvider{ s.sp = &saml.ServiceProvider{
@ -95,14 +97,14 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) {
var idpurl *url.URL var idpurl *url.URL
idpurl, err = url.ParseRequestURI(s.IDP) idpurl, err = url.ParseRequestURI(s.IDP)
if err != nil { if err != nil {
return nil, err return
} }
s.sp.IDPMetadata, err = samlsp.FetchMetadata(context.Background(), &s.HttpClient, *idpurl) s.sp.IDPMetadata, err = samlsp.FetchMetadata(context.Background(), &s.HttpClient, *idpurl)
if err != nil { if err != nil {
return nil, err return
} }
s.sp.AuthnNameIDFormat = saml.UnspecifiedNameIDFormat s.sp.AuthnNameIDFormat = saml.UnspecifiedNameIDFormat
return &s, nil return
} }
// Init initalizes the routes // Init initalizes the routes
@ -123,6 +125,9 @@ func (s *SAML) Init(router *gin.RouterGroup) {
router.POST("/acs", s.acsHF) router.POST("/acs", s.acsHF)
} }
// Teardown is to satisfy the httpserver.Site interface.
func (s *SAML) Teardown() {}
func (s *SAML) metadataHF(c *gin.Context) { func (s *SAML) metadataHF(c *gin.Context) {
m := s.sp.Metadata() m := s.sp.Metadata()
log.Debug().Time("Validuntil", m.ValidUntil).Msg("SP MEtadata") log.Debug().Time("Validuntil", m.ValidUntil).Msg("SP MEtadata")