2024-02-13 11:00:48 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-02-13 21:39:34 +00:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2024-02-19 09:09:30 +00:00
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/pkg/jwt"
|
2024-02-13 11:00:48 +00:00
|
|
|
)
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
func (h *BaseHandler) Temporal(c echo.Context) error {
|
|
|
|
cc := c.(AuthenticatedContext)
|
|
|
|
|
2024-02-13 21:39:34 +00:00
|
|
|
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
2024-02-15 17:43:35 +00:00
|
|
|
Host: h.config.Temporal.UIHost,
|
2024-02-13 21:39:34 +00:00
|
|
|
Scheme: "http",
|
|
|
|
})
|
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
originalDirector := proxy.Director
|
2024-02-13 21:39:34 +00:00
|
|
|
|
2024-02-19 09:09:30 +00:00
|
|
|
proxy.Director = func(r *http.Request) {
|
|
|
|
originalDirector(r)
|
|
|
|
// Add authentication token to be able to access temporal.
|
|
|
|
// FIXME: Maybe cache it somehow so we don't generate it on every request?
|
|
|
|
token, _ := jwt.NewTokenForUser(
|
|
|
|
h.config.Jwt.PrivateKey,
|
|
|
|
h.config.Jwt.PublicKey,
|
2024-02-20 10:24:04 +00:00
|
|
|
cc.Principal.User.Email,
|
2024-02-19 09:09:30 +00:00
|
|
|
)
|
|
|
|
r.Header.Add("Authorization", "Bearer "+token)
|
2024-02-13 11:00:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
proxy.ServeHTTP(c.Response(), c.Request())
|
|
|
|
return nil
|
2024-02-13 11:00:48 +00:00
|
|
|
}
|