...

Source file src/github.com/go-kivik/kivik/v4/x/server/userstore.go

Documentation: github.com/go-kivik/kivik/v4/x/server

     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  //go:build !js
    14  
    15  package server
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"github.com/go-kivik/kivik/v4"
    22  	internal "github.com/go-kivik/kivik/v4/int/errors"
    23  	"github.com/go-kivik/kivik/v4/x/server/auth"
    24  )
    25  
    26  type userStores []auth.UserStore
    27  
    28  var _ auth.UserStore = userStores{}
    29  
    30  func (s userStores) Validate(ctx context.Context, username, password string) (*auth.UserContext, error) {
    31  	for _, store := range s {
    32  		userCtx, err := store.Validate(ctx, username, password)
    33  		if kivik.HTTPStatus(err) == http.StatusNotFound {
    34  			continue
    35  		}
    36  		return userCtx, err
    37  	}
    38  	return nil, &internal.Error{Status: http.StatusUnauthorized, Message: "Invalid username or password"}
    39  }
    40  
    41  func (s userStores) UserCtx(ctx context.Context, username string) (*auth.UserContext, error) {
    42  	for _, store := range s {
    43  		userCtx, err := store.UserCtx(ctx, username)
    44  		if kivik.HTTPStatus(err) == http.StatusNotFound {
    45  			continue
    46  		}
    47  		return userCtx, err
    48  	}
    49  	return nil, &internal.Error{Status: http.StatusNotFound, Message: "User not found"}
    50  }
    51  

View as plain text