hydra-passwordless/cmd/server/container.go

134 lines
4.0 KiB
Go
Raw Normal View History

2020-04-08 08:56:42 +02:00
package main
import (
"log"
"net/http"
"time"
2020-05-20 11:13:14 +02:00
"gitlab.com/wpetit/goweb/cqrs"
2020-04-08 08:56:42 +02:00
"gitlab.com/wpetit/goweb/template/html"
2020-05-20 11:13:14 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/command"
2020-04-08 08:56:42 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/config"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra"
2020-04-24 09:27:07 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/mail"
2020-05-20 11:13:14 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/query"
2020-04-08 08:56:42 +02:00
"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/service/template"
"gitlab.com/wpetit/goweb/session/gorilla"
)
func getServiceContainer(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 == "" {
log.Println("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 == "" {
log.Println("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-05-20 11:13:14 +02:00
// Generate random token signing key if none is set
if conf.HTTP.TokenSigningKey == "" {
log.Println("could not find token signing key. generating one...")
tokenSigningKey, err := gorilla.GenerateRandomBytes(64)
if err != nil {
return nil, errors.Wrap(err, "could not generate token signing key")
}
conf.HTTP.TokenSigningKey = string(tokenSigningKey)
}
// Generate random token encryption key if none is set
if conf.HTTP.TokenEncryptionKey == "" {
log.Println("could not find token encryption key. generating one...")
tokenEncryptionKey, err := gorilla.GenerateRandomBytes(32)
if err != nil {
return nil, errors.Wrap(err, "could not generate token encryption key")
}
conf.HTTP.TokenEncryptionKey = string(tokenEncryptionKey)
}
2020-04-08 08:56:42 +02:00
// 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("hydra-passwordless", cookieStore),
)
// Create and expose template service provider
// Create and expose template service provider
ctn.Provide(template.ServiceName, html.ServiceProvider(
conf.HTTP.TemplateDir,
))
// Create and expose config service provider
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
ctn.Provide(hydra.ServiceName, hydra.ServiceProvider(conf.Hydra.BaseURL, 30*time.Second))
2020-04-24 09:27:07 +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-05-20 11:13:14 +02:00
ctn.Provide(cqrs.ServiceName, cqrs.ServiceProvider())
bus, err := cqrs.From(ctn)
if err != nil {
return nil, err
}
bus.RegisterCommand(
cqrs.MatchCommandRequest(&command.SendConfirmationEmailRequest{}),
cqrs.CommandHandlerFunc(command.HandleSendConfirmationEmailRequest),
)
bus.RegisterQuery(
cqrs.MatchQueryRequest(&query.VerifyUserRequest{}),
cqrs.QueryHandlerFunc(query.HandleVerifyUserRequest),
)
2020-04-08 08:56:42 +02:00
return ctn, nil
}