@@ -11,9 +11,9 @@ import (
1111 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
1212)
1313
14−func NewBasicAuthSessionLoader(validator basic.Validator, sessionGroups []string) alice.Constructor {
14+func NewBasicAuthSessionLoader(validator basic.Validator, sessionGroups []string, preferEmail bool) alice.Constructor {
1515 return func(next http.Handler) http.Handler {
16− return loadBasicAuthSession(validator, sessionGroups, next)
16+ return loadBasicAuthSession(validator, sessionGroups, preferEmail, next)
1717 }
1818}
1919
@@ -22,7 +22,20 @@ func NewBasicAuthSessionLoader(validator basic.Validator, sessionGroups []string
2222// If no authorization header is found, or the header is invalid, no session
2323// will be loaded and the request will be passed to the next handler.
2424// If a session was loaded by a previous handler, it will not be replaced.
25−func loadBasicAuthSession(validator basic.Validator, sessionGroups []string, next http.Handler) http.Handler {
25+func loadBasicAuthSession(validator basic.Validator, sessionGroups []string, preferEmail bool, next http.Handler) http.Handler {
26+ // This is a hack to be backwards compatible with the old PreferEmailToUser option.
27+ // Long term we will have a rich static user configuration option and this will
28+ // be removed.
29+ // TODO(JoelSpeed): Remove this hack once rich static user config is implemented.
30+ getSession := getBasicSession
31+ if preferEmail {
32+ getSession = func(validator basic.Validator, sessionGroups []string, req *http.Request) (*sessionsapi.SessionState, error) {
33+ session, err := getBasicSession(validator, sessionGroups, req)
34+ session.Email = session.User
35+ return session, err
36+ }
37+ }
38+
2639 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
2740 scope := middlewareapi.GetRequestScope(req)
2841 // If scope is nil, this will panic.
@@ -33,7 +46,7 @@ func loadBasicAuthSession(validator basic.Validator, sessionGroups []string, nex
3346 return
3447 }
3548
36− session, err := getBasicSession(validator, sessionGroups, req)
49+ session, err := getSession(validator, sessionGroups, req)
3750 if err != nil {
3851 logger.Errorf("Error retrieving session from token in Authorization header: %v", err)
3952 }