tokenmanager

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Tokenmanager

Control flow token generate and parse token.

Go Go Reference

Installing and Usage

go get github.com/teng231/tokenmanager

struct of claims example:

{
  "exp": 1632109978,
  "iat": 1632108178,
  "payload": {
    "user_id": 1,
    "role_id": 0
  }
}

all data store in payload.

Alg RSA

Gen demo rsa keygen

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub
  • Genkey
func GenKey() {
	mgr, err := CreateRSAToken("", "./test/jwtRS256.key")
	if err != nil {
		t.Fail()
	}
	tk, err := mgr.GenerateToken(Claims{UserId: 1}, 30*time.Minute)
	if err != nil {
		t.Fail()
	}
	log.Println(tk)
}

  • Parse
func ParseKey(tk string) {
	mgr, err := CreateRSAToken("./test/jwtRS256.key.pub", "")
	if err != nil {
		t.Fail()
	}
	extractor, err := mgr.ParseToken(tk)
	if err != nil {
		t.Fail()
	}
	bin, err := json.Marshal(extractor["payload"])
	if err != nil {
		t.Fail()
	}
	claim := &Claims{}
	json.Unmarshal(bin, claim)
	log.Println(claim)
	if claim.UserId != 1 {
		t.Fail()
	}
}
Alg HMAC

Hmac input need a secret path like: ./test/secret

  • Genkey
func Genkey() {
	mgr, err := CreateHMACToken("./test/secret")
	if err != nil {
		t.Fail()
	}
	tk, err := mgr.GenerateHmacToken(Claims{UserId: 1}, 30*time.Second)
	if err != nil {
		log.Print(err)
		t.Fail()
	}
	log.Println(tk)
}
  • ParseKey
func Genkey(tk string) {
	mgr, err := CreateHMACToken("./test/secret")
	if err != nil {
		log.Print(err)
		t.Fail()
	}
	extractor, err := mgr.ParseHmacToken(tk)
	if err != nil {
		log.Print(err)
		t.Fail()
	}
	bin, err := json.Marshal(extractor["payload"])
	if err != nil {
		t.Fail()
	}
	claim := &Claims{}
	json.Unmarshal(bin, claim)
	log.Println(claim)
	if claim.UserId != 1 {
		t.Fail()
	}
}

Documentation

Index

Constants

View Source
const (
	E_token_expired           = "token_expired"
	E_not_found_secret        = "not_found_secret"
	E_claims_not_valid        = "claims_not_valid"
	E_token_invalid           = "token_invalid"
	E_token_fail_to_valid     = "token_fail_to_valid"
	E_token_invalid_not_found = "token_invalid_not_found"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HMACToken

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

Mgr create method for working jwt

func CreateHMACToken

func CreateHMACToken(secret string) (*HMACToken, error)

CreateHMACToken is parse pub, priv key then init HMACToken

func (*HMACToken) GenerateHmacToken

func (t *HMACToken) GenerateHmacToken(payload interface{}, duration time.Duration) (string, error)

GenerateHmacToken is create token with secret

func (*HMACToken) ParseHmacToken

func (t *HMACToken) ParseHmacToken(tk string) (map[string]interface{}, error)

ParseHmacToken is verify token string is valid: input is secret

func (*HMACToken) SetSecret

func (t *HMACToken) SetSecret(path string) error

SetSecret is push privatekey to Helper

type RSAToken

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

func CreateRSAToken

func CreateRSAToken(pub, priv string) (*RSAToken, error)

CreateRSAToken is parse pub, priv key then init RSAToken

func (*RSAToken) GenerateToken

func (t *RSAToken) GenerateToken(payload interface{}, duration time.Duration) (string, error)

GenerateToken is create token with privatekey

func (*RSAToken) ParseToken

func (t *RSAToken) ParseToken(tk string) (map[string]interface{}, error)

ParseToken is verify token string is valid: input is public key

func (*RSAToken) SetPrivateKey

func (t *RSAToken) SetPrivateKey(path string) error

SetPrivateKey is push privatekey to Helper

func (*RSAToken) SetPublicKey

func (t *RSAToken) SetPublicKey(path string) error

SetPublicKey push publickey to Helper

Jump to

Keyboard shortcuts

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