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-09-08 18:04:56 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc"
|
|
|
|
"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"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
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,
|
2021-09-10 14:19:29 +00:00
|
|
|
RedirectURL: urlJoin(p.ExternalHost, "/akprox/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{
|
|
|
|
Host: externalHost.Host,
|
|
|
|
log: log.WithField("logger", "authentik.outpost.proxy.bundle").WithField("provider", p.Name),
|
2021-09-16 08:03:31 +00:00
|
|
|
outpostName: ak.Outpost.Name,
|
2021-09-09 09:00:58 +00:00
|
|
|
endpint: endpoint,
|
2021-09-08 18:04:56 +00:00
|
|
|
oauthConfig: oauth2Config,
|
|
|
|
tokenVerifier: verifier,
|
|
|
|
proxyConfig: p,
|
|
|
|
httpClient: c,
|
|
|
|
mux: mux,
|
|
|
|
}
|
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-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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Support /start and /sign_in for backwards compatibility
|
|
|
|
mux.HandleFunc("/akprox/start", a.handleRedirect)
|
|
|
|
mux.HandleFunc("/akprox/sign_in", a.handleRedirect)
|
|
|
|
mux.HandleFunc("/akprox/callback", a.handleCallback)
|
|
|
|
mux.HandleFunc("/akprox/sign_out", a.handleSignOut)
|
|
|
|
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 {
|
|
|
|
// 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) IsAllowlisted(r *http.Request) bool {
|
|
|
|
for _, u := range a.UnauthenticatedRegex {
|
2021-12-13 14:27:28 +00:00
|
|
|
a.log.WithField("regex", u.String()).WithField("url", r.URL.Path).Trace("Matching URL against allow list")
|
2021-09-08 18:04:56 +00:00
|
|
|
if u.MatchString(r.URL.Path) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) Mode() api.ProxyMode {
|
|
|
|
return *a.proxyConfig.Mode
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// TODO: Token revocation
|
|
|
|
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
|
|
|
}
|