github.com/trustbloc/kms-go@v1.1.2/kms/webkms/headers_opts.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package webkms
     8  
     9  import (
    10  	"encoding/json"
    11  	"net/http"
    12  
    13  	"github.com/bluele/gcache"
    14  )
    15  
    16  // AddHeaders function supports adding custom http headers.
    17  type AddHeaders func(req *http.Request) (*http.Header, error)
    18  
    19  // Opts represents option.
    20  type Opts struct {
    21  	HeadersFunc     AddHeaders
    22  	ComputeMACCache gcache.Cache
    23  	marshal         MarshalFunc
    24  }
    25  
    26  // NewOpt creates a new empty option.
    27  // Not to be used directly. It's intended for implementations of remoteKMS.
    28  // Use WithHeaders() option function below instead.
    29  func NewOpt() *Opts {
    30  	return &Opts{marshal: json.Marshal}
    31  }
    32  
    33  // Opt are the remoteKMS option.
    34  type Opt func(opts *Opts)
    35  
    36  // WithHeaders option is for setting additional http request headers (since it's a function, it can call a remote
    37  // authorization server to fetch the necessary info needed in these headers).
    38  func WithHeaders(addHeadersFunc AddHeaders) Opt {
    39  	return func(opts *Opts) {
    40  		opts.HeadersFunc = addHeadersFunc
    41  	}
    42  }
    43  
    44  // WithCache add cache. if size is zero cache content will not be purged.
    45  func WithCache(cacheSize int) Opt {
    46  	return func(opts *Opts) {
    47  		opts.ComputeMACCache = gcache.New(cacheSize).Build()
    48  	}
    49  }
    50  
    51  // WithMarshalFn allows providing marshal function.
    52  func WithMarshalFn(fn MarshalFunc) Opt {
    53  	return func(opts *Opts) {
    54  		opts.marshal = fn
    55  	}
    56  }