auth_jwt

package module
v0.0.0-...-8be5302 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 6 Imported by: 1

README

go-auth-jwt

GitHub go.mod Go version GitHub License

The go-auth-jwt package offers a streamlined approach for implementing JSON Web Tokens (JWT) authentication within Go applications. It's specifically tailored for use with the Gin web framework, simplifying the process of generating, validating, and invalidating JWTs.

Features

  • Easy generation of JWT tokens with custom payload.
  • Middleware integration for Gin for easy setup.
  • Support for token validation within HTTP requests.
  • Methods for invalidating tokens to handle logout scenarios.
  • Customizable token duration and secret key.

Structs

AuthJWT
  • SecretKey: The secret key used for signing tokens.
  • TokenDuration: The duration for which the token remains valid.
  • invalidatedTokens: A map to track invalidated tokens.
  • AuthType: The authorization type, typically "Bearer".

Functions

ApiMiddleware(name string, j *AuthJWT) gin.HandlerFunc

Middleware function that attaches an AuthJWT instance to the Gin context.

GenerateJWT(data interface{}) string

Creates a JWT token with the given payload. Returns the token or an empty string on failure.

IsAuthorized(r *http.Request) (interface{}, bool)

Validates the JWT token in the request's Authorization header, returning the payload and a boolean indicating if the token is valid.

IsAuthorizedToken(stoken string) (interface{}, bool)

Validates a given JWT token string, returning the payload and a validity boolean.

IsAuthorizedWithKey(r *http.Request, key string) (interface{}, bool)

Validates the request's token with a specified key, useful for multi-key setups.

InvalidateJWT(r *http.Request) bool

Invalidates the token found in the request's Authorization header.

InvalidateToken(utoken string) bool

Invalidates a specified token string.

Installation

To use go-auth-jwt in your project, run:

go get github.com/devcoons/go-auth-jwt

Getting Started

Import go-auth-jwt and other necessary packages:

import (
    "github.com/gin-gonic/gin"
    "github.com/devcoons/go-auth-jwt"
    "net/http"
    "time"
)

Initialize AuthJWT with your configuration:

auth := auth_jwt.AuthJWT{
    SecretKey:     "yourSecretKeyHere",
    TokenDuration: 24 * time.Hour,
    AuthType:      "Bearer",
}

Integrate with Gin by adding the middleware:

router.Use(auth_jwt.ApiMiddleware("jwt", &auth))

Examples

Generating a Token
token := auth.GenerateJWT(map[string]interface{}{"user": "exampleUser"})

Validating a Request
router.GET("/protected", func(c *gin.Context) {
    _, authorized := auth.IsAuthorized(c.Request)
    if !authorized {
        c.AbortWithStatus(http.StatusUnauthorized)
        return
    }
    // Your handler logic here
})

Invalidating a Token
router.POST("/logout", func(c *gin.Context) {
    if auth.InvalidateJWT(c.Request) {
        c.Status(http.StatusOK)
    } else {
        c.Status(http.StatusInternalServerError)
    }
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApiMiddleware

func ApiMiddleware(name string, j *AuthJWT) gin.HandlerFunc

Types

type AuthJWT

type AuthJWT struct {
	SecretKey     string
	TokenDuration time.Duration

	AuthType string
	// contains filtered or unexported fields
}

func (*AuthJWT) GenerateJWT

func (x *AuthJWT) GenerateJWT(data interface{}) string

func (*AuthJWT) InvalidateJWT

func (x *AuthJWT) InvalidateJWT(r *http.Request) bool

func (*AuthJWT) InvalidateToken

func (x *AuthJWT) InvalidateToken(utoken string) bool

func (*AuthJWT) IsAuthorized

func (x *AuthJWT) IsAuthorized(r *http.Request) (interface{}, bool)

func (*AuthJWT) IsAuthorizedToken

func (x *AuthJWT) IsAuthorizedToken(stoken string) (interface{}, bool)

func (*AuthJWT) IsAuthorizedWithKey

func (x *AuthJWT) IsAuthorizedWithKey(r *http.Request, key string) (interface{}, bool)

Jump to

Keyboard shortcuts

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