summaryrefslogtreecommitdiff
path: root/internal/middlewares/util.go
blob: 7a7eb39e49da3cf6a4b5e596a7f31206ce5dc0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package middlewares

import (
	"crypto/x509"

	"github.com/valyala/fasthttp"

	"github.com/authelia/authelia/v4/internal/authentication"
	"github.com/authelia/authelia/v4/internal/authorization"
	"github.com/authelia/authelia/v4/internal/clock"
	"github.com/authelia/authelia/v4/internal/configuration/schema"
	"github.com/authelia/authelia/v4/internal/expression"
	"github.com/authelia/authelia/v4/internal/metrics"
	"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/random"
	"github.com/authelia/authelia/v4/internal/regulation"
	"github.com/authelia/authelia/v4/internal/session"
	"github.com/authelia/authelia/v4/internal/storage"
	"github.com/authelia/authelia/v4/internal/templates"
	"github.com/authelia/authelia/v4/internal/totp"
	"github.com/authelia/authelia/v4/internal/webauthn"
)

// SetContentTypeApplicationJSON sets the Content-Type header to `application/json; charset=utf-8`.
func SetContentTypeApplicationJSON(ctx *fasthttp.RequestCtx) {
	ctx.SetContentTypeBytes(contentTypeApplicationJSON)
}

// SetContentTypeTextPlain sets the Content-Type header to `text/plain; charset=utf-8`.
func SetContentTypeTextPlain(ctx *fasthttp.RequestCtx) {
	ctx.SetContentTypeBytes(contentTypeTextPlain)
}

// NewProviders provisions all providers based on the configuration provided.
func NewProviders(config *schema.Configuration, caCertPool *x509.CertPool) (providers Providers, warns, errs []error) {
	providers.Random = &random.Cryptographical{}
	providers.StorageProvider = storage.NewProvider(config, caCertPool)
	providers.Authorizer = authorization.NewAuthorizer(config)
	providers.NTP = ntp.NewProvider(&config.NTP)
	providers.PasswordPolicy = NewPasswordPolicyProvider(config.PasswordPolicy)
	providers.Regulator = regulation.NewRegulator(config.Regulation, providers.StorageProvider, clock.New())
	providers.SessionProvider = session.NewProvider(config.Session, caCertPool)
	providers.TOTP = totp.NewTimeBasedProvider(config.TOTP)
	providers.UserAttributeResolver = expression.NewUserAttributes(config)

	var err error

	switch {
	case config.AuthenticationBackend.File != nil:
		providers.UserProvider = authentication.NewFileUserProvider(config.AuthenticationBackend.File)
	case config.AuthenticationBackend.LDAP != nil:
		providers.UserProvider = authentication.NewLDAPUserProvider(config.AuthenticationBackend, caCertPool)
	}

	if providers.Templates, err = templates.New(templates.Config{EmailTemplatesPath: config.Notifier.TemplatePath}); err != nil {
		errs = append(errs, err)
	}

	if providers.MetaDataService, err = webauthn.NewMetaDataProvider(config, providers.StorageProvider); err != nil {
		errs = append(errs, err)
	}

	switch {
	case config.Notifier.SMTP != nil:
		providers.Notifier = notification.NewSMTPNotifier(config.Notifier.SMTP, caCertPool)
	case config.Notifier.FileSystem != nil:
		providers.Notifier = notification.NewFileNotifier(*config.Notifier.FileSystem)
	}

	providers.OpenIDConnect = oidc.NewOpenIDConnectProvider(config, providers.StorageProvider, providers.Templates)

	if config.Telemetry.Metrics.Enabled {
		providers.Metrics = metrics.NewPrometheus()
	}

	return providers, warns, errs
}