2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2022-01-27 17:14:02 +00:00
|
|
|
"context"
|
2021-09-08 18:04:56 +00:00
|
|
|
"encoding/base64"
|
2022-01-24 19:50:13 +00:00
|
|
|
"errors"
|
2021-09-08 18:04:56 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-12-21 14:49:25 +00:00
|
|
|
"net/url"
|
2021-09-08 18:04:56 +00:00
|
|
|
"strings"
|
2021-12-01 19:41:55 +00:00
|
|
|
|
2021-12-20 20:04:45 +00:00
|
|
|
"goauthentik.io/api"
|
2021-12-01 19:41:55 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-09-08 18:04:56 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 19:05:56 +00:00
|
|
|
func (a *Application) addHeaders(headers http.Header, c *Claims) {
|
2021-09-08 18:04:56 +00:00
|
|
|
// https://goauthentik.io/docs/providers/proxy/proxy
|
2021-10-02 20:00:23 +00:00
|
|
|
|
2021-12-01 19:05:56 +00:00
|
|
|
headers.Set("X-authentik-username", c.PreferredUsername)
|
|
|
|
headers.Set("X-authentik-groups", strings.Join(c.Groups, "|"))
|
|
|
|
headers.Set("X-authentik-email", c.Email)
|
|
|
|
headers.Set("X-authentik-name", c.Name)
|
|
|
|
headers.Set("X-authentik-uid", c.Sub)
|
2021-12-12 16:58:19 +00:00
|
|
|
headers.Set("X-authentik-jwt", c.RawToken)
|
2021-10-02 20:00:23 +00:00
|
|
|
|
2021-12-01 19:19:09 +00:00
|
|
|
// System headers
|
2022-01-21 12:29:51 +00:00
|
|
|
headers.Set("X-authentik-meta-jwks", a.endpint.JwksUri)
|
2021-12-01 19:19:09 +00:00
|
|
|
headers.Set("X-authentik-meta-outpost", a.outpostName)
|
|
|
|
headers.Set("X-authentik-meta-provider", a.proxyConfig.Name)
|
|
|
|
headers.Set("X-authentik-meta-app", a.proxyConfig.AssignedApplicationSlug)
|
2021-12-01 19:41:55 +00:00
|
|
|
headers.Set("X-authentik-meta-version", constants.OutpostUserAgent())
|
2021-12-01 19:19:09 +00:00
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
userAttributes := c.Proxy.UserAttributes
|
|
|
|
// Attempt to set basic auth based on user's attributes
|
|
|
|
if *a.proxyConfig.BasicAuthEnabled {
|
|
|
|
var ok bool
|
|
|
|
var password string
|
|
|
|
if password, ok = userAttributes[*a.proxyConfig.BasicAuthPasswordAttribute].(string); !ok {
|
|
|
|
password = ""
|
|
|
|
}
|
|
|
|
// Check if we should use email or a custom attribute as username
|
|
|
|
var username string
|
|
|
|
if username, ok = userAttributes[*a.proxyConfig.BasicAuthUserAttribute].(string); !ok {
|
|
|
|
username = c.Email
|
|
|
|
}
|
|
|
|
authVal := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
|
|
|
a.log.WithField("username", username).Trace("setting http basic auth")
|
2021-12-01 19:05:56 +00:00
|
|
|
headers.Set("Authorization", fmt.Sprintf("Basic %s", authVal))
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
// Check if user has additional headers set that we should sent
|
|
|
|
if additionalHeaders, ok := userAttributes["additionalHeaders"].(map[string]interface{}); ok {
|
|
|
|
a.log.WithField("headers", additionalHeaders).Trace("setting additional headers")
|
|
|
|
if additionalHeaders == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for key, value := range additionalHeaders {
|
2021-12-01 19:05:56 +00:00
|
|
|
headers.Set(key, toString(value))
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 20:04:45 +00:00
|
|
|
|
2022-01-24 19:50:13 +00:00
|
|
|
// getTraefikForwardUrl See https://doc.traefik.io/traefik/middlewares/forwardauth/
|
2022-01-27 17:14:02 +00:00
|
|
|
func (a *Application) getTraefikForwardUrl(r *http.Request) (*url.URL, error) {
|
2021-12-21 14:49:25 +00:00
|
|
|
u, err := url.Parse(fmt.Sprintf(
|
|
|
|
"%s://%s%s",
|
|
|
|
r.Header.Get("X-Forwarded-Proto"),
|
|
|
|
r.Header.Get("X-Forwarded-Host"),
|
|
|
|
r.Header.Get("X-Forwarded-Uri"),
|
|
|
|
))
|
|
|
|
if err != nil {
|
2022-01-27 17:14:02 +00:00
|
|
|
return nil, err
|
2021-12-21 14:49:25 +00:00
|
|
|
}
|
2021-12-26 13:11:11 +00:00
|
|
|
a.log.WithField("url", u.String()).Trace("traefik forwarded url")
|
2022-01-27 17:14:02 +00:00
|
|
|
return u, nil
|
2021-12-21 14:49:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 13:25:21 +00:00
|
|
|
// getNginxForwardUrl See https://github.com/kubernetes/ingress-nginx/blob/main/rootfs/etc/nginx/template/nginx.tmpl
|
2022-01-27 17:14:02 +00:00
|
|
|
func (a *Application) getNginxForwardUrl(r *http.Request) (*url.URL, error) {
|
|
|
|
ou := r.Header.Get("X-Original-URI")
|
|
|
|
if ou != "" {
|
|
|
|
u, _ := url.Parse(r.URL.String())
|
|
|
|
u.Path = ou
|
|
|
|
a.log.WithField("url", u.String()).Info("building forward URL from X-Original-URI")
|
|
|
|
return u, nil
|
|
|
|
}
|
2022-01-25 13:25:21 +00:00
|
|
|
h := r.Header.Get("X-Original-URL")
|
2022-01-24 19:50:13 +00:00
|
|
|
if len(h) < 1 {
|
2022-01-27 17:14:02 +00:00
|
|
|
return nil, errors.New("no forward URL found")
|
2022-01-24 19:50:13 +00:00
|
|
|
}
|
|
|
|
u, err := url.Parse(h)
|
|
|
|
if err != nil {
|
|
|
|
a.log.WithError(err).Warning("failed to parse URL from nginx")
|
2022-01-27 17:14:02 +00:00
|
|
|
return nil, err
|
2021-12-21 14:49:25 +00:00
|
|
|
}
|
2022-01-24 19:50:13 +00:00
|
|
|
a.log.WithField("url", u.String()).Trace("nginx forwarded url")
|
2022-01-27 17:14:02 +00:00
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) ReportMisconfiguration(r *http.Request, msg string, fields map[string]interface{}) {
|
|
|
|
fields["message"] = msg
|
|
|
|
a.log.WithFields(fields).Error("Reporting configuration error")
|
|
|
|
req := api.EventRequest{
|
|
|
|
Action: api.EVENTACTIONS_CONFIGURATION_ERROR,
|
|
|
|
App: "authentik.providers.proxy", // must match python apps.py name
|
|
|
|
ClientIp: *api.NewNullableString(api.PtrString(r.RemoteAddr)),
|
|
|
|
Context: &fields,
|
|
|
|
}
|
|
|
|
_, _, err := a.ak.Client.EventsApi.EventsEventsCreate(context.Background()).EventRequest(req).Execute()
|
|
|
|
if err != nil {
|
|
|
|
a.log.WithError(err).Warning("failed to report configuration error")
|
|
|
|
}
|
2022-01-24 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) IsAllowlisted(u *url.URL) bool {
|
|
|
|
for _, ur := range a.UnauthenticatedRegex {
|
2021-12-20 20:04:45 +00:00
|
|
|
var testString string
|
|
|
|
if a.Mode() == api.PROXYMODE_PROXY || a.Mode() == api.PROXYMODE_FORWARD_SINGLE {
|
2022-01-24 19:50:13 +00:00
|
|
|
testString = u.Path
|
2021-12-20 20:04:45 +00:00
|
|
|
} else {
|
2022-01-24 19:50:13 +00:00
|
|
|
testString = u.String()
|
2021-12-20 20:04:45 +00:00
|
|
|
}
|
|
|
|
a.log.WithField("regex", u.String()).WithField("url", testString).Trace("Matching URL against allow list")
|
2022-01-24 19:50:13 +00:00
|
|
|
if ur.MatchString(testString) {
|
2021-12-20 20:04:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|