go-chi / jwtauth

JWT authentication middleware for Go HTTP services
MIT License
550 stars 91 forks source link

Unable to retrieve token from context #15

Closed unitrq closed 7 years ago

unitrq commented 7 years ago

Casting token pointer value retrieved from request context fails for no obvious reason:


func accCtx(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        ctx := r.Context()
        val := ctx.Value("jwt")
        log.Printf("%#v\n", val)
        t, ok := val.(*jwt.Token)
        if !ok {
            errBadRequest(w)
            return
        }
        newctx := context.WithValue(ctx, ctxAccountID, t.Claims["account_id"])
        next.ServeHTTP(w, r.WithContext(newctx))
    })
}

The Printf call above displays something that looks like valid jwt.Token reference:


&jwt.Token{Raw:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50X2lkIjoiYTIxZjE4YjQtMGI0Mi00ZDdhLWJkNDAtMmI0ZTZkNGZlYTdjIiwidXNlcl9pZCI6ImQxMWQwOTZkLTc0N2ItNGYyNS05NjcxLWJhNzJjNjM3NTY0YSJ9.ERX3v2mVL_AQu3_nYj7e4xkeXse6VNKR_FcZm8qvQ-A", Method:(*jwt.SigningMethodHMAC)(0xc4200ce760), Header:map[string]interface {}{"alg":"HS256", "typ":"JWT"}, Claims:map[string]interface {}{"account_id":"a21f18b4-0b42-4d7a-bd40-2b4e6d4fea7c", "user_id":"d11d096d-747b-4f25-9671-ba72c637564a"}, Signature:"ERX3v2mVL_AQu3_nYj7e4xkeXse6VNKR_FcZm8qvQ-A", Valid:true}

However, casting that value to *jwt.Token fails.

VojtechVitek commented 7 years ago

Is this vendor/ issue, as in https://github.com/goware/jwtauth/issues/14 ?

unitrq commented 7 years ago

It is indeed, sorry for duplicating existing issue

VojtechVitek commented 7 years ago

No worries.