2020-08-31 06:49:35 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2022-05-02 15:26:54 +00:00
|
|
|
"strings"
|
2022-02-14 16:22:30 +00:00
|
|
|
"time"
|
2020-08-31 06:49:35 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
http_utils "github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
2020-08-31 06:49:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cookieKey int
|
|
|
|
|
|
|
|
var (
|
|
|
|
userAgentKey cookieKey = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
func UserAgentIDFromCtx(ctx context.Context) (string, bool) {
|
|
|
|
userAgentID, ok := ctx.Value(userAgentKey).(string)
|
|
|
|
return userAgentID, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserAgent struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type userAgentHandler struct {
|
2022-05-02 15:26:54 +00:00
|
|
|
cookieHandler *http_utils.CookieHandler
|
|
|
|
cookieName string
|
|
|
|
idGenerator id.Generator
|
|
|
|
nextHandler http.Handler
|
|
|
|
ignoredPrefixes []string
|
2020-08-31 06:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserAgentCookieConfig struct {
|
|
|
|
Name string
|
2022-02-14 16:22:30 +00:00
|
|
|
MaxAge time.Duration
|
2020-08-31 06:49:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 15:26:54 +00:00
|
|
|
func NewUserAgentHandler(config *UserAgentCookieConfig, cookieKey []byte, idGenerator id.Generator, externalSecure bool, ignoredPrefixes ...string) (func(http.Handler) http.Handler, error) {
|
2020-08-31 06:49:35 +00:00
|
|
|
opts := []http_utils.CookieHandlerOpt{
|
|
|
|
http_utils.WithEncryption(cookieKey, cookieKey),
|
|
|
|
http_utils.WithMaxAge(int(config.MaxAge.Seconds())),
|
|
|
|
}
|
2022-02-14 16:22:30 +00:00
|
|
|
if !externalSecure {
|
2020-08-31 06:49:35 +00:00
|
|
|
opts = append(opts, http_utils.WithUnsecure())
|
|
|
|
}
|
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return &userAgentHandler{
|
2022-05-02 15:26:54 +00:00
|
|
|
nextHandler: handler,
|
|
|
|
cookieName: config.Name,
|
|
|
|
cookieHandler: http_utils.NewCookieHandler(opts...),
|
|
|
|
idGenerator: idGenerator,
|
|
|
|
ignoredPrefixes: ignoredPrefixes,
|
2020-08-31 06:49:35 +00:00
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ua *userAgentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-05-02 15:26:54 +00:00
|
|
|
for _, prefix := range ua.ignoredPrefixes {
|
|
|
|
if strings.HasPrefix(r.URL.Path, prefix) {
|
|
|
|
ua.nextHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 06:49:35 +00:00
|
|
|
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)
|
2022-04-25 08:01:17 +00:00
|
|
|
ua.setUserAgent(w, r.Host, agent)
|
2020-08-31 06:49:35 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func (ua *userAgentHandler) setUserAgent(w http.ResponseWriter, host string, agent *UserAgent) error {
|
|
|
|
err := ua.cookieHandler.SetEncryptedCookie(w, ua.cookieName, host, agent)
|
2020-08-31 06:49:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.ThrowPermissionDenied(err, "HTTP-AqgqdA", "cannot set user agent cookie")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|