jwt

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 8 Imported by: 0

README

Jwt Issuer

This package provides jwt issuing and verification in "secret" or "rsa" way.

Installation

go get -u github.com/metadiv-io/jwt

Create a new jwt claims

Create new jwt claims:

j := jwt.New()

Set the claims:

j.Set("sub", "1234567890")
j.Set("name", "John Doe")
j.Set("admin", true)

Get the claims:

sub := j.Get("sub")
name := j.Get("name")
admin := j.Get("admin")

Issue a jwt token

Issue a jwt token with a secret and set the expiration time to 2 hours:

token, err := j.Encode("secret", time.Hour * 2)

Issue a jwt token with a rsa private key and set the expiration time to 2 hours:

token, err := j.EncodeByRSA(privateKey, time.Hour * 2)

Verify a jwt token

Verify a jwt token without a secret or a rsa public key:

j := jwt.UnverifiedParse(token)

Verify a jwt token with a secret:

j := jwt.Parse(token, "secret")

Verify a jwt token with a rsa public key:

j := jwt.ParseByRSA(token, publicKey)

RSA key generation utility

This package provides a utility to generate rsa private and public keys:

keyPair, err := jwt.CreateRSAKeyPair()
log.Println(keyPair.Private)
log.Println(keyPair.Public)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jwt added in v1.2.0

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

func New added in v1.2.0

func New() *Jwt

New jwt instance.

func Parse added in v1.2.0

func Parse(token string, secret string) *Jwt

Parse a jwt token.

func ParseByRSA added in v1.2.0

func ParseByRSA(token string, publicKey string) *Jwt

Parse a jwt token by RSA public key.

func UnverifiedParse added in v1.2.0

func UnverifiedParse(token string) *Jwt

Parse a token without secret or pem.

func (*Jwt) Encode added in v1.2.0

func (j *Jwt) Encode(secret string, expiredAfter time.Duration) (string, error)

Encode by secret.

func (*Jwt) EncodeByRSA added in v1.2.0

func (j *Jwt) EncodeByRSA(privateKey string, expiredAfter time.Duration) (string, error)

Encode by RSA private key.

func (*Jwt) Get added in v1.2.0

func (j *Jwt) Get(key string) any

Get a claim.

func (*Jwt) Set added in v1.2.0

func (j *Jwt) Set(key string, value any)

Set a claim.

type RSAKeyPair added in v1.2.0

type RSAKeyPair struct {
	Private string
	Public  string
}

func CreateRSAKeyPair

func CreateRSAKeyPair() (*RSAKeyPair, error)

Create a RSA key pair.

Jump to

Keyboard shortcuts

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