jwt

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jwt contains functions required for JWT signing and validation.

Currently, only PS512 algorithm is supported, more methods will be added in future releases.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	Dat interface{} `json:"dat,omitempty"`
	*jwt.StandardClaims
}

Claims represents a custom claim where the dat section is used for custom data.

TODO: make this generic in 2.0

func NewClaims added in v1.2.0

func NewClaims() *Claims

NewClaims creates a new instance of the custom JWT claims.

TODO: make this generic in 2.0

func (*Claims) WithDat added in v1.2.0

func (c *Claims) WithDat(dat interface{}) *Claims

WithDat adds a dat claim to the JWT token.

TODO: make this generic in 2.0

Example
package main

import (
	"fmt"

	"github.com/qqiao/webapp/jwt"
)

func main() {
	claims := jwt.NewClaims().WithDat("123")

	fmt.Println(claims.Dat)

}
Output:

123

func (*Claims) WithExpiry added in v1.2.0

func (c *Claims) WithExpiry(expiry time.Time) *Claims

WithExpiry updates the expiry of the JWT token to the time specified.

Example
package main

import (
	"fmt"
	"time"

	"github.com/qqiao/webapp/jwt"
)

func main() {
	now := time.Unix(0, 0).Add(1 * time.Hour)
	claims := jwt.NewClaims().WithExpiry(now)
	fmt.Printf("%d", claims.ExpiresAt)

}
Output:

3600

type Manager

type Manager interface {
	// Alg returns the signing algorithm supported by the current manager
	// instance.
	Alg() string

	// ParseCustom parses a JWT token with the claims and returns the claims of
	// the token.
	ParseCustom(token string) (<-chan *Claims, <-chan error)

	// SignCustom signs the JWT token with the given claims.
	SignCustom(claims *Claims) (<-chan string, <-chan error)
}

Manager is responsible for all the JWT token related operations.

type PS512Manager added in v1.8.0

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

PS512Manager is responsible for creating and validating JWT tokens using PS512 algorithm.

Given that validating JWT comes with a hefty cost, internally, the manager caches already validated tokens, so if the same token is validated repeatedly, cached results will be returned.

func NewPS512Manager added in v1.2.0

func NewPS512Manager(publicKey *rsa.PublicKey, privateKey *rsa.PrivateKey) *PS512Manager

NewPS512Manager creates a new JWT client that signs and validates JWT tokens using the PS512 algorithm.

Example
package main

import (
	"crypto/rsa"
	"fmt"

	"github.com/qqiao/webapp/jwt"
)

func main() {
	// In real program usage, the private and public key pair has to be real
	// and should be dummy ones like this
	privateKey := &rsa.PrivateKey{}
	publicKey := &rsa.PublicKey{}

	manager := jwt.NewPS512Manager(publicKey, privateKey)
	fmt.Println(manager.Alg())

}
Output:

PS512

func (*PS512Manager) Alg added in v1.8.0

func (m *PS512Manager) Alg() string

Alg returns the signing algorithm supported by the current manager instance.

func (*PS512Manager) ParseCustom added in v1.8.0

func (m *PS512Manager) ParseCustom(token string) (<-chan *Claims,
	<-chan error)

ParseCustom parses a JWT token with the claims and returns the claims of the token.

TODO: make this generic in 2.0

func (*PS512Manager) SignCustom added in v1.8.0

func (m *PS512Manager) SignCustom(claims *Claims) (<-chan string,
	<-chan error)

SignCustom signs the JWT token with the given claims.

TODO: make this generic in 2.0

Jump to

Keyboard shortcuts

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