Go API Documentation

github.com/TykTechnologies/tyk/signature_validator

No package summary is available.

Package

Files: 2. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Types

Hasher

This type doesn't have documentation.

Field name Field type Comment
type

any

No comment on field.
type Hasher interface {
	Name() string
	Hash(token string, sharedSecret string, timeStamp int64) []byte
}

MasheryMd5sum

This type doesn't have documentation.

type MasheryMd5sum struct{}

MasherySha256Sum

This type doesn't have documentation.

type MasherySha256Sum struct{}

SignatureValidator

This type doesn't have documentation.

Field name Field type Comment
h

Hasher

No comment on field.
type SignatureValidator struct {
	h Hasher
}

Validator

This type doesn't have documentation.

Field name Field type Comment
type

any

No comment on field.
type Validator interface {
	Init(hasherName string) error
	Validate(attempt, actual string, allowedClockSkew int64) error
}

Functions

func (*SignatureValidator) Init

func (v *SignatureValidator) Init(hasherName string) error {
	switch hasherName {
	case "MasherySHA256":
		v.h = MasherySha256Sum{}
	case "MasheryMD5":
		v.h = MasheryMd5sum{}
	default:
		return errors.New(fmt.Sprintf("unsupported hasher type (%s)", hasherName))
	}

	return nil
}

Cognitive complexity: 6, Cyclomatic complexity: 4

Uses: errors.New, fmt.Sprintf.

func (MasheryMd5sum) Hash

func (m MasheryMd5sum) Hash(token string, sharedSecret string, timeStamp int64) []byte {
	signature := md5.Sum([]byte(token + sharedSecret + strconv.FormatInt(timeStamp, 10)))

	return signature[:]
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: md5.Sum, strconv.FormatInt.

func (MasheryMd5sum) Name

func (m MasheryMd5sum) Name() string {
	return "MasheryMD5"
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (MasherySha256Sum) Hash

func (m MasherySha256Sum) Hash(token string, sharedSecret string, timeStamp int64) []byte {
	signature := sha256.Sum256([]byte(token + sharedSecret + strconv.FormatInt(timeStamp, 10)))

	return signature[:]
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: sha256.Sum256, strconv.FormatInt.

func (MasherySha256Sum) Name

func (m MasherySha256Sum) Name() string {
	return "MasherySHA256"
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (SignatureValidator) Validate

func (v SignatureValidator) Validate(signature, key, secret string, allowedClockSkew int64) error {
	signatureBytes, _ := hex.DecodeString(signature)
	now := time.Now().Unix()
	for i := int64(0); i <= allowedClockSkew; i++ {
		if bytes.Equal(v.h.Hash(key, secret, now+i), signatureBytes) {
			return nil
		}

		if i == int64(0) {
			continue
		}

		if bytes.Equal(v.h.Hash(key, secret, now-i), signatureBytes) {
			return nil
		}
	}

	return errors.New("signature is not valid")
}

Cognitive complexity: 8, Cyclomatic complexity: 5

Uses: bytes.Equal, errors.New, hex.DecodeString, time.Now.

Tests

Files: 2. Third party imports: 0. Imports from organisation: 0. Tests: 3. Benchmarks: 1.

Constants

const (
	token		= "5bcef48a3f03d311ff27d156630baf849e3b438b8a48fec99239d5c9"
	sharedSecret	= "foobar"
	now		= 1546259837
)

Test functions

TestMasherySha256Sum_Hash

References: hex.EncodeToString.

TestValidateSignature_Init

References: errors.New.

TestValidateSignature_Validate

References: errors.New, hex.EncodeToString, test.Flaky, time.Now.

Benchmark functions

BenchmarkMasherySha256Sum_Hash

References: time.Now.