2021-11-23 09:45:38 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2022-10-22 05:41:27 +00:00
|
|
|
"crypto/x509"
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
2022-06-14 07:20:13 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/metrics"
|
2021-11-25 01:56:58 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/v4/internal/notification"
|
|
|
|
"github.com/authelia/authelia/v4/internal/ntp"
|
|
|
|
"github.com/authelia/authelia/v4/internal/oidc"
|
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
2022-07-18 00:56:09 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2021-12-01 12:11:29 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/totp"
|
2021-11-25 01:56:58 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
func getStorageProvider() (provider storage.Provider) {
|
2022-10-22 05:41:27 +00:00
|
|
|
switch {
|
|
|
|
case config.Storage.Local == nil:
|
|
|
|
return getStorageProviderWithPool(nil)
|
|
|
|
default:
|
|
|
|
caCertPool, _, _ := utils.NewX509CertPool(config.CertificatesDirectory)
|
|
|
|
|
|
|
|
return getStorageProviderWithPool(caCertPool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStorageProviderWithPool(caCertPool *x509.CertPool) (provider storage.Provider) {
|
2021-11-23 09:45:38 +00:00
|
|
|
switch {
|
|
|
|
case config.Storage.PostgreSQL != nil:
|
2022-10-22 05:41:27 +00:00
|
|
|
return storage.NewPostgreSQLProvider(config, caCertPool)
|
2021-11-23 09:45:38 +00:00
|
|
|
case config.Storage.MySQL != nil:
|
2022-10-22 05:41:27 +00:00
|
|
|
return storage.NewMySQLProvider(config, caCertPool)
|
2021-11-23 09:45:38 +00:00
|
|
|
case config.Storage.Local != nil:
|
2021-12-01 12:11:29 +00:00
|
|
|
return storage.NewSQLiteProvider(config)
|
2021-11-23 09:45:38 +00:00
|
|
|
default:
|
2021-11-25 01:56:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProviders() (providers middlewares.Providers, warnings []error, errors []error) {
|
|
|
|
// TODO: Adjust this so the CertPool can be used like a provider.
|
2022-10-22 05:41:27 +00:00
|
|
|
caCertPool, warnings, errors := utils.NewX509CertPool(config.CertificatesDirectory)
|
2021-11-25 01:56:58 +00:00
|
|
|
if len(warnings) != 0 || len(errors) != 0 {
|
|
|
|
return providers, warnings, errors
|
|
|
|
}
|
|
|
|
|
2022-10-22 05:41:27 +00:00
|
|
|
storageProvider := getStorageProviderWithPool(caCertPool)
|
2021-11-25 01:56:58 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
userProvider authentication.UserProvider
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.AuthenticationBackend.File != nil:
|
|
|
|
userProvider = authentication.NewFileUserProvider(config.AuthenticationBackend.File)
|
|
|
|
case config.AuthenticationBackend.LDAP != nil:
|
2022-10-22 05:41:27 +00:00
|
|
|
userProvider = authentication.NewLDAPUserProvider(config.AuthenticationBackend, caCertPool)
|
2021-11-25 01:56:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
templatesProvider, err := templates.New(templates.Config{EmailTemplatesPath: config.Notifier.TemplatePath})
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
var notifier notification.Notifier
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.Notifier.SMTP != nil:
|
2022-10-22 05:41:27 +00:00
|
|
|
notifier = notification.NewSMTPNotifier(config.Notifier.SMTP, caCertPool, templatesProvider)
|
2021-11-25 01:56:58 +00:00
|
|
|
case config.Notifier.FileSystem != nil:
|
|
|
|
notifier = notification.NewFileNotifier(*config.Notifier.FileSystem)
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
ntpProvider := ntp.NewProvider(&config.NTP)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
clock := utils.RealClock{}
|
|
|
|
authorizer := authorization.NewAuthorizer(config)
|
2022-10-22 05:41:27 +00:00
|
|
|
sessionProvider := session.NewProvider(config.Session, caCertPool)
|
2021-11-25 01:56:58 +00:00
|
|
|
regulator := regulation.NewRegulator(config.Regulation, storageProvider, clock)
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
oidcProvider, err := oidc.NewOpenIDConnectProvider(config.IdentityProviders.OIDC, storageProvider)
|
2021-11-25 01:56:58 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
totpProvider := totp.NewTimeBasedProvider(config.TOTP)
|
|
|
|
|
2022-04-15 09:30:51 +00:00
|
|
|
ppolicyProvider := middlewares.NewPasswordPolicyProvider(config.PasswordPolicy)
|
2022-04-03 00:48:26 +00:00
|
|
|
|
2022-06-14 07:20:13 +00:00
|
|
|
var metricsProvider metrics.Provider
|
|
|
|
if config.Telemetry.Metrics.Enabled {
|
|
|
|
metricsProvider = metrics.NewPrometheus()
|
|
|
|
}
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
return middlewares.Providers{
|
|
|
|
Authorizer: authorizer,
|
|
|
|
UserProvider: userProvider,
|
|
|
|
Regulator: regulator,
|
|
|
|
OpenIDConnect: oidcProvider,
|
|
|
|
StorageProvider: storageProvider,
|
2022-06-14 07:20:13 +00:00
|
|
|
Metrics: metricsProvider,
|
2021-11-25 01:56:58 +00:00
|
|
|
NTP: ntpProvider,
|
|
|
|
Notifier: notifier,
|
|
|
|
SessionProvider: sessionProvider,
|
2022-07-18 00:56:09 +00:00
|
|
|
Templates: templatesProvider,
|
2021-12-01 12:11:29 +00:00
|
|
|
TOTP: totpProvider,
|
2022-04-15 09:30:51 +00:00
|
|
|
PasswordPolicy: ppolicyProvider,
|
2021-11-25 01:56:58 +00:00
|
|
|
}, warnings, errors
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|