package
3.0.0
Repository: https://github.com/onsdigital/dp-net.git
Documentation: pkg.go.dev

# README

Handlers

This package contains handlers to manage header and cookie values, and identity validation.

Context values middleware

===================

CheckHeader and CheckCookie handlers forward values coming from a request header or a cookie, to a request context.

The mapping is done using enumeration of possible keys, and their mappings to header, context or cookie keys.

Usage

  • Read a header from a request and add its value to the underlying request context:
    // Access token
    handler := handlers.CheckHeader(handlers.UserAccess)

    // Locale
    handler := handlers.CheckHeader(handlers.Locale)

    // CollectionID
    handler := handlers.CheckHeader(handlers.CollectionID)
  • Read a cookie and add its value to the output request context:
    // Access token
    handler := handlers.CheckCookie(handlers.UserAccess)

    // Locale
    handler := handlers.CheckCookie(handlers.Locale)

    // CollectionID
    handler := handlers.CheckCookie(handlers.CollectionID)
  • Access the context value:
    // Access token
    accessToken := ctx.Value(handlers.UserAccess.Context())

    // Locale
    locale := ctx.Value(handlers.Locale.Context())

    // CollectionID
    collectionID := ctx.Value(handlers.CollectionID.Context())

Identity middleware

===================

Middleware component that authenticates requests against zebedee.

The identity and permissions returned from the identity endpoint are added to the request context.

Getting started

Initialise the identity middleware and add it into the HTTP handler chain using alice:

    router := mux.NewRouter()
    alice := alice.New(handlers.Identity(<zebedeeURL>)).Then(router)
    httpServer := server.New(config.BindAddr, alice)

Wrap authenticated endpoints using the handlers.CheckIdentity(handler) function to check that a request identity exists.

    router.Path("/jobs").Methods("POST").HandlerFunc(handlers.CheckIdentity(api.addJob))

Add required headers to outbound requests to other services

    import "github.com/ONSdigital/dp-net/v3/request"

    request.AddServiceTokenHeader(req, api.AuthToken)
    request.AddUserHeader(req, "UserA")

or, put less portably:

    req.Header.Add("Authorization", api.AuthToken)
    req.Header.Add("User-Identity", "UserA")

But most of this should be done by dp-net/v2/http and dp-api-clients-go/v2/....

Testing

If you need to use the middleware component in unit tests you can call the constructor function that allows injection of the HTTP client

import (
    clientsidentity "github.com/ONSdigital/dp-api-clients-go/v2/identity"
    dphttp "github.com/ONSdigital/dp-net/v3/http"
    dphandlers "github.com/ONSdigital/dp-net/v3/handlers"
)

httpClient := &dphttp.ClienterMock{
    DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
        return &http.Response{
            StatusCode: http.StatusOK,
        }, nil
    },
}
// set last argument to secretKey if you want to support legacy headers
clientsidentity.NewAPIClient(httpClient, zebedeeURL, "")

identityHandler := dphandlers.IdentityWithHTTPClient(doAuth, httpClient)

# Packages

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

# Functions

CheckCookie is a wrapper which adds a cookie value (if found) to the request context.
CheckHeader is a wrapper which adds a value from the request header (if found) to the request context.
CheckIdentity wraps a HTTP handler function.
ControllerHandler is a middleware handler that ensures all required logical arguments are being passed along and then returns a generic http.HandlerFunc.
DoCheckCookie returns a handler that performs the CheckCookie logic.
DoCheckHeader returns a handler that performs the CheckHeader logic.
No description provided by the author
Identity is a handler that controls the authenticating of a request.
IdentityWithHTTPClient allows a handler to be created that uses the given identity client.

# Constants

Possible values for sets of keys.
Possible values for sets of keys.
Possible values for sets of keys.

# Variables

KeyMaps maps the possible values of Key enumeration to their Header, Cookie and Context correspnding keys.

# Structs

KeyMap represents a mapping between header keys, cookie keys and context keys for an equivalent value.

# Type aliases

ControllerHandlerFunc is a function type that accepts arguments required for logical flow in handlers Implementation of function should set headers as needed.
Key - iota enum of possible sets of keys for middleware manipulation.