# Golang JWT middleware example: adding validation results to context

**URL:** <https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540>\
**Category:** JWT.io\
**Tags:** go, api\
**Created:** [September 18, 2023, 9:35pm UTC](https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540 "2023-09-18T21:35:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbonnet](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mbonnet](https://community.auth0.com/u/mbonnet)\
**Post date:** [September 18, 2023, 9:35pm UTC](https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540/1 "2023-09-18T21:35:08Z")

</div>

I am looking at the [example](https://github.com/auth0-samples/auth0-golang-api-samples/tree/master/01-Authorization-RS256) for JWT middleware in Go.

In my API backend as-is, I use the Echo framework (specifically, based off this [example](https://github.com/zett-8/go-clean-echo/tree/master)). This is no big issue itself, as Echo has a very useful `echo.WrapMiddleware()` function for turning regular bare HTTP handler middlewares into Echo ones. I literally just add the following in `jwt.go`:

```auto
func EchoEnsureValidToken() (echo.MiddlewareFunc, error) {
	mw := echo.WrapMiddleware(EnsureValidToken())
	return mw, nil
}

```

and then use it in my app as per normal. However, I’m running into an issue with my handler functions.

main.go:

```auto
package main

import (
	"log"
	"os"

	database "tlmgateway/db"
	_ "tlmgateway/docs"
	"tlmgateway/handlers"
	"tlmgateway/logger"
	"tlmgateway/middlewares"

	"tlmgateway/services"
	"tlmgateway/stores"

	"go.uber.org/zap"
)

var GO_ENV = os.Getenv("GO_ENV")

// @title Go clean echo API v1
// @version 1.0
// @description This is a sample server.
// @termsOfService http://swagger.io/terms/

// @host localhost:3000
// @BasePath /
// @schemes http
func main() {
	err := logger.New()
	if err != nil {
		log.Fatal(err)
	}

	db, err := database.New(GO_ENV == "development")
	if err != nil {
		logger.Fatal("failed to connect to the database", zap.Error(err))
	}

	e := handlers.Echo()

	s := stores.New(db)
	ss := services.New(s)
	h := handlers.New(ss)

	jwtCheck, err := middlewares.EchoEnsureValidToken()
	if err != nil {
		logger.Fatal("failed to set JWT middleware", zap.Error(err))
	}

	handlers.SetDefault(e)
	handlers.SetApi(e, h, jwtCheck)

	PORT := os.Getenv("PORT")
	if PORT == "" {
		PORT = "3001"
	}

	logger.Fatal("failed to start server", zap.Error(e.Start(":"+PORT)))
}

```

But in Echo, the routing and handlers are done a bit differently:

```auto
package handlers

import (
	"net/http"
	"strings"

	"repo/services"
	"repo/utils"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	echoSwagger "github.com/swaggo/echo-swagger"
)

type Handlers struct {
	RecordHandler
}

func New(s *services.Services) *Handlers {
	return &Handlers{
		RecordHandler: &recordHandler{s.Record},
	}
}

func SetDefault(e *echo.Echo) {
	utils.SetHTMLTemplateRenderer(e)

	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "OK")
	})
	e.GET("/healthcheck", HealthCheckHandler)
	e.GET("/swagger/*", echoSwagger.WrapHandler)
}

func SetApi(e *echo.Echo, h *Handlers, m echo.MiddlewareFunc) {
	g := e.Group("/api/v1")
	g.Use(m)

	// Organizations
	g.GET("/record", h.RecordHandler.GetRecords)
}

func Echo() *echo.Echo {
	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Pre(middleware.RemoveTrailingSlash())
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
	}))
	e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
		Skipper: func(c echo.Context) bool {
			return strings.Contains(c.Request().URL.Path, "swagger")
		},
	}))

	return e
}

```

My issue is, these handlers simply take the Context and go from there. They’re defined like this:

`GetRecords(c echo.Context) error`

The question then is, how do I add something like ValidatedClaims and CustomClaims from the middleware function in the example to the context so that it can be used where I need to use it? The above code is more for illustration purposes, since the only implementation issue I’m having is “add validation results to context”.

---

<div class="post-metadata">

**Author:** ![mbonnet](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@mbonnet](https://community.auth0.com/u/mbonnet)\
**Post date:** [September 19, 2023, 5:34pm UTC](https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540/2 "2023-09-19T17:34:46Z")

</div>

OP here. The solution is that the stated problem doesn’t actually exist, I was misunderstanding the flow of information.

using the `middlewares.EchoEnsureValidToken()` middleware in fact adds information to the `Request()` part of the `echo.Context` type that is then used in the handler functions.

You access the Request with `c.Request()`. To get the claims specifically, within the `middlewares` package:

`claims := c.Request().Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims).CustomClaims.(*CustomClaims)`

If you want to access those types from outside the `middlewares` package, you have to import `middlewares` and use it as a specifier for the `CustomClaims` type:

`claims := c.Request().Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims).CustomClaims(*middlewares.CustomClaims)`

Hope this helps anyone else running into the same brain teaser I did.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 19, 2023, 9:32pm UTC](https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540/3 "2023-09-19T21:32:12Z")

</div>

Thanks for following up with your solution!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 3, 2023, 9:33pm UTC](https://community.auth0.com/t/golang-jwt-middleware-example-adding-validation-results-to-context/116540/4 "2023-10-03T21:33:02Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
