Documentation
¶
Index ¶
- Constants
- Variables
- type AuthorizationService
- type AuthorizerFn
- type ErrCallbackFn
- type InvalidUserCallbackFn
- type Option
- func AsyncWorkers(n int) Option
- func Authorizer(fn AuthorizerFn) Option
- func EncryptionKey(kp nkeys.KeyPair) Option
- func ErrCallback(fn ErrCallbackFn) Option
- func InvalidUser(cb InvalidUserCallbackFn) Option
- func Logger(l natsserver.Logger) Option
- func Name(n string) Option
- func ResponseSigner(fn ResponseSignerFn) Option
- func ResponseSignerIssuer(pub string) Option
- func ResponseSignerKey(kp nkeys.KeyPair) Option
- func ServiceEndpoints(n int) Option
- type Options
- type RequestContextError
- type ResponseSignerFn
Constants ¶
const ( NatsServerXKeyHeader = "Nats-Server-Xkey" ExpectedAudience = "nats-authorization-request" SysRequestUserAuthSubj = "$SYS.REQ.USER.AUTH" )
Variables ¶
var ( // ErrAbortRequest is an Error that signals for operation to abort. // Aborted operations don't respond to the callout request. Reason for // aborting is to allow the NATS server to delay the connection error // response and thus delay any possible denial-of-service attack. ErrAbortRequest = errors.New("abort request") // ErrAuthorizerRequired indicates that an authorizer must be provided for // the operation to proceed. ErrAuthorizerRequired = errors.New("authorizer is required") // ErrBadCalloutOption indicates an invalid or incompatible configuration // option was provided to NewAuthorizationService. ErrBadCalloutOption = errors.New("bad options") // ErrService represents a general service error that may occur during // operation execution. ErrService = errors.New("service error") // ErrRejectedAuth indicates that an authorization request was explicitly // rejected, typically due to invalid credentials or other authorization // failures. ErrRejectedAuth = errors.New("rejected authorization request") // ErrWorkerChannelFull indicates the async worker channel is at capacity // and the request was dropped. ErrWorkerChannelFull = errors.New("async worker channel full") )
Functions ¶
This section is empty.
Types ¶
type AuthorizationService ¶
type AuthorizationService struct {
Service micro.Service
// contains filtered or unexported fields
}
AuthorizationService is a service that handles user authorization requests and processes JWT-based authentication. It uses options to specify configuration such as authorizers, response signers, encryption keys, and logging. The service can operate with synchronous or asynchronous request handling using worker channels.
func NewAuthorizationService ¶
func NewAuthorizationService( nc *nats.Conn, opts ...Option, ) (*AuthorizationService, error)
NewAuthorizationService initializes and returns a new instance of AuthorizationService with the provided options. It sets up the service, including logging, error handling, and request-processing functionality.
Errors may occur during initialization due to missing or incompatible options. Returns an AuthorizationService instance and an error, if any occurred.
func (*AuthorizationService) AsyncWorkerHandler ¶
func (c *AuthorizationService) AsyncWorkerHandler(msg micro.Request)
AsyncWorkerHandler enqueues an incoming micro.Request to the workers channel for asynchronous processing.
func (*AuthorizationService) ServiceHandler ¶
func (c *AuthorizationService) ServiceHandler(msg micro.Request)
ServiceHandler processes incoming authorization micro.Request messages, decodes and validates them, and generates appropriate responses or errors based on the authorization outcome.
func (*AuthorizationService) Stop ¶
func (c *AuthorizationService) Stop() error
Stop gracefully shuts down the AuthorizationService by stopping its underlying service and closing worker channels. Note that the connection provided to the service when created is not closed as it is not created by the service.
type AuthorizerFn ¶
type AuthorizerFn func(req *jwt.AuthorizationRequest) (string, error)
AuthorizerFn is a callback to AuthorizationService returns a user JWT
type ErrCallbackFn ¶
type ErrCallbackFn func(err error)
ErrCallbackFn this is an optional callback that gets invoked whenever the AuthorizerFn returns an error, this is useful for tests
type InvalidUserCallbackFn ¶
InvalidUserCallbackFn is a function type invoked when a user JWT validation fails, providing the JWT and the error details.
type Option ¶
Option is a function type used to configure the AuthorizationService options
func AsyncWorkers ¶
AsyncWorkers sets the number of asynchronous workers that will be used the AuthorizationService.
func Authorizer ¶
func Authorizer(fn AuthorizerFn) Option
Authorizer sets a custom authorization function (AuthorizerFn) for signing and handling user JWTs in the service configuration.
func EncryptionKey ¶
EncryptionKey sets the encryption key for the service, requiring it to be a curve seed. Returns an error for invalid keys.
func ErrCallback ¶
func ErrCallback(fn ErrCallbackFn) Option
ErrCallback sets a callback function to handle errors returned by the AuthorizerFn, useful for logging or testing.
func InvalidUser ¶
func InvalidUser(cb InvalidUserCallbackFn) Option
InvalidUser configures a callback function invoked when a user JWT validation fails, passing the JWT and error details.
func Logger ¶
func Logger(l natsserver.Logger) Option
Logger sets the custom logger for the AuthorizationService.
func Name ¶
Name sets the name of the service. This value must not contain spaces or dots of it may be rejected by the micro.Service backing the AuthorizationService.
func ResponseSigner ¶
func ResponseSigner(fn ResponseSignerFn) Option
ResponseSigner sets a custom ResponseSignerFn to handle the signing of AuthorizationResponseClaims in the service options.
func ResponseSignerIssuer ¶
ResponseSignerIssuer configures the issuer for the response signer using an account public key or seed. Returns an error if the provided key is invalid or not associated with an account.
func ResponseSignerKey ¶
ResponseSignerKey sets the response signer key to be used for signing authorization responses in the authorization service. The key pair must be an account private key, otherwise an error is returned.
func ServiceEndpoints ¶
ServiceEndpoints configures the number of service endpoints for the AuthorizationService.
type Options ¶
type Options struct {
// Name for the AuthorizationService cannot have spaces, etc, as this is
// the name that the actual micro.Service will use.
Name string
// Authorizer function that processes authorization request and issues user
// JWT
Authorizer AuthorizerFn
// ResponseSigner is a function that performs the signing of the
// jwt.AuthorizationResponseClaim
ResponseSigner ResponseSignerFn
// ResponseSigner is the key that will be used to sign the
// jwt.AuthorizationResponseClaim
ResponseSignerKey nkeys.KeyPair
// ResponseSigner is the key that ID of the account issuing the
// jwt.AuthorizationResponseClaim if not set, ResponseSigner is the account
ResponseSignerIssuer string
// EncryptionKey is an optional configuration that must be provided if the
// callout is configured to use encryption.
EncryptionKey nkeys.KeyPair
// Logger for the service process
Logger natsserver.Logger
// InvalidUser when set user JWTs are validated if error notified via the
// callback
InvalidUser InvalidUserCallbackFn
// ErrCallback is an optional callback invoked whenever AuthorizerFn
// returns an error, useful for handling test errors.
ErrCallback ErrCallbackFn
// ServiceEndpoints sets the number of endpoints available for the service
// to handle requests.
ServiceEndpoints int
// AsyncWorkers specifies the number of workers used for asynchronous task
// processing.
AsyncWorkers int
}
Options defines a configuration struct for handling authorization and signing of user JWTs within a service.
type RequestContextError ¶
RequestContextError represents an error in the context of handling a user request, including the user ID and the cause.
func (RequestContextError) Error ¶
func (r RequestContextError) Error() string
func (RequestContextError) Unwrap ¶
func (r RequestContextError) Unwrap() error
func (RequestContextError) User ¶
func (r RequestContextError) User() string
type ResponseSignerFn ¶
type ResponseSignerFn func(*jwt.AuthorizationResponseClaims) (string, error)
ResponseSignerFn allows externalizing the signing to a different workflow where the callout doesn't directly sign the jwt.AuthorizationResponseClaims but instead forwards the request to some other mechanism
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
delegated
command
|
|
|
delegated_default_sentinel
command
|
|
|
dynamic_accounts
command
|
|
|
dynamic_accounts/client
command
|