2020-07-10 18:07:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2020-07-17 09:39:37 +02:00
|
|
|
"time"
|
|
|
|
|
2020-10-05 14:16:25 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/mail"
|
2020-09-04 10:10:32 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/model"
|
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/voter"
|
|
|
|
|
2020-07-17 09:39:37 +02:00
|
|
|
"github.com/wader/gormstore"
|
2020-07-10 18:07:41 +02:00
|
|
|
|
2020-08-13 10:29:52 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/auth"
|
2020-07-16 09:28:27 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/orm"
|
2020-07-13 09:20:14 +02:00
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/config"
|
|
|
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/service"
|
|
|
|
"gitlab.com/wpetit/goweb/service/build"
|
|
|
|
"gitlab.com/wpetit/goweb/service/session"
|
|
|
|
"gitlab.com/wpetit/goweb/session/gorilla"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getServiceContainer(ctx context.Context, conf *config.Config) (*service.Container, error) {
|
|
|
|
// Initialize and configure service container
|
|
|
|
ctn := service.NewContainer()
|
|
|
|
|
|
|
|
ctn.Provide(build.ServiceName, build.ServiceProvider(ProjectVersion, GitRef, BuildDate))
|
|
|
|
|
|
|
|
// Generate random cookie authentication key if none is set
|
|
|
|
if conf.HTTP.CookieAuthenticationKey == "" {
|
|
|
|
logger.Info(ctx, "could not find cookie authentication key. generating one...")
|
|
|
|
|
|
|
|
cookieAuthenticationKey, err := gorilla.GenerateRandomBytes(64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not generate cookie authentication key")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.HTTP.CookieAuthenticationKey = string(cookieAuthenticationKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate random cookie encryption key if none is set
|
|
|
|
if conf.HTTP.CookieEncryptionKey == "" {
|
|
|
|
logger.Info(ctx, "could not find cookie encryption key. generating one...")
|
|
|
|
|
|
|
|
cookieEncryptionKey, err := gorilla.GenerateRandomBytes(32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not generate cookie encryption key")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.HTTP.CookieEncryptionKey = string(cookieEncryptionKey)
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:39:37 +02:00
|
|
|
ctn.Provide(orm.ServiceName, orm.ServiceProvider("postgres", conf.Database.DSN, conf.Debug))
|
|
|
|
|
|
|
|
orm, err := orm.From(ctn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
// Create and initialize HTTP session service provider
|
2020-07-17 09:39:37 +02:00
|
|
|
sessionStore := gormstore.NewOptions(
|
|
|
|
orm.DB(),
|
|
|
|
gormstore.Options{
|
|
|
|
TableName: "sessions",
|
|
|
|
SkipCreateTable: false,
|
|
|
|
},
|
2020-07-10 18:07:41 +02:00
|
|
|
[]byte(conf.HTTP.CookieAuthenticationKey),
|
|
|
|
[]byte(conf.HTTP.CookieEncryptionKey),
|
|
|
|
)
|
|
|
|
|
2020-07-17 09:39:37 +02:00
|
|
|
quit := make(chan struct{})
|
|
|
|
go sessionStore.PeriodicCleanup(1*time.Hour, quit)
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
// Define default cookie options
|
2020-07-17 09:39:37 +02:00
|
|
|
sessionStore.SessionOpts.Path = "/"
|
|
|
|
sessionStore.SessionOpts.HttpOnly = true
|
2020-10-13 14:59:25 +02:00
|
|
|
sessionStore.SessionOpts.Secure = conf.HTTP.CookieSecure
|
2020-07-17 09:39:37 +02:00
|
|
|
sessionStore.SessionOpts.MaxAge = conf.HTTP.CookieMaxAge
|
2020-10-13 14:59:25 +02:00
|
|
|
sessionStore.SessionOpts.SameSite = http.SameSiteLaxMode
|
2020-07-10 18:07:41 +02:00
|
|
|
|
|
|
|
ctn.Provide(
|
|
|
|
session.ServiceName,
|
2020-07-17 09:39:37 +02:00
|
|
|
gorilla.ServiceProvider("daddy", sessionStore),
|
2020-07-10 18:07:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create and expose config service provider
|
|
|
|
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
|
|
|
|
|
|
|
|
provider, err := oidc.NewProvider(ctx, conf.OIDC.IssuerURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create oidc provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctn.Provide(oidc.ServiceName, oidc.ServiceProvider(
|
|
|
|
oidc.WithCredentials(conf.OIDC.ClientID, conf.OIDC.ClientSecret),
|
|
|
|
oidc.WithProvider(provider),
|
|
|
|
oidc.WithScopes("email", "openid"),
|
|
|
|
))
|
|
|
|
|
2020-08-13 10:29:52 +02:00
|
|
|
ctn.Provide(auth.ServiceName, auth.ServiceProvider(conf.Auth.Rules))
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
ctn.Provide(voter.ServiceName, voter.ServiceProvider(
|
|
|
|
voter.StrategyUnanimous,
|
|
|
|
model.NewDecisionSupportFileVoter(),
|
|
|
|
model.NewWorkgroupVoter(),
|
|
|
|
))
|
|
|
|
|
2020-10-05 14:16:25 +02:00
|
|
|
ctn.Provide(mail.ServiceName, mail.ServiceProvider(
|
|
|
|
mail.WithServer(conf.SMTP.Host, conf.SMTP.Port),
|
|
|
|
mail.WithCredentials(conf.SMTP.User, conf.SMTP.Password),
|
|
|
|
mail.WithTLS(conf.SMTP.UseStartTLS, conf.SMTP.InsecureSkipVerify),
|
|
|
|
))
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
return ctn, nil
|
|
|
|
}
|