Http: add pluggable authentication and token (#20066)

This commit is contained in:
andig 2025-03-27 12:35:13 +01:00 • committed by GitHub
parent 6f5c0b6478
commit 986772d525
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 140 additions and 48 deletions

7
plugin/auth/api.go Normal file
View file

@ -0,0 +1,7 @@
package auth
import "net/http"
type Authorizer interface {
Transport(base http.RoundTripper) (http.RoundTripper, error)
}

26
plugin/auth/config.go Normal file
View file

@ -0,0 +1,26 @@
package auth
import (
"context"
"fmt"
"strings"
reg "github.com/evcc-io/evcc/util/registry"
)
var registry = reg.New[Authorizer]("auth")
// NewFromConfig creates auth from configuration
func NewFromConfig(ctx context.Context, typ string, other map[string]any) (Authorizer, error) {
factory, err := registry.Get(strings.ToLower(typ))
if err != nil {
return nil, err
}
v, err := factory(ctx, other)
if err != nil {
err = fmt.Errorf("cannot create auth type '%s': %w", typ, err)
}
return v, err
}

27
plugin/auth/nop.go Normal file
View file

@ -0,0 +1,27 @@
package auth
import (
"context"
"net/http"
"github.com/evcc-io/evcc/util"
)
type nop struct{}
func init() {
registry.AddCtx("nop", NewNopFromConfig)
}
func NewNopFromConfig(ctx context.Context, other map[string]any) (Authorizer, error) {
var cc struct{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return new(nop), nil
}
func (p *nop) Transport(base http.RoundTripper) (http.RoundTripper, error) {
return base, nil
}