signer

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2021 License: MIT Imports: 9 Imported by: 0

README

Build Status Go Reference

Go HSDP Signer

This package implements the HSDP API signing algorithm. You can sign a http.Request instance

Usage

package main

import (
  "github.com/philips-software/go-hsdp-signer"
  "net/http"
)

func newSigner(sharedKey, secretKey string) func(*http.Request) error {
    s, err := signer.New(sharedKey, secretKey)
    if err != nil {
       return func(req *http.Request) error {
          return err
       }
    }
    return func(req *http.Request) error {
	return s.SignRequest(req)
    }	
}

func main() {
    signRequest := newSigner("myKey", "mySecret")

    req, _ := http.NewRequest("GET", "https://example.com/some/path", nil)
    
    signRequest(req)
     
}

License

Licensed is MIT

Documentation

Overview

Package signer provides an implementation of the HSDP API signing algorithm. It can sign standard Go http.Request

Index

Constants

View Source
const (
	LogTimeFormat       = "2006-01-02T15:04:05.000Z07:00"
	TimeFormat          = time.RFC3339
	HeaderAuthorization = "hsdp-api-signature"
	HeaderSignedDate    = "SignedDate"
	DefaultPrefix64     = "REhQV1M="
	AlgorithmName       = "HmacSHA256"
)

Constants

Variables

View Source
var (
	ErrMissingSharedKey   = errors.New("missing shared key")
	ErrMissingShareSecret = errors.New("missing shared secret")
	ErrSignatureExpired   = errors.New("signature expired")
	ErrInvalidSignature   = errors.New("invalid signature")
	ErrInvalidCredential  = errors.New("invalid credential")
	ErrNotSupportedYet    = errors.New("missing implementation, please contact the author(s)")
	ErrInvalidNowFunc     = errors.New("invalid now function")
)

Errors

Functions

func GetSharedKey added in v1.2.0

func GetSharedKey(request *http.Request) (string, error)

GetSharedKey extracts the shared key from request

func Prefix added in v1.2.0

func Prefix(prefix string) func(*Signer) error

Prefix sets the signing prefix

func SignBody added in v1.2.0

func SignBody() func(*Signer) error

SignBody includes body in the signature

func SignHeaders added in v1.3.0

func SignHeaders(headers ...string) func(*Signer) error

SignHeaders includes the headers if present

func SignMethod added in v1.2.0

func SignMethod() func(*Signer) error

SignMethod includes body in the signature

func SignParam added in v1.2.0

func SignParam() func(*Signer) error

SignParam includes body in the signature

func WithNowFunc added in v1.2.0

func WithNowFunc(nowFunc NowFunc) func(*Signer) error

WithNowFunc uses the nowFunc as the source of time

Types

type NowFunc

type NowFunc func() time.Time

NowFunc is a time source

type Signer

type Signer struct {
	// contains filtered or unexported fields
}

Signer holds the configuration of a signer instance

func New

func New(sharedKey, sharedSecret string, options ...func(*Signer) error) (*Signer, error)

New creates an instance of Signer

func NewWithPrefixAndNowFunc

func NewWithPrefixAndNowFunc(sharedKey, sharedSecret, prefix string, nowFunc NowFunc) (*Signer, error)

NewWithPrefixAndNowFunc create na instance of Signer, taking prefix and nowFunc as additional parameters

func (*Signer) SignRequest

func (s *Signer) SignRequest(request *http.Request, withHeaders ...string) error

SignRequest signs a http.Request by adding an Authorization and SignedDate header

func (*Signer) ValidateRequest

func (s *Signer) ValidateRequest(request *http.Request) (bool, error)

ValidateRequest validates a previously signed request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL