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) alice.Constructor {
15 return func(next http.Handler) http.Handler {
16 return loadBasicAuthSession(validator, 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, next http.Handler) http.Handler {
26 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
27 scope := middlewareapi.GetRequestScope(req)
28 // If scope is nil, this will panic.
29 // A scope should always be injected before this handler is called.
30 if scope.Session != nil {
31 // The session was already loaded, pass to the next handler
32 next.ServeHTTP(rw, req)
36 session, err := getBasicSession(validator, req)
38 logger.Errorf("Error retrieving session from token in Authorization header: %v", err)
41 // Add the session to the scope if it was found
42 scope.Session = session
43 next.ServeHTTP(rw, req)
47// getBasicSession attempts to load a basic session from the request.
48// If the credentials in the request exist within the htpasswdMap,
49// a new session will be created.
50func getBasicSession(validator basic.Validator, req *http.Request) (*sessionsapi.SessionState, error) {
51 auth := req.Header.Get("Authorization")
53 // No auth header provided, so don't attempt to load a session
57 user, password, err := findBasicCredentialsFromHeader(auth)
62 if validator.Validate(user, password) {
63 logger.PrintAuthf(user, req, logger.AuthSuccess, "Authenticated via basic auth and HTpasswd File")
64 return &sessionsapi.SessionState{User: user}, nil
67 logger.PrintAuthf(user, req, logger.AuthFailure, "Invalid authentication via basic auth: not in Htpasswd File")
71// findBasicCredentialsFromHeader finds basic auth credneitals from the
72// Authorization header of a given request.
73func findBasicCredentialsFromHeader(header string) (string, string, error) {
74 tokenType, token, err := splitAuthHeader(header)
79 if tokenType != "Basic" {
80 return "", "", fmt.Errorf("invalid Authorization header: %q", header)
83 user, password, err := getBasicAuthCredentials(token)
85 return "", "", fmt.Errorf("error decoding basic auth credentials: %v", err)
88 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