Categorygithub.com/xmidt-org/wrp-listener
modulepackage
0.9.0
Repository: https://github.com/xmidt-org/wrp-listener.git
Documentation: pkg.go.dev

# README

wrp-listener

wrp-listener is a library that provides a webhook registerer and a validation function to be used for authentication.

Build Status codecov.io Go Report Card Apache V2 License GitHub Release GoDoc

Summary

`wrp-listener`` provides a package to help a consumer register to a webhook and authenticate messages received. Registering to a webhook can be done directly or set up to run at an interval.

Details

The below code snippet gets you registered to the webhook and events flowing to you.

	l, err := listener.New("https://example.com",
		&webhook.Registration{
			Config: webhook.DeliveryConfig{
				ReceiverURL: receiverURL,
				ContentType: contentType,
			},
			Events:   []string{events},
			Duration: webhook.CustomDuration(5 * time.Minute),
		},		
		listener.AcceptSHA1(),
		listener.Interval(1 * time.Minute),
		listener.AcceptedSecrets(sharedSecrets...),
	)
	if err != nil {
		panic(err)
	}

    err = l.Register(context.Background(), sharedSecrets[0])
	if err != nil {
		panic(err)
	}

Authorization that the information from the webhook likstener provider is also pretty simple.

func (el *eventListener) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	token, err := el.l.Tokenize(r)
    if err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		return
	}

	err = el.l.Authorize(r, token)
	if err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		return
	}

	// ... do more stuff ...

	w.WriteHeader(http.StatusOK)
}

The full example found in cmd/bearerListener/main.go is a working command line example that shows how to use the library from end to end.

Additional examples can be found in the example_test.go file.

Functional tests are found in functional_test.go

Code of Conduct

This project and everyone participating in it are governed by the XMiDT Code Of Conduct. By participating, you agree to this Code.

Contributing

Refer to CONTRIBUTING.md.

# Packages

No description provided by the author
No description provided by the author

# Functions

AcceptCustom is an option that sets the hash to use for the webhook callback validation to use.
AcceptedSecrets is an option that provides the list of secrets accepted by the webhook listener when validating the callback event.
AcceptNoHash enables the use of no hash for the webhook listener callback validation.
AcceptSHA1 enables the use of the sha1 hash for the webhook listener callback validation.
AcceptSHA256 enables the use of the sha256 hash for the webhook listener callback validation.
DecorateRequest is an option that provides the function to use to decorate the http request before it is sent to the webhook registration endpoint.
HTTPClient is an option that provides the http client to use for the webhook listener registration to use.
Interval is an option that sets the interval to wait between webhook registration attempts.
New creates a new webhook listener with the given registration and options.
Once is an option that sets the webhook to only be registered once.
WebhookOpts is an option that provides the webhook.Options to apply during the validation of the registration of the webhook.
WithRegistrationEventListener is an option that provides the listener to use for webhook registration events.
WithRegistrationEventListener is an option that provides the listener to use for webhook registration events.
WithTokenizeEventListener is an option that provides the listener to use for webhook tokenize events.

# Variables

ErrInvalidAlgorithm is returned when the algorithm is not supported.
ErrDecoratorFailed is returned when the decorator returns an error.
ErrInput is returned when an invalid input is provided.
ErrInvalidHeaderFormat is returned when the header is not in the correct format.
ErrInvalidSignature is returned when the signature is invalid.
ErrInvalidTokenHeader is returned when the token header is invalid.
ErrNewRequestFailed is returned when the request cannot be created.
ErrNotAcceptedHash is returned when the hash is not accepted, because it is not in the list of accepted hashes.
ErrNoToken is returned when the token is not found.
ErrRegistrationFailed is returned when the webhook registration fails.
ErrRegistrationNotAttempted is returned when the webhook registration was not attempted.
ErrUnableToReadBody is returned when the body cannot be read.

# Structs

Listener provides a way to register a webhook and validate the callbacks.

# Interfaces

A Decorator decorates an http request before it is sent to the webhook registration endpoint.
Option is an interface that is used to configure the webhook listener.
Token represents the information needed to authenticate the flow of incoming webhook callbacks.

# Type aliases

CancelEventListenerFunc removes the listener it's associated with and cancels any future events sent to that listener.
The Decorator type is an adapter to allow the use of ordinary functions as decorators.