github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/auth/basic/basic.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  // Package basic provides HTTP Basic Auth services.
    14  package basic
    15  
    16  import (
    17  	"net/http"
    18  
    19  	"github.com/go-kivik/kivik/v4/x/kivikd"
    20  	"github.com/go-kivik/kivik/v4/x/kivikd/auth"
    21  	"github.com/go-kivik/kivik/v4/x/kivikd/authdb"
    22  )
    23  
    24  // HTTPBasicAuth provides HTTP Basic Auth
    25  type HTTPBasicAuth struct{}
    26  
    27  var _ auth.Handler = &HTTPBasicAuth{}
    28  
    29  // MethodName returns "default"
    30  func (a *HTTPBasicAuth) MethodName() string {
    31  	return "default" // For compatibility with the name used by CouchDB
    32  }
    33  
    34  // Authenticate authenticates a request against a user store using HTTP Basic
    35  // Auth.
    36  func (a *HTTPBasicAuth) Authenticate(_ http.ResponseWriter, r *http.Request) (*authdb.UserContext, error) {
    37  	store := kivikd.GetService(r).UserStore
    38  	username, password, ok := r.BasicAuth()
    39  	if !ok {
    40  		return nil, nil
    41  	}
    42  	return store.Validate(r.Context(), username, password)
    43  }