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
config *toml.Tree
authhf auth.AuthenticationHandler
sites []Site
}
// 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
func (s *Server) Stop(ctx context.Context) {
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.
// The Middleware must be able to detect multiple calls byy itself. Deduplication is not performed.
type Site interface {
Init(*gin.RouterGroup)
Teardown()
}
// RegisterSite adds an site to the engine as its own grouo
func (s *Server) RegisterSite(path string, site Site) {
site.Init(s.router.Group(path))
s.sites = append(s.sites, site)
return
}

Datei anzeigen

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