2020-07-10 18:07:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
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/gorilla/sessions"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and initialize HTTP session service provider
|
|
|
|
cookieStore := sessions.NewCookieStore(
|
|
|
|
[]byte(conf.HTTP.CookieAuthenticationKey),
|
|
|
|
[]byte(conf.HTTP.CookieEncryptionKey),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Define default cookie options
|
|
|
|
cookieStore.Options = &sessions.Options{
|
|
|
|
Path: "/",
|
|
|
|
HttpOnly: true,
|
|
|
|
MaxAge: conf.HTTP.CookieMaxAge,
|
|
|
|
SameSite: http.SameSiteStrictMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctn.Provide(
|
|
|
|
session.ServiceName,
|
|
|
|
gorilla.ServiceProvider("daddy", cookieStore),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-07-16 09:28:27 +02:00
|
|
|
ctn.Provide(orm.ServiceName, orm.ServiceProvider("postgres", conf.Database.DSN, conf.Debug))
|
2020-07-13 09:20:14 +02:00
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
return ctn, nil
|
|
|
|
}
|