dangerous

package module
v0.0.0-...-70577de Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: BSD-3-Clause Imports: 16 Imported by: 0

README

itsdangerous-go

Build Status codecov Go Report Card

itsdangerous-go is coded according to itsdangerous. Due to golang's inheritance rule and it is strongly typed language, so itsdangerous-go does not have the identical API to origin version. But itdangerous-go can do what itsdangerous did, for that reason you can try to do something that you did in python.

Installing

go get -u github.com/kcorlidy/itsdangerous-go

A Simple Example

package main

import (
	"fmt"
	"github.com/kcorlidy/dangerous"
)

func main() {
    data := map[string]interface{}{"id": 5, "name": "itsdangerous"}
    // Signer's default digest method is sha256
	ser := dangerous.Serializer{Secret: "secret key", Salt: "auth"}
	result, _ := ser.URLSafeDumps(data)
	fmt.Println(string(result))
	fmt.Println(ser.URLSafeLoads(string(result)))
}
Equal to python code
from itsdangerous import URLSafeSerializer
import hashlib
#  Signer's default digest method is sha1
auth_s = URLSafeSerializer("secret key", "auth",signer_kwargs={"digest_method": hashlib.sha256})
token = auth_s.dumps({"id": 5, "name": "itsdangerous"})

print(token)
# eyJpZCI6NSwibmFtZSI6Iml0c2Rhbmdlcm91cyJ9.sMTJJoYOuSliwOpT81Z_Ql4OIPQacePyoz79f3x_MEo

data = auth_s.loads(token)
print(data["name"])

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JwsAlgorithms = map[string]interface{}{
		"HS256": HMACAlgorithm{DigestMethod: sha256.New},
		"HS384": HMACAlgorithm{DigestMethod: sha512.New384},
		"HS512": HMACAlgorithm{DigestMethod: sha512.New},
		"none":  SigningAlgorithm{},
	}

	DefaultAlgorithm = "HS512"

	DefaultSerializer = JSON{}

	DefaultExpiresIn int64 = 3600
)
View Source
var (
	Sep                    = []byte(".")
	DefaultFallbackSigners = []map[string]interface{}{{"DigestMethod": sha512.New}}
)
View Source
var (
	BlankBytes = []byte("")
	DefaultSep = "."
)
View Source
var Base64Alphabet = WantBytes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=")

Functions

func ApplyKwargs

func ApplyKwargs(struct1 interface{}, kwargs map[string]interface{}) error

func B64decode

func B64decode(encoded []byte) ([]byte, error)

func B64encode

func B64encode(msg []byte) string

func ByteCompare

func ByteCompare(a, b []byte) bool

func Bytes2Int

func Bytes2Int(_byte []byte) (_int int64)

func Compress

func Compress(src []byte) []byte

func Concentrate

func Concentrate(b ...interface{}) ([]byte, error)

func DumpPayload

func DumpPayload(vx interface{}, api interface{}) (string, error)

func Int2Bytes

func Int2Bytes(_int int64) (bs []byte)

func IsValidStruct

func IsValidStruct(t interface{}) bool

func LoadPayload

func LoadPayload(payload []byte, api interface{}) (interface{}, error)

func PreURLSafeDumpPayload

func PreURLSafeDumpPayload(JSONPayload []byte) ([]byte, error)

func PreURLSafeLoadPayload

func PreURLSafeLoadPayload(payload []byte) ([]byte, error)

func RSplit

func RSplit(b, sep []byte) ([]byte, []byte)

func URLSafeDumpPayload

func URLSafeDumpPayload(obj interface{}, api interface{}) (string, error)

func URLSafeLoadPayload

func URLSafeLoadPayload(payload []byte, api interface{}) (interface{}, error)

func UnCompress

func UnCompress(data []byte) ([]byte, error)

func WantBytes

func WantBytes(str string, chartype ...interface{}) []byte

Types

type HMACAlgorithm

type HMACAlgorithm struct {
	DigestMethod func() hash.Hash
}

func (HMACAlgorithm) GetSignature

func (ha HMACAlgorithm) GetSignature(key, value []byte) []byte

func (HMACAlgorithm) VerifySignature

func (ha HMACAlgorithm) VerifySignature(key, value, sig []byte) bool

type JSON

type JSON struct {
}

JSON is a empty struct, just for applying

func (JSON) Dump

func (js JSON) Dump(v interface{}) (string, error)

Dump is equal to json.Marshal

func (JSON) Load

func (js JSON) Load(data []byte) (interface{}, error)

Load is totally equal to json.Unmarshal

type JSONAPI

type JSONAPI interface {
	Load(data []byte) (interface{}, error)
	Dump(v interface{}) (string, error)
}

JSONAPI used to solve the problem that applying new struct to `serializer` or `jws`

type JSONWebSignatureSerializer

type JSONWebSignatureSerializer struct {
	Secret        string
	Salt          string
	Serializer    JSONAPI
	Signer        Signer
	AlgorithmName string
	Algorithm     Signature
	ExpiresIn     int64
}

func (JSONWebSignatureSerializer) DumpPayload

func (jwss JSONWebSignatureSerializer) DumpPayload(header, obj interface{}) ([]byte, error)

func (JSONWebSignatureSerializer) Dumps

func (jwss JSONWebSignatureSerializer) Dumps(obj interface{}, args ...interface{}) ([]byte, error)

func (JSONWebSignatureSerializer) GetIssueDate

