https://github.com/rafaelsouzaribeiro/jwt-auth
JWT with gRPC, HTTP, and Gin auth module in Golang
https://github.com/rafaelsouzaribeiro/jwt-auth
gin-gonic golang grpc http jwt-authentication
Last synced: 6 months ago
JSON representation
JWT with gRPC, HTTP, and Gin auth module in Golang
- Host: GitHub
- URL: https://github.com/rafaelsouzaribeiro/jwt-auth
- Owner: rafaelsouzaribeiro
- Created: 2024-04-09T18:32:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-10T06:25:30.000Z (12 months ago)
- Last Synced: 2025-04-10T07:33:36.131Z (12 months ago)
- Topics: gin-gonic, golang, grpc, http, jwt-authentication
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Version: v1.20.0
Adding Bearer authentication with Echo
```go
e := echo.New()
cre, err := middleware.NewCredential(3600, "secretkey", nil)
if err != nil {
panic(err)
}
e.GET("/protected", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "You are authorized!"})
}, cre.AuthMiddlewareEcho)
e.Logger.Fatal(e.Start(":1323"))
```
Version: v1.19.0
Adding Bearer authentication with Gin
```go
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/rafaelsouzaribeiro/jwt-auth/pkg/middleware"
)
router := gin.Default()
cre, err := middleware.NewCredential(3600, "secretkey", nil)
if err != nil {
panic(err)
}
router.POST("/publish", cre.AuthMiddlewareGin(), func(c *gin.Context) {
var json struct {
Message string `json:"message"`
}
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": json.Message,
})
})
router.Run(":8080")
```
Version: v1.15.0 or latest
Updating folder: pkg/middleware
```go
import jwtauth "github.com/rafaelsouzaribeiro/jwt-auth/pkg/middleware"
```
Version: v1.14.0 or latest
ExtractClaims method
```go
cre, err := jwtauth.NewCredential(3600, "secretkey", nil)
if err != nil {
panic(err)
}
claims, err = cre.ExtractClaims(token)
if err != nil {
panic(err)
}
println(fmt.Sprint(claims["lastname"]), fmt.Sprint(claims["firstname"]))
```
CreateToken
```go
// pass multiple data
claims := map[string]interface{}{
"username": username,
// ... other claims
}
token, err := cre.CreateToken(claims)
```
Methods for http getting the bearer token and validating
```go
package main
import (
"fmt"
"net/http"
jwtauth "github.com/rafaelsouzaribeiro/jwt-auth"
)
func main() {
mx := http.NewServeMux()
cre, err := jwtauth.NewCredential(3600, "secretkey", nil)
if err != nil {
panic(err)
}
// Protected routes
mx.HandleFunc("/route1", cre.AuthMiddleware(rota1Handler))
mx.HandleFunc("/route2", cre.AuthMiddleware(rota2Handler))
// Public route
mx.HandleFunc("/public-route", rotaPublicaHandler)
http.ListenAndServe(":8080", mx)
}
func rota1Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Token-protected Route 1")
}
func rota2Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Token-protected Route 2")
}
func rotaPublicaHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Public route accessible without token")
}
```
How to add an interceptor in grpc?
This example takes Bearer Token Authentication and skips token validation for functions login,loginAdm
```go
c, errs := jwtauth.NewCredential(3600, secretkey, []string{"login", "loginAdm"})
if err != nil {
panic(errs)
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(c.UnaryInterceptorBearer),
grpc.StreamInterceptor(c.StreamInterceptorBearer),
)
```
How to add an interceptor in grpc? passing the token as a parameter
```go
cre, err := jwtauth.NewCredential(3600, secretkey, nil)
if err != nil {
return "", err
}
claims := map[string]interface{}{
"username": username,
// ... other claims
}
token, err := cre.CreateToken(claims)
if err != nil {
return "", err
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(c.JwtUnaryInterceptor(token)),
grpc.StreamInterceptor(c.JwtStreamInterceptor(token)),
)
```