github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/authentication/macaroon" 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 ) (macaroon.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 ) (macaroon.LocalMacaroonAuthenticator, error) { 35 stateAuthenticator, err := stateauthenticator.NewAuthenticator(statePool, clock) 36 if err != nil { 37 return nil, errors.Trace(err) 38 } 39 errH := stateAuthenticator.AddHandlers(mux) 40 if errH != nil { 41 return nil, errors.Trace(errH) 42 } 43 go stateAuthenticator.Maintain(abort) 44 return stateAuthenticator, nil 45 }