2022-02-14 17:22:30 +01:00
|
|
|
package login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2025-01-06 10:47:46 +01:00
|
|
|
"net/url"
|
2022-05-02 17:26:54 +02:00
|
|
|
"strings"
|
2022-08-16 07:04:36 +02:00
|
|
|
"time"
|
2022-02-14 17:22:30 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/csrf"
|
|
|
|
"github.com/gorilla/mux"
|
2024-01-16 11:28:56 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
http_utils "github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/http/middleware"
|
|
|
|
_ "github.com/zitadel/zitadel/internal/api/ui/login/statik"
|
|
|
|
auth_repository "github.com/zitadel/zitadel/internal/auth/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/auth/repository/eventsourcing"
|
2025-01-06 10:47:46 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/cache"
|
|
|
|
"github.com/zitadel/zitadel/internal/cache/connector"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/form"
|
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/static"
|
2022-02-14 17:22:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Login struct {
|
2022-04-25 10:01:17 +02:00
|
|
|
endpoint string
|
|
|
|
router http.Handler
|
|
|
|
renderer *Renderer
|
|
|
|
parser *form.Parser
|
|
|
|
command *command.Commands
|
|
|
|
query *query.Queries
|
|
|
|
staticStorage static.Storage
|
2022-02-14 17:22:30 +01:00
|
|
|
authRepo auth_repository.Repository
|
2022-04-25 11:16:36 +02:00
|
|
|
externalSecure bool
|
2022-03-14 07:55:09 +01:00
|
|
|
consolePath string
|
2022-04-25 10:01:17 +02:00
|
|
|
oidcAuthCallbackURL func(context.Context, string) string
|
2022-09-12 17:18:08 +01:00
|
|
|
samlAuthCallbackURL func(context.Context, string) string
|
2022-03-14 07:55:09 +01:00
|
|
|
idpConfigAlg crypto.EncryptionAlgorithm
|
|
|
|
userCodeAlg crypto.EncryptionAlgorithm
|
2025-01-06 10:47:46 +01:00
|
|
|
caches *Caches
|
2022-02-14 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
LanguageCookieName string
|
2022-03-14 07:55:09 +01:00
|
|
|
CSRFCookieName string
|
2022-02-14 17:22:30 +01:00
|
|
|
Cache middleware.CacheConfig
|
2022-08-16 07:04:36 +02:00
|
|
|
AssetCache middleware.CacheConfig
|
2023-08-24 11:41:52 +02:00
|
|
|
|
|
|
|
// LoginV2
|
|
|
|
DefaultOTPEmailURLV2 string
|
2022-02-14 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
login = "LOGIN"
|
|
|
|
HandlerPrefix = "/ui/login"
|
|
|
|
DefaultLoggedOutPath = HandlerPrefix + EndpointLogoutDone
|
|
|
|
)
|
|
|
|
|
2022-03-14 07:55:09 +01:00
|
|
|
func CreateLogin(config Config,
|
|
|
|
command *command.Commands,
|
|
|
|
query *query.Queries,
|
|
|
|
authRepo *eventsourcing.EsRepository,
|
|
|
|
staticStorage static.Storage,
|
2022-04-25 11:16:36 +02:00
|
|
|
consolePath string,
|
2022-04-25 10:01:17 +02:00
|
|
|
oidcAuthCallbackURL func(context.Context, string) string,
|
2022-09-12 17:18:08 +01:00
|
|
|
samlAuthCallbackURL func(context.Context, string) string,
|
2022-03-14 07:55:09 +01:00
|
|
|
externalSecure bool,
|
2022-03-29 11:53:19 +02:00
|
|
|
userAgentCookie,
|
2022-04-25 10:01:17 +02:00
|
|
|
issuerInterceptor,
|
2022-09-12 17:18:08 +01:00
|
|
|
oidcInstanceHandler,
|
2023-02-27 22:36:43 +01:00
|
|
|
samlInstanceHandler,
|
|
|
|
assetCache,
|
2023-02-15 02:52:11 +01:00
|
|
|
accessHandler mux.MiddlewareFunc,
|
2022-03-14 07:55:09 +01:00
|
|
|
userCodeAlg crypto.EncryptionAlgorithm,
|
|
|
|
idpConfigAlg crypto.EncryptionAlgorithm,
|
|
|
|
csrfCookieKey []byte,
|
2025-01-06 10:47:46 +01:00
|
|
|
cacheConnectors connector.Connectors,
|
2022-03-14 07:55:09 +01:00
|
|
|
) (*Login, error) {
|
2022-02-14 17:22:30 +01:00
|
|
|
login := &Login{
|
|
|
|
oidcAuthCallbackURL: oidcAuthCallbackURL,
|
2022-09-12 17:18:08 +01:00
|
|
|
samlAuthCallbackURL: samlAuthCallbackURL,
|
2022-04-25 11:16:36 +02:00
|
|
|
externalSecure: externalSecure,
|
2022-03-14 07:55:09 +01:00
|
|
|
consolePath: consolePath,
|
2022-02-14 17:22:30 +01:00
|
|
|
command: command,
|
|
|
|
query: query,
|
|
|
|
staticStorage: staticStorage,
|
|
|
|
authRepo: authRepo,
|
2022-03-14 07:55:09 +01:00
|
|
|
idpConfigAlg: idpConfigAlg,
|
|
|
|
userCodeAlg: userCodeAlg,
|
2022-02-14 17:22:30 +01:00
|
|
|
}
|
2022-08-16 07:04:36 +02:00
|
|
|
csrfInterceptor := createCSRFInterceptor(config.CSRFCookieName, csrfCookieKey, externalSecure, login.csrfErrorHandler())
|
|
|
|
cacheInterceptor := createCacheInterceptor(config.Cache.MaxAge, config.Cache.SharedMaxAge, assetCache)
|
2022-02-14 17:22:30 +01:00
|
|
|
security := middleware.SecurityHeaders(csp(), login.cspErrorHandler)
|
2022-04-25 10:01:17 +02:00
|
|
|
|
2023-12-05 12:12:01 +01:00
|
|
|
login.router = CreateRouter(login, middleware.TelemetryHandler(IgnoreInstanceEndpoints...), oidcInstanceHandler, samlInstanceHandler, csrfInterceptor, cacheInterceptor, security, userAgentCookie, issuerInterceptor, accessHandler)
|
|
|
|
login.renderer = CreateRenderer(HandlerPrefix, staticStorage, config.LanguageCookieName)
|
2022-02-14 17:22:30 +01:00
|
|
|
login.parser = form.NewParser()
|
2025-01-06 10:47:46 +01:00
|
|
|
|
|
|
|
var err error
|
|
|
|
login.caches, err = startCaches(context.Background(), cacheConnectors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-14 17:22:30 +01:00
|
|
|
return login, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func csp() *middleware.CSP {
|
|
|
|
csp := middleware.DefaultSCP
|
|
|
|
csp.ObjectSrc = middleware.CSPSourceOptsSelf()
|
|
|
|
csp.StyleSrc = csp.StyleSrc.AddNonce()
|
2024-02-05 15:45:15 +01:00
|
|
|
csp.ScriptSrc = csp.ScriptSrc.AddNonce().AddHash("sha256", "AjPdJSbZmeWHnEc5ykvJFay8FTWeTeRbs9dutfZ0HqE=")
|
2022-02-14 17:22:30 +01:00
|
|
|
return &csp
|
|
|
|
}
|
|
|
|
|
2022-08-16 07:04:36 +02:00
|
|
|
func createCSRFInterceptor(cookieName string, csrfCookieKey []byte, externalSecure bool, errorHandler http.Handler) func(http.Handler) http.Handler {
|
2022-02-14 17:22:30 +01:00
|
|
|
path := "/"
|
2022-05-02 17:26:54 +02:00
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.HasPrefix(r.URL.Path, EndpointResources) {
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2023-08-31 08:39:16 +02:00
|
|
|
// ignore form post callback
|
|
|
|
// it will redirect to the "normal" callback, where the cookie is set again
|
2023-09-29 11:26:14 +02:00
|
|
|
if (r.URL.Path == EndpointExternalLoginCallbackFormPost || r.URL.Path == EndpointSAMLACS) && r.Method == http.MethodPost {
|
2023-08-31 08:39:16 +02:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-01-16 11:28:56 +01:00
|
|
|
// by default we use SameSite Lax and the externalSecure (TLS) for the secure flag
|
2023-11-14 11:01:59 +02:00
|
|
|
sameSiteMode := csrf.SameSiteLaxMode
|
2024-01-16 11:28:56 +01:00
|
|
|
secureOnly := externalSecure
|
|
|
|
instance := authz.GetInstance(r.Context())
|
|
|
|
// in case of `allow iframe`...
|
|
|
|
if len(instance.SecurityPolicyAllowedOrigins()) > 0 {
|
|
|
|
// ... we need to change to SameSite none ...
|
2023-11-14 11:01:59 +02:00
|
|
|
sameSiteMode = csrf.SameSiteNoneMode
|
2024-01-16 11:28:56 +01:00
|
|
|
// ... and since SameSite none requires the secure flag, we'll set it for TLS and for localhost
|
|
|
|
// (regardless of the TLS / externalSecure settings)
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 17:00:38 +02:00
|
|
|
secureOnly = externalSecure || http_utils.DomainContext(r.Context()).RequestedDomain() == "localhost"
|
2023-11-14 11:01:59 +02:00
|
|
|
}
|
2022-05-02 17:26:54 +02:00
|
|
|
csrf.Protect(csrfCookieKey,
|
2024-01-16 11:28:56 +01:00
|
|
|
csrf.Secure(secureOnly),
|
|
|
|
csrf.CookieName(http_utils.SetCookiePrefix(cookieName, externalSecure, http_utils.PrefixHost)),
|
2022-05-02 17:26:54 +02:00
|
|
|
csrf.Path(path),
|
|
|
|
csrf.ErrorHandler(errorHandler),
|
2023-11-14 11:01:59 +02:00
|
|
|
csrf.SameSite(sameSiteMode),
|
2022-05-02 17:26:54 +02:00
|
|
|
)(handler).ServeHTTP(w, r)
|
|
|
|
})
|
2022-08-16 07:04:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCacheInterceptor(maxAge, sharedMaxAge time.Duration, assetCache mux.MiddlewareFunc) func(http.Handler) http.Handler {
|
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.HasPrefix(r.URL.Path, EndpointDynamicResources) {
|
|
|
|
assetCache.Middleware(handler).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(r.URL.Path, EndpointResources) {
|
|
|
|
middleware.AssetsCacheInterceptor(maxAge, sharedMaxAge).Handler(handler).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
middleware.NoCacheInterceptor().Handler(handler).ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2022-02-14 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) Handler() http.Handler {
|
|
|
|
return l.router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) getClaimedUserIDsOfOrgDomain(ctx context.Context, orgName string) ([]string, error) {
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 17:00:38 +02:00
|
|
|
orgDomain, err := domain.NewIAMDomainName(orgName, http_utils.DomainContext(ctx).RequestedDomain())
|
2023-10-11 09:55:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loginName, err := query.NewUserPreferredLoginNameSearchQuery("@"+orgDomain, query.TextEndsWithIgnoreCase)
|
2022-02-14 17:22:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-17 09:55:28 +00:00
|
|
|
users, err := l.query.SearchUsers(ctx, &query.UserSearchQueries{Queries: []query.SearchQuery{loginName}}, "", nil)
|
2022-02-14 17:22:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userIDs := make([]string, len(users.Users))
|
|
|
|
for i, user := range users.Users {
|
|
|
|
userIDs[i] = user.ID
|
|
|
|
}
|
|
|
|
return userIDs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setContext(ctx context.Context, resourceOwner string) context.Context {
|
|
|
|
data := authz.CtxData{
|
|
|
|
UserID: login,
|
|
|
|
OrgID: resourceOwner,
|
|
|
|
}
|
|
|
|
return authz.SetCtxData(ctx, data)
|
|
|
|
}
|
2022-04-25 11:16:36 +02:00
|
|
|
|
2023-08-15 14:47:05 +02:00
|
|
|
func setUserContext(ctx context.Context, userID, resourceOwner string) context.Context {
|
|
|
|
data := authz.CtxData{
|
|
|
|
UserID: userID,
|
|
|
|
OrgID: resourceOwner,
|
|
|
|
}
|
|
|
|
return authz.SetCtxData(ctx, data)
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:16:36 +02:00
|
|
|
func (l *Login) baseURL(ctx context.Context) string {
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 17:00:38 +02:00
|
|
|
return http_utils.DomainContext(ctx).Origin() + HandlerPrefix
|
2022-04-25 11:16:36 +02:00
|
|
|
}
|
2025-01-06 10:47:46 +01:00
|
|
|
|
|
|
|
type Caches struct {
|
|
|
|
idpFormCallbacks cache.Cache[idpFormCallbackIndex, string, *idpFormCallback]
|
|
|
|
}
|
|
|
|
|
|
|
|
func startCaches(background context.Context, connectors connector.Connectors) (_ *Caches, err error) {
|
|
|
|
caches := new(Caches)
|
|
|
|
caches.idpFormCallbacks, err = connector.StartCache[idpFormCallbackIndex, string, *idpFormCallback](background, []idpFormCallbackIndex{idpFormCallbackIndexRequestID}, cache.PurposeIdPFormCallback, connectors.Config.IdPFormCallbacks, connectors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return caches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type idpFormCallbackIndex int
|
|
|
|
|
|
|
|
const (
|
|
|
|
idpFormCallbackIndexUnspecified idpFormCallbackIndex = iota
|
|
|
|
idpFormCallbackIndexRequestID
|
|
|
|
)
|
|
|
|
|
|
|
|
type idpFormCallback struct {
|
|
|
|
InstanceID string
|
|
|
|
State string
|
|
|
|
Form url.Values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys implements cache.Entry
|
|
|
|
func (c *idpFormCallback) Keys(i idpFormCallbackIndex) []string {
|
|
|
|
if i == idpFormCallbackIndexRequestID {
|
|
|
|
return []string{idpFormCallbackKey(c.InstanceID, c.State)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func idpFormCallbackKey(instanceID, state string) string {
|
|
|
|
return instanceID + "-" + state
|
|
|
|
}
|