Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jwt: invalid token form #15

Open
ezewu opened this issue Mar 30, 2023 · 0 comments
Open

jwt: invalid token form #15

ezewu opened this issue Mar 30, 2023 · 0 comments

Comments

@ezewu
Copy link

ezewu commented Mar 30, 2023

package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/apps"
	"github.com/kataras/jwt"
	"github.com/rs/cors"
	"log"
	"time"
)

type UserClaims struct {
	ID       int    `json:"id"`
	Username string `json:"username"`
}

func main() {
	app := iris.New()

	c := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowCredentials: true,
		Debug:            false,
	})
	app.WrapRouter(c.ServeHTTP)

	app.Get("/a", func(ctx iris.Context) {
		var sharedKey = []byte("sercrethatmaycontainch@r$32chars")

		claims := UserClaims{
			ID:       123456,
			Username: "Username",
		}

		accessToken, tErr := jwt.Sign(jwt.HS256, sharedKey, claims, jwt.MaxAge(20000*time.Hour))
		if tErr != nil {
			log.Println("accessToken err")
		}
		refreshToken, rErr := jwt.Sign(jwt.HS256, sharedKey, claims, jwt.MaxAge(30000*time.Hour))
		if rErr != nil {
			log.Println("refreshToken err")
		}

		_ = ctx.JSON(iris.Map{
			"accessToken":  accessToken,
			"refreshToken": refreshToken,
		})
	})

	app.Get("/b", func(ctx iris.Context) {
		type RefreshToken struct {
			RefreshToken string `json:"refreshToken"`
		}
		var rt RefreshToken

		if err := ctx.ReadJSON(&rt); err != nil {
			apps.Get().Logger().Error("---------------- 读取参数失败", err.Error())
		}

		var sharedKey = []byte("sercrethatmaycontainch@r$32chars")

		verifiedToken, err := jwt.Verify(jwt.HS256, sharedKey, []byte(rt.RefreshToken))
		if err != nil {
			log.Println("1111111111111111111111111111111", err)
		} else {
			var u UserClaims
			err = verifiedToken.Claims(&u)
			log.Println(u.ID, " -------------------------- ", u.Username)
			log.Println("2222222222222222222222222222222")
		}
	})

	_ = app.Listen(":9000", nil)
}

Hello, is there a typo there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant