// iris provides some basic middleware, most for your learning curve. // You can use any net/http compatible middleware with iris.FromStd wrapper. // // JWT net/http video tutorial for golang newcomers: https://www.youtube.com/watch?v=dgJFeqeXVKw // // This middleware is the only one cloned from external source: https://github.com/auth0/go-jwt-middleware // (because it used "context" to define the user but we don't need that so a simple iris.FromStd wouldn't work as expected.) package main import ( "github.com/kataras/iris/v12" "github.com/iris-contrib/middleware/jwt" ) func getTokenHandler(ctx iris.Context) { token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "foo": "bar", }) // Sign and get the complete encoded token as a string using the secret tokenString, _ := token.SignedString([]byte("My Secret")) ctx.HTML(`Token: ` + tokenString + `

/secured?token=` + tokenString + ``) } func myAuthenticatedHandler(ctx iris.Context) { user := ctx.Values().Get("jwt").(*jwt.Token) ctx.Writef("This is an authenticated request\n") ctx.Writef("Claim content:\n") foobar := user.Claims.(jwt.MapClaims) for key, value := range foobar { ctx.Writef("%s = %s", key, value) } } func main() { app := iris.New() j := jwt.New(jwt.Config{ // Extract by "token" url parameter. Extractor: jwt.FromParameter("token"), ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) { return []byte("My Secret"), nil }, SigningMethod: jwt.SigningMethodHS256, }) app.Get("/", getTokenHandler) app.Get("/secured", j.Serve, myAuthenticatedHandler) app.Listen(":8080") }