7 "github.com/justinas/alice"
8 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
9 sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/basic"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
14func NewBasicAuthSessionLoader(validator basic.Validator, sessionGroups []string, preferEmail bool) alice.Constructor {
15 return func(next http.Handler) http.Handler {
16 return loadBasicAuthSession(validator, sessionGroups, preferEmail, next)
20// loadBasicAuthSession attmepts to load a session from basic auth credentials
21// stored in an Authorization header within the request.
22// If no authorization header is found, or the header is invalid, no session
23// will be loaded and the request will be passed to the next handler.
24// If a session was loaded by a previous handler, it will not be replaced.
25func 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
29 // TODO(JoelSpeed): Remove this hack once rich static user config is implemented.
30 getSession := getBasicSession
32 getSession = func(validator basic.Validator, sessionGroups []string, req *http.Request) (*sessionsapi.SessionState, error) {
33 session, err := getBasicSession(validator, sessionGroups, req)
35 session.Email = session.User
41 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
42 scope := middlewareapi.GetRequestScope(req)
43 // If scope is nil, this will panic.
44 // A scope should always be injected before this handler is called.
45 if scope.Session != nil {
46 // The session was already loaded, pass to the next handler
47 next.ServeHTTP(rw, req)
51 session, err := getSession(validator, sessionGroups, req)
53 logger.Errorf("Error retrieving session from token in Authorization header: %v", err)
56 // Add the session to the scope if it was found
57 scope.Session = session
58 next.ServeHTTP(rw, req)
62// getBasicSession attempts to load a basic session from the request.
63// If the credentials in the request exist within the htpasswdMap,
64// a new session will be created.
65func getBasicSession(validator basic.Validator, sessionGroups []string, req *http.Request) (*sessionsapi.SessionState, error) {
66 auth := req.Header.Get("Authorization")
68 // No auth header provided, so don't attempt to load a session
72 user, password, err := findBasicCredentialsFromHeader(auth)
77 if validator.Validate(user, password) {
78 logger.PrintAuthf(user, req, logger.AuthSuccess, "Authenticated via basic auth and HTpasswd File")
80 return &sessionsapi.SessionState{User: user, Groups: sessionGroups}, nil
83 logger.PrintAuthf(user, req, logger.AuthFailure, "Invalid authentication via basic auth: not in Htpasswd File")
87// findBasicCredentialsFromHeader finds basic auth credneitals from the
88// Authorization header of a given request.
89func findBasicCredentialsFromHeader(header string) (string, string, error) {
90 tokenType, token, err := splitAuthHeader(header)
95 if tokenType != "Basic" {
96 return "", "", fmt.Errorf("invalid Authorization header: %q", header)
99 user, password, err := getBasicAuthCredentials(token)
101 return "", "", fmt.Errorf("error decoding basic auth credentials: %v", err)
104 return user, password, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list