2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2021-12-02 14:17:32 +00:00
|
|
|
"context"
|
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"
|
|
|
|
"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
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
sessions sessions.Store
|
|
|
|
proxyConfig api.ProxyOutpostConfig
|
|
|
|
httpClient *http.Client
|
|
|
|
|
|
|
|
log *log.Entry
|
|
|
|
mux *mux.Router
|
2022-01-27 17:14:02 +00:00
|
|
|
ak *ak.APIController
|
2021-12-20 21:20:23 +00:00
|
|
|
|
|
|
|
errorTemplates *template.Template
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 19:48:11 +00:00
|
|
|
func NewApplication(p api.ProxyOutpostConfig, c *http.Client, cs *ak.CryptoStore, ak *ak.APIController) (*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
|
|
|
|
|
|
|
var verifier = oidc.NewVerifier(p.OidcConfiguration.Issuer, ks, &oidc.Config{
|
|
|
|
ClientID: *p.ClientId,
|
2021-12-02 14:17:32 +00:00
|
|
|
SupportedSigningAlgs: []string{"RS256", "HS256"},
|
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()
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
// Configure an OpenID Connect aware OAuth2 client.
|
2021-09-16 08:03:31 +00:00
|
|
|
endpoint := GetOIDCEndpoint(p, ak.Outpost.Config["authentik_host"].(string))
|
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()
|
|
|
|
a := &Application{
|
2021-12-20 21:20:23 +00:00
|
|
|
Host: externalHost.Host,
|
2022-01-19 18:01:24 +00:00
|
|
|
log: muxLogger,
|
2021-12-20 21:20:23 +00:00
|
|
|
outpostName: ak.Outpost.Name,
|
2022-07-30 15:51:01 +00:00
|
|
|
endpoint: endpoint,
|
2021-12-20 21:20:23 +00:00
|
|
|
oauthConfig: oauth2Config,
|
|
|
|
tokenVerifier: verifier,
|
|
|
|
proxyConfig: p,
|
|
|
|
httpClient: c,
|
|
|
|
mux: mux,
|
|
|
|
errorTemplates: templates.GetTemplates(),
|
2022-01-27 17:14:02 +00:00
|
|
|
ak: ak,
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
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 != "" {
|
|
|
|
return l.WithField("request_username", c.PreferredUsername)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2023-01-14 20:44:28 +00:00
|
|
|
return l.WithField("request_username", 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)
|
|
|
|
after := time.Since(before)
|
|
|
|
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),
|
2021-09-08 18:04:56 +00:00
|
|
|
}).Observe(float64(after))
|
|
|
|
})
|
|
|
|
})
|
2021-12-16 10:00:19 +00:00
|
|
|
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)
|
2022-05-26 13:15:30 +00:00
|
|
|
switch *p.Mode.Get() {
|
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 {
|
|
|
|
err := cs.AddKeypair(*kp)
|
|
|
|
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
|
|
|
}
|
|
|
|
a.Cert = cs.Get(*kp)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-05-26 13:15:30 +00:00
|
|
|
return *a.proxyConfig.Mode.Get()
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) handleSignOut(rw http.ResponseWriter, r *http.Request) {
|
2022-12-12 19:02:37 +00:00
|
|
|
redirect := a.endpoint.EndSessionEndpoint
|
2022-05-20 08:10:26 +00:00
|
|
|
s, err := a.sessions.Get(r, constants.SessionName)
|
2021-09-08 18:04:56 +00:00
|
|
|
if err != nil {
|
2022-12-12 19:02:37 +00:00
|
|
|
http.Redirect(rw, r, redirect, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-12 19:02:37 +00:00
|
|
|
if c, exists := s.Values[constants.SessionClaims]; c == nil || !exists {
|
|
|
|
cc := c.(Claims)
|
|
|
|
uv := url.Values{
|
|
|
|
"id_token_hint": []string{cc.RawToken},
|
|
|
|
}
|
|
|
|
redirect += "?" + uv.Encode()
|
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
s.Options.MaxAge = -1
|
|
|
|
err = s.Save(r, rw)
|
|
|
|
if err != nil {
|
2022-12-12 19:02:37 +00:00
|
|
|
http.Redirect(rw, r, redirect, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-12 19:02:37 +00:00
|
|
|
http.Redirect(rw, r, redirect, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|