git.iko.soy/typeable/oauth2-proxy
on 756d385 help sign in
89 lines 3.0 kB history raw
1package middleware
3import (
4 "fmt"
5 "net/http"
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)
17 }
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)
33 return
34 }
36 session, err := getBasicSession(validator, req)
37 if err != nil {
38 logger.Errorf("Error retrieving session from token in Authorization header: %v", err)
39 }
41 // Add the session to the scope if it was found
42 scope.Session = session
43 next.ServeHTTP(rw, req)
44 })
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")
52 if auth == "" {
53 // No auth header provided, so don't attempt to load a session
54 return nil, nil
55 }
57 user, password, err := findBasicCredentialsFromHeader(auth)
58 if err != nil {
59 return nil, err
60 }
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
65 }
67 logger.PrintAuthf(user, req, logger.AuthFailure, "Invalid authentication via basic auth: not in Htpasswd File")
68 return nil, nil
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)
75 if err != nil {
76 return "", "", err
77 }
79 if tokenType != "Basic" {
80 return "", "", fmt.Errorf("invalid Authorization header: %q", header)
81 }
83 user, password, err := getBasicAuthCredentials(token)
84 if err != nil {
85 return "", "", fmt.Errorf("error decoding basic auth credentials: %v", err)
86 }
88 return user, password, nil