mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	if basic auth fails should write unauthorized to response
This commit is contained in:
		
							parent
							
								
									64d203491c
								
							
						
					
					
						commit
						253c069b26
					
				@ -31,6 +31,7 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
 | 
				
			|||||||
			// Check credentials
 | 
								// Check credentials
 | 
				
			||||||
			if !ok || username != rule.Username || password != rule.Password {
 | 
								if !ok || username != rule.Username || password != rule.Password {
 | 
				
			||||||
				w.Header().Set("WWW-Authenticate", "Basic")
 | 
									w.Header().Set("WWW-Authenticate", "Basic")
 | 
				
			||||||
 | 
									w.WriteHeader(http.StatusUnauthorized)
 | 
				
			||||||
				return http.StatusUnauthorized, nil
 | 
									return http.StatusUnauthorized, nil
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,12 +24,14 @@ func TestBasicAuth(t *testing.T) {
 | 
				
			|||||||
		result int
 | 
							result int
 | 
				
			||||||
		cred   string
 | 
							cred   string
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
 | 
							{"/testing", http.StatusUnauthorized, "ttest:test"},
 | 
				
			||||||
		{"/testing", http.StatusOK, "test:ttest"},
 | 
							{"/testing", http.StatusOK, "test:ttest"},
 | 
				
			||||||
 | 
							
 | 
				
			||||||
		{"/testing", http.StatusUnauthorized, ""},
 | 
							{"/testing", http.StatusUnauthorized, ""},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar"))
 | 
						
 | 
				
			||||||
	for i, test := range tests {
 | 
						for i, test := range tests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
@ -41,7 +43,14 @@ func TestBasicAuth(t *testing.T) {
 | 
				
			|||||||
		req.Header.Set("Authorization", auth)
 | 
							req.Header.Set("Authorization", auth)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		rec := httptest.NewRecorder()
 | 
							rec := httptest.NewRecorder()
 | 
				
			||||||
		rw.ServeHTTP(rec, req)
 | 
							result, err := rw.ServeHTTP(rec, req)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Test %d: Could not ServeHTTP %v", i, err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if result != test.result {
 | 
				
			||||||
 | 
								t.Errorf("Test %d: Expected Header '%d' but was '%d'",
 | 
				
			||||||
 | 
									i, test.result, result)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if rec.Code != test.result {
 | 
							if rec.Code != test.result {
 | 
				
			||||||
			t.Errorf("Test %d: Expected Header '%d' but was '%d'",
 | 
								t.Errorf("Test %d: Expected Header '%d' but was '%d'",
 | 
				
			||||||
@ -54,5 +63,5 @@ func TestBasicAuth(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) {
 | 
					func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) {
 | 
				
			||||||
	fmt.Fprintf(w, r.URL.String())
 | 
						fmt.Fprintf(w, r.URL.String())
 | 
				
			||||||
	return 0, nil
 | 
						return http.StatusOK, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user