github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/basicauth.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  //go:build js
    14  
    15  package pouchdb
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	kivik "github.com/go-kivik/kivik/v4"
    22  )
    23  
    24  // BasicAuth handles HTTP Basic Auth for remote PouchDB connections. This
    25  // is the only auth support built directly into PouchDB, so this is a very
    26  // thin wrapper. This is the default authentication mechanism when credentials
    27  // are provided in the connection URL, so this function is rarely ever needed.
    28  func BasicAuth(username, password string) kivik.Option {
    29  	return basicAuth{
    30  		username: username,
    31  		password: password,
    32  	}
    33  }
    34  
    35  type basicAuth struct {
    36  	username string
    37  	password string
    38  }
    39  
    40  var _ kivik.Option = basicAuth{}
    41  
    42  func (a basicAuth) Apply(target interface{}) {
    43  	if client, ok := target.(*client); ok {
    44  		client.setAuth(a.username, a.password)
    45  	}
    46  }
    47  
    48  func (a basicAuth) String() string {
    49  	return fmt.Sprintf("[BasicAuth{user:%s,pass:%s}]", a.username, strings.Repeat("*", len(a.password)))
    50  }