github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/httpserverargs/authenticator.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package httpserverargs
     5  
     6  import (
     7  	"github.com/juju/clock"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/apiserver/apiserverhttp"
    11  	"github.com/juju/juju/apiserver/httpcontext"
    12  	"github.com/juju/juju/apiserver/stateauthenticator"
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  // NewStateAuthenticatorFunc is a function type satisfied by
    17  // NewStateAuthenticator.
    18  type NewStateAuthenticatorFunc func(
    19  	statePool *state.StatePool,
    20  	mux *apiserverhttp.Mux,
    21  	clock clock.Clock,
    22  	abort <-chan struct{},
    23  ) (httpcontext.LocalMacaroonAuthenticator, error)
    24  
    25  // NewStateAuthenticator returns a new LocalMacaroonAuthenticator that
    26  // authenticates users and agents using the given state pool. The
    27  // authenticator will register handlers into the mux for dealing with
    28  // local macaroon logins.
    29  func NewStateAuthenticator(
    30  	statePool *state.StatePool,
    31  	mux *apiserverhttp.Mux,
    32  	clock clock.Clock,
    33  	abort <-chan struct{},
    34  ) (httpcontext.LocalMacaroonAuthenticator, error) {
    35  	stateAuthenticator, err := stateauthenticator.NewAuthenticator(statePool, clock)
    36  	if err != nil {
    37  		return nil, errors.Trace(err)
    38  	}
    39  	stateAuthenticator.AddHandlers(mux)
    40  	go stateAuthenticator.Maintain(abort)
    41  	return stateAuthenticator, nil
    42  }