2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
// Legacy headers, remove after 2022.1
|
2021-12-01 19:05:56 +00:00
|
|
|
headers.Set("X-Auth-Username", c.PreferredUsername)
|
|
|
|
headers.Set("X-Auth-Groups", strings.Join(c.Groups, "|"))
|
|
|
|
headers.Set("X-Forwarded-Email", c.Email)
|
|
|
|
headers.Set("X-Forwarded-Preferred-Username", c.PreferredUsername)
|
|
|
|
headers.Set("X-Forwarded-User", c.Sub)
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2021-10-02 20:00:23 +00:00
|
|
|
// New headers, unique prefix
|
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-10-02 20:00:23 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|