Documentation
¶
Index ¶
- func ExtractBearerToken(r *http.Request) (string, bool)
- type AccessToken
- type Auth
- func (a *Auth) Authenticate(r *http.Request) (authenticated bool, reason string, err error)
- func (a *Auth) Authorize(r *http.Request, permission string) (authorized bool, err error)
- func (a *Auth) IsServiceRequest(r *http.Request) bool
- func (a *Auth) NewAuthClient() (*http.Client, error)
- func (a *Auth) Start() chan error
- type AuthClient
- type AuthProvider
- type Config
- type Key
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AccessToken ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
func (*Auth) Authenticate ¶
Authenticate checks the provided HTTP request for a valid Bearer token in the Authorization header. If the token is missing, malformed, or invalid, it returns false, a reason, and an error. The reason is only set when the request cannot be authenticated, and it is designed to be sent back to the client to provide feedback on why authentication failed.
func (*Auth) Authorize ¶
Authorize checks if the request meets the given permission. Returns true if authorized, false otherwise, and an error if the request is invalid or the check fails.
func (*Auth) IsServiceRequest ¶
IsServiceRequest checks whether the given HTTP request originates from a service. It delegates the request to the underlying AuthProvider to perform the service request check.
func (*Auth) NewAuthClient ¶
NewAuthClient creates a new HTTP client with an AuthClient as the transport, allowing access token injection on each request.
func (*Auth) Start ¶
Start initializes the auth service and begins a periodic refresh using a ticker. This function ensures that the service is started only once and returns an error channel that reports any issues encountered during refreshes. Consumers of this function must listen to the returned error channel to prevent it from blocking when errors occur.
type AuthClient ¶
type AuthClient struct {
// contains filtered or unexported fields
}
type AuthProvider ¶
type AuthProvider interface {
// AuthorizeRequest checks if the request meets the specified authorization requirement.
// It returns true if the request is authorized, otherwise false, and an error if something goes wrong.
AuthorizeRequest(r *http.Request, permission string) (isAuthorized bool, err error)
// IsServiceRequest checks whether the given HTTP request originates from a service.
// It returns true if the request is identified as a service request, otherwise false.
IsServiceRequest(r *http.Request) (isService bool)
// RefreshAccessToken refreshes the current access token.
// It returns the new access token, the time for the next refresh, and an error if the operation fails.
RefreshAccessToken() (accessToken *AccessToken, nextRefresh time.Time, err error)
// RefreshKeys retrieves updated authentication keys and the scheduled time for the next key refresh.
// It returns a slice of keys, the time for the next refresh, and an error if the operation fails.
RefreshKeys() (keys []*Key, nextRefresh time.Time, err error)
}
type Config ¶
type Config struct {
AuthProvider AuthProvider // Provider for authentication logic configuration
}
type Key ¶
type Key struct {
Kid string `json:"kid"` // Kid is the unique identifier for the key.
Iat int64 `json:"iat"` // Iat is the issued-at time in Unix time (seconds since the epoch).
Exp int64 `json:"exp"` // Exp is the expiration time in Unix time (seconds since the epoch).
Alg string `json:"alg"` // Alg specifies the algorithm used with the key (e.g., "RS256").
Pem string `json:"pem"` // Key contains the RSA public key in PEM format.
}