46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
|
|
// ABOUTME: Authentication providers for EdgeXR Master Controller API
|
||
|
|
// ABOUTME: Supports Bearer token authentication with pluggable provider interface
|
||
|
|
|
||
|
|
package client
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
// AuthProvider interface for attaching authentication to requests
|
||
|
|
type AuthProvider interface {
|
||
|
|
// Attach adds authentication headers to the request
|
||
|
|
Attach(ctx context.Context, req *http.Request) error
|
||
|
|
}
|
||
|
|
|
||
|
|
// StaticTokenProvider implements Bearer token authentication with a fixed token
|
||
|
|
type StaticTokenProvider struct {
|
||
|
|
Token string
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewStaticTokenProvider creates a new static token provider
|
||
|
|
func NewStaticTokenProvider(token string) *StaticTokenProvider {
|
||
|
|
return &StaticTokenProvider{Token: token}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Attach adds the Bearer token to the request Authorization header
|
||
|
|
func (s *StaticTokenProvider) Attach(ctx context.Context, req *http.Request) error {
|
||
|
|
if s.Token != "" {
|
||
|
|
req.Header.Set("Authorization", "Bearer "+s.Token)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// NoAuthProvider implements no authentication (for testing or public endpoints)
|
||
|
|
type NoAuthProvider struct{}
|
||
|
|
|
||
|
|
// NewNoAuthProvider creates a new no-auth provider
|
||
|
|
func NewNoAuthProvider() *NoAuthProvider {
|
||
|
|
return &NoAuthProvider{}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Attach does nothing (no authentication)
|
||
|
|
func (n *NoAuthProvider) Attach(ctx context.Context, req *http.Request) error {
|
||
|
|
return nil
|
||
|
|
}
|