2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2021-12-02 14:17:32 +00:00
|
|
|
"context"
|
2023-02-12 15:34:57 +00:00
|
|
|
"crypto/sha256"
|
2021-09-08 18:04:56 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/gob"
|
2021-10-13 19:48:11 +00:00
|
|
|
"fmt"
|
2021-12-20 21:20:23 +00:00
|
|
|
"html/template"
|
2021-09-08 18:04:56 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-07-30 15:51:01 +00:00
|
|
|
"path"
|
2021-09-08 18:04:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc"
|
2021-12-16 10:00:19 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/sessions"
|
2023-01-14 21:12:48 +00:00
|
|
|
"github.com/jellydator/ttlcache/v3"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/constants"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/hs256"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/metrics"
|
2021-12-20 21:20:23 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2/templates"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
Host string
|
|
|
|
Cert *tls.Certificate
|
|
|
|
UnauthenticatedRegex []*regexp.Regexp
|
|
|
|
|
2022-07-30 15:51:01 +00:00
|
|
|
endpoint OIDCEndpoint
|
2021-09-08 18:04:56 +00:00
|
|
|
oauthConfig oauth2.Config
|
|
|
|
tokenVerifier *oidc.IDTokenVerifier
|
2021-09-16 08:03:31 +00:00
|
|
|
outpostName string
|
2023-02-12 15:34:57 +00:00
|
|
|
sessionName string
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2023-02-13 15:34:47 +00:00
|
|
|
sessions sessions.Store
|
|
|
|
proxyConfig api.ProxyOutpostConfig
|
|
|
|
httpClient *http.Client
|
|
|
|
publicHostHTTPClient *http.Client
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
log *log.Entry
|
|
|
|
mux *mux.Router
|
2022-01-27 17:14:02 +00:00
|
|
|
ak *ak.APIController
|
2023-02-12 15:34:57 +00:00
|
|
|
srv Server
|
2021-12-20 21:20:23 +00:00
|
|
|
|
2023-01-14 21:12:48 +00:00
|
|
|
errorTemplates *template.Template
|
|
|
|
authHeaderCache *ttlcache.Cache[string, Claims]
|
2023-08-26 17:40:48 +00:00
|
|
|
|
|
|
|
isEmbedded bool
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 15:34:57 +00:00
|
|
|
type Server interface {
|
|
|
|
API() *ak.APIController
|
|
|
|
Apps() []*Application
|
|
|
|
CryptoStore() *ak.CryptoStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApplication(p api.ProxyOutpostConfig, c *http.Client, server Server) (*Application, error) {
|
2021-09-08 18:04:56 +00:00
|
|
|
gob.Register(Claims{})
|
2021-10-13 19:48:11 +00:00
|
|
|
muxLogger := log.WithField("logger", "authentik.outpost.proxyv2.application").WithField("name", p.Name)
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
externalHost, err := url.Parse(p.ExternalHost)
|
|
|
|
if err != nil {
|
2021-10-13 19:48:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse URL, skipping provider")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:17:32 +00:00
|
|
|
var ks oidc.KeySet
|
|
|
|
if contains(p.OidcConfiguration.IdTokenSigningAlgValuesSupported, "HS256") {
|
|
|
|
ks = hs256.NewKeySet(*p.ClientSecret)
|
|
|
|
} else {
|
|
|
|
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, c)
|
|
|
|
ks = oidc.NewRemoteKeySet(ctx, p.OidcConfiguration.JwksUri)
|
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2022-07-30 15:51:01 +00:00
|
|
|
redirectUri, _ := url.Parse(p.ExternalHost)
|
|
|
|
redirectUri.Path = path.Join(redirectUri.Path, "/outpost.goauthentik.io/callback")
|
|
|
|
redirectUri.RawQuery = url.Values{
|
|
|
|
CallbackSignature: []string{"true"},
|
|
|
|
}.Encode()
|
|
|
|
|
2023-08-26 17:40:48 +00:00
|
|
|
isEmbedded := false
|
2023-02-12 15:34:57 +00:00
|
|
|
if m := server.API().Outpost.Managed.Get(); m != nil {
|
2023-08-26 17:40:48 +00:00
|
|
|
isEmbedded = *m == "goauthentik.io/outposts/embedded"
|
2023-01-19 14:39:30 +00:00
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
// Configure an OpenID Connect aware OAuth2 client.
|
2023-01-19 14:39:30 +00:00
|
|
|
endpoint := GetOIDCEndpoint(
|
|
|
|
p,
|
2023-02-12 15:34:57 +00:00
|
|
|
server.API().Outpost.Config["authentik_host"].(string),
|
2023-08-26 17:40:48 +00:00
|
|
|
isEmbedded,
|
2023-01-19 14:39:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
verifier := oidc.NewVerifier(endpoint.Issuer, ks, &oidc.Config{
|
|
|
|
ClientID: *p.ClientId,
|
|
|
|
SupportedSigningAlgs: []string{"RS256", "HS256"},
|
|
|
|
})
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
oauth2Config := oauth2.Config{
|
|
|
|
ClientID: *p.ClientId,
|
|
|
|
ClientSecret: *p.ClientSecret,
|
2022-07-30 15:51:01 +00:00
|
|
|
RedirectURL: redirectUri.String(),
|
2021-09-09 09:00:58 +00:00
|
|
|
Endpoint: endpoint.Endpoint,
|
2021-11-10 22:06:21 +00:00
|
|
|
Scopes: p.ScopesToRequest,
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
mux := mux.NewRouter()
|
2023-02-12 15:34:57 +00:00
|
|
|
|
2023-02-13 15:34:47 +00:00
|
|
|
// Save cookie name, based on hashed client ID
|
2023-02-12 15:34:57 +00:00
|
|
|
h := sha256.New()
|
|
|
|
bs := string(h.Sum([]byte(*p.ClientId)))
|
|
|
|
sessionName := fmt.Sprintf("authentik_proxy_%s", bs[:8])
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
a := &Application{
|
2023-02-13 15:34:47 +00:00
|
|
|
Host: externalHost.Host,
|
|
|
|
log: muxLogger,
|
|
|
|
outpostName: server.API().Outpost.Name,
|
|
|
|
sessionName: sessionName,
|
|
|
|
endpoint: endpoint,
|
|
|
|
oauthConfig: oauth2Config,
|
|
|
|
tokenVerifier: verifier,
|
|
|
|
proxyConfig: p,
|
|
|
|
httpClient: c,
|
|
|
|
publicHostHTTPClient: web.NewHostInterceptor(c, server.API().Outpost.Config["authentik_host"].(string)),
|
|
|
|
mux: mux,
|
|
|
|
errorTemplates: templates.GetTemplates(),
|
|
|
|
ak: server.API(),
|
|
|
|
authHeaderCache: ttlcache.New(ttlcache.WithDisableTouchOnHit[string, Claims]()),
|
|
|
|
srv: server,
|
2023-08-26 17:40:48 +00:00
|
|
|
isEmbedded: isEmbedded,
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2023-01-14 21:12:48 +00:00
|
|
|
go a.authHeaderCache.Start()
|
2022-05-11 08:08:38 +00:00
|
|
|
a.sessions = a.getStore(p, externalHost)
|
2021-09-08 18:04:56 +00:00
|
|
|
mux.Use(web.NewLoggingHandler(muxLogger, func(l *log.Entry, r *http.Request) *log.Entry {
|
2023-01-14 20:44:28 +00:00
|
|
|
c := a.getClaimsFromSession(r)
|
|
|
|
if c == nil {
|
2021-09-08 18:04:56 +00:00
|
|
|
return l
|
|
|
|
}
|
2023-01-14 20:44:28 +00:00
|
|
|
if c.PreferredUsername != "" {
|
2023-01-14 22:29:51 +00:00
|
|
|
return l.WithField("user", c.PreferredUsername)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2023-01-14 22:29:51 +00:00
|
|
|
return l.WithField("user", c.Sub)
|
2021-09-08 18:04:56 +00:00
|
|
|
}))
|
|
|
|
mux.Use(func(inner http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2023-01-14 20:44:28 +00:00
|
|
|
c := a.getClaimsFromSession(r)
|
2021-09-08 18:04:56 +00:00
|
|
|
user := ""
|
|
|
|
if c != nil {
|
2021-12-02 08:58:50 +00:00
|
|
|
user = c.PreferredUsername
|
2021-12-16 10:19:57 +00:00
|
|
|
hub := sentry.GetHubFromContext(r.Context())
|
|
|
|
if hub == nil {
|
|
|
|
hub = sentry.CurrentHub()
|
|
|
|
}
|
|
|
|
hub.Scope().SetUser(sentry.User{
|
2021-12-16 10:00:19 +00:00
|
|
|
Username: user,
|
|
|
|
ID: c.Sub,
|
|
|
|
IPAddress: r.RemoteAddr,
|
|
|
|
})
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
before := time.Now()
|
|
|
|
inner.ServeHTTP(rw, r)
|
2023-07-27 16:51:08 +00:00
|
|
|
elapsed := time.Since(before)
|
2021-09-08 18:04:56 +00:00
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": a.outpostName,
|
|
|
|
"type": "app",
|
|
|
|
"method": r.Method,
|
|
|
|
"host": web.GetHost(r),
|
2023-07-27 16:51:08 +00:00
|
|
|
}).Observe(float64(elapsed) / float64(time.Second))
|
|
|
|
metrics.RequestsLegacy.With(prometheus.Labels{
|
|
|
|
"outpost_name": a.outpostName,
|
|
|
|
"type": "app",
|
|
|
|
"method": r.Method,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
}).Observe(float64(elapsed))
|
2021-09-08 18:04:56 +00:00
|
|
|
})
|
|
|
|
})
|
2023-02-12 15:34:57 +00:00
|
|
|
if server.API().GlobalConfig.ErrorReporting.Enabled {
|
|
|
|
mux.Use(sentryhttp.New(sentryhttp.Options{}).Handle)
|
|
|
|
}
|
2022-07-30 15:51:01 +00:00
|
|
|
mux.Use(func(inner http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-18 19:10:35 +00:00
|
|
|
if strings.EqualFold(r.URL.Query().Get(CallbackSignature), "true") {
|
|
|
|
a.log.Debug("handling OAuth Callback from querystring signature")
|
2022-07-30 15:51:01 +00:00
|
|
|
a.handleAuthCallback(w, r)
|
2022-08-18 19:10:35 +00:00
|
|
|
} else if strings.EqualFold(r.URL.Query().Get(LogoutSignature), "true") {
|
|
|
|
a.log.Debug("handling OAuth Logout from querystring signature")
|
2022-08-07 16:50:24 +00:00
|
|
|
a.handleSignOut(w, r)
|
2022-07-30 15:51:01 +00:00
|
|
|
} else {
|
|
|
|
inner.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2022-07-30 15:51:01 +00:00
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/start", a.handleAuthStart)
|
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/callback", a.handleAuthCallback)
|
2022-02-08 19:25:38 +00:00
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/sign_out", a.handleSignOut)
|
2023-04-01 16:10:52 +00:00
|
|
|
switch *p.Mode {
|
2021-09-08 18:04:56 +00:00
|
|
|
case api.PROXYMODE_PROXY:
|
|
|
|
err = a.configureProxy()
|
|
|
|
case api.PROXYMODE_FORWARD_SINGLE:
|
|
|
|
fallthrough
|
|
|
|
case api.PROXYMODE_FORWARD_DOMAIN:
|
|
|
|
err = a.configureForward()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-05 20:26:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to configure application mode: %w", err)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kp := p.Certificate.Get(); kp != nil {
|
2023-02-12 15:34:57 +00:00
|
|
|
err := server.CryptoStore().AddKeypair(*kp)
|
2021-09-08 18:04:56 +00:00
|
|
|
if err != nil {
|
2022-07-05 20:26:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to initially fetch certificate: %w", err)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2023-02-12 15:34:57 +00:00
|
|
|
a.Cert = server.CryptoStore().Get(*kp)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *p.SkipPathRegex != "" {
|
|
|
|
a.UnauthenticatedRegex = make([]*regexp.Regexp, 0)
|
|
|
|
for _, regex := range strings.Split(*p.SkipPathRegex, "\n") {
|
|
|
|
re, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
2022-01-07 08:51:41 +00:00
|
|
|
//TODO: maybe create event for this?
|
2021-11-28 14:06:26 +00:00
|
|
|
a.log.WithError(err).Warning("failed to compile SkipPathRegex")
|
|
|
|
continue
|
2021-09-08 18:04:56 +00:00
|
|
|
} else {
|
|
|
|
a.UnauthenticatedRegex = append(a.UnauthenticatedRegex, re)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 19:48:11 +00:00
|
|
|
return a, nil
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) Mode() api.ProxyMode {
|
2023-04-01 16:10:52 +00:00
|
|
|
return *a.proxyConfig.Mode
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:43:01 +00:00
|
|
|
func (a *Application) HasQuerySignature(r *http.Request) bool {
|
|
|
|
if strings.EqualFold(r.URL.Query().Get(CallbackSignature), "true") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.EqualFold(r.URL.Query().Get(LogoutSignature), "true") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:19:43 +00:00
|
|
|
func (a *Application) ProxyConfig() api.ProxyOutpostConfig {
|
|
|
|
return a.proxyConfig
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (a *Application) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
a.mux.ServeHTTP(rw, r)
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:12:48 +00:00
|
|
|
func (a *Application) Stop() {
|
|
|
|
a.authHeaderCache.Stop()
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (a *Application) handleSignOut(rw http.ResponseWriter, r *http.Request) {
|
2022-12-12 19:02:37 +00:00
|
|
|
redirect := a.endpoint.EndSessionEndpoint
|
2023-02-12 15:34:57 +00:00
|
|
|
s, err := a.sessions.Get(r, a.SessionName())
|
2021-09-08 18:04:56 +00:00
|
|
|
if err != nil {
|
2023-02-02 20:18:59 +00:00
|
|
|
a.redirectToStart(rw, r)
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-02 20:18:59 +00:00
|
|
|
c, exists := s.Values[constants.SessionClaims]
|
|
|
|
if c == nil && !exists {
|
|
|
|
a.redirectToStart(rw, r)
|
|
|
|
return
|
2022-12-12 19:02:37 +00:00
|
|
|
}
|
2023-02-02 20:18:59 +00:00
|
|
|
cc := c.(Claims)
|
|
|
|
uv := url.Values{
|
|
|
|
"id_token_hint": []string{cc.RawToken},
|
|
|
|
}
|
|
|
|
redirect += "?" + uv.Encode()
|
|
|
|
err = a.Logout(cc.Sub)
|
2021-09-08 18:04:56 +00:00
|
|
|
if err != nil {
|
2023-02-02 20:18:59 +00:00
|
|
|
a.log.WithError(err).Warning("failed to logout of other sessions")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2022-12-12 19:02:37 +00:00
|
|
|
http.Redirect(rw, r, redirect, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|