mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
56b916a2b0
* begin init checks for projections * first projection checks * debug notification providers with query fixes * more projections and first index * more projections * more projections * finish projections * fix tests (remove db name) * create tables in setup * fix logging / error handling * add tenant to views * rename tenant to instance_id * add instance_id to all projections * add instance_id to all queries * correct instance_id on projections * add instance_id to failed_events * use separate context for instance * implement features projection * implement features projection * remove unique constraint from setup when migration failed * add error to failed setup event * add instance_id to primary keys * fix IAM projection * remove old migrations folder * fix keysFromYAML test
100 lines
2.5 KiB
Go
100 lines
2.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
http_utils "github.com/caos/zitadel/internal/api/http"
|
|
"github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/id"
|
|
)
|
|
|
|
type cookieKey int
|
|
|
|
var (
|
|
userAgentKey cookieKey = 0
|
|
)
|
|
|
|
func UserAgentIDFromCtx(ctx context.Context) (string, bool) {
|
|
userAgentID, ok := ctx.Value(userAgentKey).(string)
|
|
return userAgentID, ok
|
|
}
|
|
|
|
func InstanceIDFromCtx(ctx context.Context) string {
|
|
return "" //TODO: implement
|
|
}
|
|
|
|
type UserAgent struct {
|
|
ID string
|
|
}
|
|
|
|
type userAgentHandler struct {
|
|
cookieHandler *http_utils.CookieHandler
|
|
cookieName string
|
|
idGenerator id.Generator
|
|
nextHandler http.Handler
|
|
}
|
|
|
|
type UserAgentCookieConfig struct {
|
|
Name string
|
|
MaxAge time.Duration
|
|
}
|
|
|
|
func NewUserAgentHandler(config *UserAgentCookieConfig, cookieKey []byte, domain string, idGenerator id.Generator, externalSecure bool) (func(http.Handler) http.Handler, error) {
|
|
opts := []http_utils.CookieHandlerOpt{
|
|
http_utils.WithEncryption(cookieKey, cookieKey),
|
|
http_utils.WithDomain(domain),
|
|
http_utils.WithMaxAge(int(config.MaxAge.Seconds())),
|
|
}
|
|
if !externalSecure {
|
|
opts = append(opts, http_utils.WithUnsecure())
|
|
}
|
|
return func(handler http.Handler) http.Handler {
|
|
return &userAgentHandler{
|
|
nextHandler: handler,
|
|
cookieName: config.Name,
|
|
cookieHandler: http_utils.NewCookieHandler(opts...),
|
|
idGenerator: idGenerator,
|
|
}
|
|
}, nil
|
|
}
|
|
|
|
func (ua *userAgentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
agent, err := ua.getUserAgent(r)
|
|
if err != nil {
|
|
agent, err = ua.newUserAgent()
|
|
}
|
|
if err == nil {
|
|
ctx := context.WithValue(r.Context(), userAgentKey, agent.ID)
|
|
r = r.WithContext(ctx)
|
|
ua.setUserAgent(w, agent)
|
|
}
|
|
ua.nextHandler.ServeHTTP(w, r)
|
|
}
|
|
|
|
func (ua *userAgentHandler) newUserAgent() (*UserAgent, error) {
|
|
agentID, err := ua.idGenerator.Next()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &UserAgent{ID: agentID}, nil
|
|
}
|
|
|
|
func (ua *userAgentHandler) getUserAgent(r *http.Request) (*UserAgent, error) {
|
|
userAgent := new(UserAgent)
|
|
err := ua.cookieHandler.GetEncryptedCookieValue(r, ua.cookieName, userAgent)
|
|
if err != nil {
|
|
return nil, errors.ThrowPermissionDenied(err, "HTTP-YULqH4", "cannot read user agent cookie")
|
|
}
|
|
return userAgent, nil
|
|
}
|
|
|
|
func (ua *userAgentHandler) setUserAgent(w http.ResponseWriter, agent *UserAgent) error {
|
|
err := ua.cookieHandler.SetEncryptedCookie(w, ua.cookieName, agent)
|
|
if err != nil {
|
|
return errors.ThrowPermissionDenied(err, "HTTP-AqgqdA", "cannot set user agent cookie")
|
|
}
|
|
return nil
|
|
}
|