func (jwss JSONWebSignatureSerializer) GetIssueDate(t int64) string

func (JSONWebSignatureSerializer) LoadPayload

func (jwss JSONWebSignatureSerializer) LoadPayload(payload []byte) (interface{}, interface{}, error)

func (JSONWebSignatureSerializer) Loads

func (jwss JSONWebSignatureSerializer) Loads(s string) (interface{}, interface{}, error)

func (JSONWebSignatureSerializer) MakeHeader

func (jwss JSONWebSignatureSerializer) MakeHeader(headerfields map[string]interface{}) map[string]interface{}

func (JSONWebSignatureSerializer) MakeSigner

func (jwss JSONWebSignatureSerializer) MakeSigner() Signer

func (*JSONWebSignatureSerializer) SetDefault

func (jwss *JSONWebSignatureSerializer) SetDefault()

func (JSONWebSignatureSerializer) TimedDumps

func (jwss JSONWebSignatureSerializer) TimedDumps(obj interface{}, args ...interface{}) ([]byte, error)

func (JSONWebSignatureSerializer) TimedLoads

func (jwss JSONWebSignatureSerializer) TimedLoads(s string) (map[string]interface{}, interface{}, error)

func (JSONWebSignatureSerializer) TimedMakeHeader

func (jwss JSONWebSignatureSerializer) TimedMakeHeader(headerfields map[string]interface{}) map[string]interface{}

type Serializer

type Serializer struct {
	Secret          string
	Salt            string
	SerializerOP    JSONAPI // Can override it becomes easier
	Signer          Signer
	Signerkwargs    map[string]interface{}
	FallbackSigners []map[string]interface{}
}

func (Serializer) Dumps

func (ser Serializer) Dumps(objx interface{}) ([]byte, error)

func (Serializer) IterUnSigners

func (ser Serializer) IterUnSigners() []interface{}

func (Serializer) Loads

func (ser Serializer) Loads(s string) (interface{}, error)

func (Serializer) PreDumps

func (ser Serializer) PreDumps(objx interface{}, dumpfunc func(interface{}, interface{}) (string, error)) ([]byte, error)

func (Serializer) PreLoads

func (ser Serializer) PreLoads(s string, loadfunc func([]byte, interface{}) (interface{}, error)) (interface{}, error)

func (Serializer) PreTimedDumps

func (ser Serializer) PreTimedDumps(objx interface{}, dumpfunc func(interface{}, interface{}) (string, error)) ([]byte, error)

func (Serializer) PreTimedLoads

func (ser Serializer) PreTimedLoads(s string, MaxAge int64, loadfunc func([]byte, interface{}) (interface{}, error)) (interface{}, error)

func (*Serializer) SetDefault

func (ser *Serializer) SetDefault()

func (Serializer) TimedDumps

func (ser Serializer) TimedDumps(objx interface{}) ([]byte, error)

func (Serializer) TimedLoads

func (ser Serializer) TimedLoads(s string, MaxAge int64) (interface{}, error)

func (Serializer) URLSafeDumps

func (ser Serializer) URLSafeDumps(objx interface{}) ([]byte, error)

func (Serializer) URLSafeLoads

func (ser Serializer) URLSafeLoads(s string) (interface{}, error)

func (Serializer) URLSafeTimedDumps

func (ser Serializer) URLSafeTimedDumps(objx interface{}) ([]byte, error)

func (Serializer) URLSafeTimedLoads

func (ser Serializer) URLSafeTimedLoads(s string, MaxAge int64) (interface{}, error)

type Signature

type Signature interface {
	GetSignature(key, value []byte) []byte
	VerifySignature(key, value, sig []byte) bool
}

type Signer

type Signer struct {
	Secret        string
	Salt          string
	Sep           string
	SecretBytes   []byte
	SaltBytes     []byte
	SepBytes      []byte
	KeyDerivation string // concat, django-concat, hmac
	DigestMethod  func() hash.Hash
	Algorithm     Signature // HMACAlgorithm, NoneAlgorithm
}

func (*Signer) DeriveKey

func (signer *Signer) DeriveKey() ([]byte, error)

func (Signer) GetSignature

func (signer Signer) GetSignature(value []byte) []byte

func (Signer) GetTimestamp

func (signer Signer) GetTimestamp() int64

func (*Signer) SetDefault

func (signer *Signer) SetDefault()

func (Signer) Sign

func (signer Signer) Sign(value string) []byte

func (Signer) SignTimestamp

func (signer Signer) SignTimestamp(values string) []byte

func (Signer) UnSign

func (signer Signer) UnSign(signedvalues string) ([]byte, error)

func (Signer) UnSignTimestamp

func (signer Signer) UnSignTimestamp(values string, MaxAge int64) ([]byte, int64, error)

func (Signer) Validate

func (signer Signer) Validate(signedvalues string) bool

func (Signer) ValidateTimestamp

func (signer Signer) ValidateTimestamp(signedvalue string, MaxAge int64) bool

func (Signer) VerifySignature

func (signer Signer) VerifySignature(value []byte, sig []byte) bool

type SigningAlgorithm

type SigningAlgorithm struct {
}

func (SigningAlgorithm) GetSignature

func (sa SigningAlgorithm) GetSignature(key, value []byte) []byte

func (SigningAlgorithm) VerifySignature

func (sa SigningAlgorithm) VerifySignature(key, value, sig []byte) bool

Jump to

Keyboard shortcuts

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