matok

package module
v0.0.0-...-2362435 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: AGPL-3.0 Imports: 7 Imported by: 0

README

Matok = MAC Token

Somewhat like a JWT with hardcoded defaults. Not suitable for query passing. Fits nicely in Authorization header and cookies.

Usage

m := Matok.NewManager(secret)

Encode

// getToken.go
tokPool := bytebufferpool.Pool{}

...
buf := tokPool.Get()
defer tokPool.Put(buf)

tok := m.Encode(buf.B, userClaims)
ctx.Write(tok)

Decode

// middleware/auth.go
tok := ctx.Request.Header.Peek("Authorization")
if len(tok) < 7 {
	...
}
tok = tok[7:] // The "Bearer " thing
dat, ok := m.DecodeReuse(tok)
...

See https://gitlab.com/pgarin/matok/-/blob/master/main_test.go

Bench

goos: linux
goarch: amd64
pkg: gitlab.com/pgarin/matok
cpu: AMD Ryzen 5 3500X 6-Core Processor             
BenchmarkEncode
BenchmarkEncode-6        	 1000000	      1105 ns/op	     416 B/op	       2 allocs/op
BenchmarkDecodeReuse
BenchmarkDecodeReuse-6   	 1996802	       594.0 ns/op	      32 B/op	       1 allocs/op

JWT libraries I've seen do just about the same

BenchmarkHS256Signing-6   	 1599536	       721.1 ns/op	    1544 B/op	      32 allocs/op

Most of them don't include decode bench^

func BenchmarkJWTDecode(b *testing.B) {
	tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiUHlvdHIgR2FyaW4iLCJtb2RlIjoyNTV9.eP3J7iMNyWOv8DeU3jtL1aXNUWtV9ArXa_heivaNpEs"
	for i := 0; i < b.N; i++ {
		_, _ = jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
			return secret, nil
		})
	}
	b.ReportAllocs()
}
BenchmarkJWTDecode-6   	  127827	     10668 ns/op	    3376 B/op	      56 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

func NewManager

func NewManager(secret string) *Manager

func (*Manager) DecodeReuse

func (m *Manager) DecodeReuse(tok []byte) ([]byte, bool)

DecodeReuse verifies the given token against the singing secret and decodes its contents to the tok.Data array.

Requires single Init or InitBytes invocation to work.

func (*Manager) Encode

func (m *Manager) Encode(dst, dat []byte) []byte

Encode encodes ascii85(dat) + '~' + ascii85(sha256(dat + secret)) in the dst slice.

Requires single Init or InitBytes invocation to work.

Jump to

Keyboard shortcuts

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