jwt

package
v0.0.0-...-d8aa7a9 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: MIT Imports: 2 Imported by: 1

README

Package JWT

This package contains tools for JWT token creation and validation. Supports JWT token creation and validation using symmetric and asymmetric key.

JWT Creation Using Symmetric Key

	signKey := []byte("abcde")

	claims := UserClaim{}
	claims.UserID = 12345
	claims.SecondaryID = "8fae85be-e441-4344-8634-d41f23684146"
	claims.Scopes = []string{"read"}
	claims.ClientID = "apdifuoqpweyr9823u"
	claims.Id = "63410cd1-110b-4a2c-8c3f-ae1535eda9a1"
	claims.IssuedAt = time.Now().Unix()
	claims.ExpiresAt = time.Now().Add(48 * time.Hour).Unix()

	jwt := NewJWT(signKey)
    token, _ := jwt.Create(claims)
    fmt.Println(token)

JWT Creation Using Asymmetric Key

	claims := UserClaim{}
	claims.UserID = 12345
	claims.SecondaryID = "8fae85be-e441-4344-8634-d41f23684146"
	claims.Scopes = []string{"read"}
	claims.ClientID = "apdifuoqpweyr9823u"
	claims.Id = "63410cd1-110b-4a2c-8c3f-ae1535eda9a1"
	claims.IssuedAt = time.Now().Unix()
	claims.ExpiresAt = time.Now().Add(24 * time.Hour).Unix()

	pubKey, _ := ioutil.ReadFile("/path/to/app.rsa.pub")
	privKey, _ := ioutil.ReadFile("/path/to/app.rsa")

	jwt, _ := NewJWTRSA(pubKey, privKey)

    token, _ := jwt.Create(claims)
    fmt.Println(token)

For public and private key generation, you can use this openssl command:

# generate private key
openssl genrsa -out app.rsa 2048

# generate public key using private key
openssl rsa -in app.rsa -pubout > app.rsa.pub

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JWT

type JWT struct {
	SignKey []byte
}

func NewJWT

func NewJWT(signKey []byte) *JWT

func (*JWT) Create

func (j *JWT) Create(claims jwt.Claims) (tokenString string, err error)

func (*JWT) Parse

func (j *JWT) Parse(token string) (tkn *jwt.Token, err error)

func (*JWT) Valid

func (j *JWT) Valid(token string) (valid bool, err error)

type JWTRSA

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

func NewJWTRSA

func NewJWTRSA(pubKey []byte, privKey []byte) (jwtrsa *JWTRSA, err error)

func (*JWTRSA) Create

func (j *JWTRSA) Create(claims jwt.Claims) (tokenString string, err error)

func (*JWTRSA) Parse

func (j *JWTRSA) Parse(token string) (tkn *jwt.Token, err error)

type UserClaim

type UserClaim struct {
	UserID      int64    `json:"user_id"`
	SecondaryID string   `json:"secondary_id"`
	ClientID    string   `json:"client_id"`
	Scopes      []string `json:"scopes"`
	jwt.StandardClaims
}

UserClaim defines user token claim

Jump to

Keyboard shortcuts

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