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"
|
|
|
|
"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"
|
2021-10-13 19:48:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/api"
|
|
|
|
"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
|
|
|
|
|
2021-09-09 09:00:58 +00:00
|
|
|
endpint 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
|
|
|
})
|
|
|
|
|
|
|
|
// 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-02-08 19:25:38 +00:00
|
|
|
RedirectURL: urlJoin(p.ExternalHost, "/outpost.goauthentik.io/callback"),
|
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,
|
|
|
|
endpint: endpoint,
|
|
|
|
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
|
|
|
}
|
2021-10-12 11:33:20 +00:00
|
|
|
a.sessions = a.getStore(p)
|
2021-09-08 18:04:56 +00:00
|
|
|
mux.Use(web.NewLoggingHandler(muxLogger, func(l *log.Entry, r *http.Request) *log.Entry {
|
|
|
|
s, err := a.sessions.Get(r, constants.SeesionName)
|
|
|
|
if err != nil {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
claims, ok := s.Values[constants.SessionClaims]
|
|
|
|
if claims == nil || !ok {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
c, ok := claims.(Claims)
|
|
|
|
if !ok {
|
|
|
|
return l
|
|
|
|
}
|
2021-12-02 08:58:50 +00:00
|
|
|
return l.WithField("request_username", c.PreferredUsername)
|
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) {
|
|
|
|
c, _ := a.getClaims(r)
|
|
|
|
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",
|
|
|
|
"scheme": r.URL.Scheme,
|
|
|
|
"method": r.Method,
|
|
|
|
"path": r.URL.Path,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
"user": user,
|
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)
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
// Support /start and /sign_in for backwards compatibility
|
2022-02-08 19:25:38 +00:00
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/start", a.handleRedirect)
|
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/sign_in", a.handleRedirect)
|
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/callback", a.handleCallback)
|
|
|
|
mux.HandleFunc("/outpost.goauthentik.io/sign_out", a.handleSignOut)
|
2021-09-08 18:04:56 +00:00
|
|
|
switch *p.Mode {
|
|
|
|
case api.PROXYMODE_PROXY:
|
|
|
|
err = a.configureProxy()
|
|
|
|
case api.PROXYMODE_FORWARD_SINGLE:
|
|
|
|
fallthrough
|
|
|
|
case api.PROXYMODE_FORWARD_DOMAIN:
|
|
|
|
err = a.configureForward()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-13 19:48:11 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to configure application mode")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kp := p.Certificate.Get(); kp != nil {
|
|
|
|
err := cs.AddKeypair(*kp)
|
|
|
|
if err != nil {
|
2021-10-13 19:48:11 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to initially fetch certificate")
|
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 {
|
|
|
|
return *a.proxyConfig.Mode
|
|
|
|
}
|
|
|
|
|
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-01-07 08:51:41 +00:00
|
|
|
//TODO: Token revocation
|
2021-09-08 18:04:56 +00:00
|
|
|
s, err := a.sessions.Get(r, constants.SeesionName)
|
|
|
|
if err != nil {
|
2021-09-09 09:00:58 +00:00
|
|
|
http.Redirect(rw, r, a.endpint.EndSessionEndpoint, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Options.MaxAge = -1
|
|
|
|
err = s.Save(r, rw)
|
|
|
|
if err != nil {
|
2021-09-09 09:00:58 +00:00
|
|
|
http.Redirect(rw, r, a.endpint.EndSessionEndpoint, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-09 09:00:58 +00:00
|
|
|
http.Redirect(rw, r, a.endpint.EndSessionEndpoint, http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|