@@ -22,6 +22,7 @@ import (
2222 ipapi "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/ip"
2323 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
2424 sessionsapi "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
25+ "github.com/oauth2-proxy/oauth2-proxy/pkg/authentication/basic"
2526 "github.com/oauth2-proxy/oauth2-proxy/pkg/cookies"
2627 "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
2728 "github.com/oauth2-proxy/oauth2-proxy/pkg/ip"
@@ -96,8 +97,8 @@ type OAuthProxy struct {
9697 sessionStore sessionsapi.SessionStore
9798 ProxyPrefix string
9899 SignInMessage string
99− HtpasswdFile *HtpasswdFile
100− DisplayHtpasswdForm bool
100+ basicAuthValidator basic.Validator
101+ displayHtpasswdForm bool
101102 serveMux http.Handler
102103 SetXAuthRequest bool
103104 PassBasicAuth bool
@@ -314,6 +315,16 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
314315 }
315316 }
316317
318+ var basicAuthValidator basic.Validator
319+ if opts.HtpasswdFile != "" {
320+ logger.Printf("using htpasswd file: %s", opts.HtpasswdFile)
321+ var err error
322+ basicAuthValidator, err = basic.NewHTPasswdValidator(opts.HtpasswdFile)
323+ if err != nil {
324+ return nil, fmt.Errorf("could not load htpasswdfile: %v", err)
325+ }
326+ }
327+
317328 return &OAuthProxy{
318329 CookieName: opts.Cookie.Name,
319330 CSRFCookieName: fmt.Sprintf("%v_%v", opts.Cookie.Name, "csrf"),
@@ -364,6 +375,9 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
364375 trustedIPs: trustedIPs,
365376 Banner: opts.Banner,
366377 Footer: opts.Footer,
378+
379+ basicAuthValidator: basicAuthValidator,
380+ displayHtpasswdForm: basicAuthValidator != nil,
367381 }, nil
368382}
369383
@@ -386,10 +400,6 @@ func (p *OAuthProxy) GetRedirectURI(host string) string {
386400 return u.String()
387401}
388402
389−func (p *OAuthProxy) displayCustomLoginForm() bool {
390− return p.HtpasswdFile != nil && p.DisplayHtpasswdForm
391−}
392−
393403func (p *OAuthProxy) redeemCode(ctx context.Context, host, code string) (s *sessionsapi.SessionState, err error) {
394404 if code == "" {
395405 return nil, errors.New("missing code")
@@ -526,7 +536,7 @@ func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
526536 }{
527537 ProviderName: p.provider.Data().ProviderName,
528538 SignInMessage: template.HTML(p.SignInMessage),
529− CustomLogin: p.displayCustomLoginForm(),
539+ CustomLogin: p.displayHtpasswdForm,
530540 Redirect: redirectURL,
531541 Version: VERSION,
532542 ProxyPrefix: p.ProxyPrefix,
@@ -540,7 +550,7 @@ func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
540550
541551// ManualSignIn handles basic auth logins to the proxy
542552func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
543− if req.Method != "POST" || p.HtpasswdFile == nil {
553+ if req.Method != "POST" || p.basicAuthValidator == nil {
544554 return "", false
545555 }
546556 user := req.FormValue("username")
@@ -549,7 +559,7 @@ func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (st
549559 return "", false
550560 }
551561 // check auth
552− if p.HtpasswdFile.Validate(user, passwd) {
562+ if p.basicAuthValidator.Validate(user, passwd) {
553563 logger.PrintAuthf(user, req, logger.AuthSuccess, "Authenticated via HtpasswdFile")
554564 return user, true
555565 }
@@ -1159,7 +1169,7 @@ func (p *OAuthProxy) stripAuthHeaders(req *http.Request) {
11591169// CheckBasicAuth checks the requests Authorization header for basic auth
11601170// credentials and authenticates these against the proxies HtpasswdFile
11611171func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*sessionsapi.SessionState, error) {
1162− if p.HtpasswdFile == nil {
1172+ if p.basicAuthValidator == nil {
11631173 return nil, nil
11641174 }
11651175 auth := req.Header.Get("Authorization")
@@ -1178,7 +1188,7 @@ func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*sessionsapi.SessionStat
11781188 if len(pair) != 2 {
11791189 return nil, fmt.Errorf("invalid format %s", b)
11801190 }
1181− if p.HtpasswdFile.Validate(pair[0], pair[1]) {
1191+ if p.basicAuthValidator.Validate(pair[0], pair[1]) {
11821192 logger.PrintAuthf(pair[0], req, logger.AuthSuccess, "Authenticated via basic auth and HTpasswd File")
11831193 return &sessionsapi.SessionState{User: pair[0]}, nil
11841194 }