parsing - Invalid Json web token in Go -
i trying make json web token authentication system go cant seem parsing of web token working. error occurs in following function.
func requiretokenauthentication(rw http.responsewriter, req *http.request, next http.handlerfunc) { authbackend := initjwtauthenticationbackend() jwtstring := req.header.get("authorization") token, err := jwt.parse(jwtstring, func(token *jwt.token) (interface{}, error) { if _, ok := token.method.(*jwt.signingmethodrsa); !ok { log.println("unexpected signing method") return nil, fmt.errorf("unexpected signing method: %v", token.header["alg"]) } else { log.println("the token has been returned") return authbackend.publickey, nil } }) log.println(token) log.println(token.valid) if err == nil && token.valid && !authbackend.isinblacklist(req.header.get("authorization")) { next(rw, req) } else { rw.writeheader(http.statusunauthorized) log.p rintln("status unauthorized requiretokenauthentication") } }
returns following log
[negroni] started /test/hello 2016/09/13 01:34:46 &{bearer eyjhbgcioijsuzuxmiisinr5cci6ikpxvcj9.eyjlehaioje0nzm5nzq4otasimlhdci6mtq3mzcxnty5mcwic3viijoiin0.mnwewdr8nuvdlo_4ie43me7iph2lesj1uikokgd6vjb7isjfpshn8e7eqr4gkwuiilti34_i6ijrpmx9qrpugkzvsoxx44qlfi6m7fdhvysriybbqwtcvkcpvhnsk8bhjyegy813aaxomk6skzjoaks5jyuvnnzdnqmenyj1bm6fdbgp-olhur_cjk0pym1nmhv9zli1rpjogu4mfj1t4thyzaegirpnzymamtrk6tyefe6xi4voeeadq7hxvwreg6wnsqsygww8uoaiwly1ylbhtkpmt8zfrwllylqs_uuz0xiaswo1mf2plvozz1wlf3zehls31t1egb1xl4wtnqe <nil> map[] <nil> false} 2016/09/13 01:34:46 false 2016/09/13 01:34:46 status unauthorized requiretokenauthentication [negroni] completed 401 unauthorized in 71.628ms
and here curl using initiate it
curl -h "authorization: bearer eyjhbgcioijsuzuxmiisinr5cci6ikpxvcj9.eyjlehaioje0nzm5nzq4otasimlhdci6mtq3mzcxnty5mcwic3viijoiin0.mnwewdr8nuvdlo_4ie43me7iph2lesj1uikokgd6vjb7isjfpshn8e7eqr4gkwuiilti34_i6ijrpmx9qrpugkzvsoxx44qlfi6m7fdhvysriybbqwtcvkcpvhnsk8bhjyegy813aaxomk6skzjoaks5jyuvnnzdnqmenyj1bm6fdbgp-olhur_cjk0pym1nmhv9zli1rpjogu4mfj1t4thyzaegirpnzymamtrk6tyefe6xi4voeeadq7hxvwreg6wnsqsygww8uoaiwly1ylbhtkpmt8zfrwllylqs_uuz0xiaswo1mf2plvozz1wlf3zehls31t1egb1xl4wtnqe" http://localhost:5000/test/hello
i have tried curl without bearer
curl -h "authorization:eyjhbgcioijsuzuxmiisinr5cci6ikpxvcj9.eyjlehaioje0nzm5nzq4otasimlhdci6mtq3mzcxnty5mcwic3viijoiin0.mnwewdr8nuvdlo_4ie43me7iph2lesj1uikokgd6vjb7isjfpshn8e7eqr4gkwuiilti34_i6ijrpmx9qrpugkzvsoxx44qlfi6m7fdhvysriybbqwtcvkcpvhnsk8bhjyegy813aaxomk6skzjoaks5jyuvnnzdnqmenyj1bm6fdbgp-olhur_cjk0pym1nmhv9zli1rpjogu4mfj1t4thyzaegirpnzymamtrk6tyefe6xi4voeeadq7hxvwreg6wnsqsygww8uoaiwly1ylbhtkpmt8zfrwllylqs_uuz0xiaswo1mf2plvozz1wlf3zehls31t1egb1xl4wtnqe" http://localhost:5000/test/hello
the error occurring because token invalid token.valid = false
have generated using following process.
here router
router.handlefunc("/token-auth", controllers.login).methods("post")
here login controller
func login(w http.responsewriter, r *http.request) { requestuser := new(models.user) decoder := json.newdecoder(r.body) decoder.decode(&requestuser) responsestatus, token := utils.login(requestuser) //here util file seen below used w.header().set("content-type", "application/json") w.writeheader(responsestatus) w.write(token) }
this util file
func login(requestuser *models.user) (int, []byte) { authbackend := authentication.initjwtauthenticationbackend() if authbackend.authenticate(requestuser) { token, err := authbackend.generatetoken(requestuser.uuid) if err != nil { return http.statusinternalservererror, []byte("") } else { response, _ := json.marshal(parameters.tokenauthentication{token}) return http.statusok, response } } return http.statusunauthorized, []byte("") }
and here method used generate token
func (backend *jwtauthenticationbackend) generatetoken(useruuid string) (string, error) { token := jwt.new(jwt.signingmethodrs512) claims := token.claims.(jwt.mapclaims) claims["exp"] = time.now().add(time.hour * time.duration(settings.get().jwtexpirationdelta)).unix() claims["iat"] = time.now().unix() claims["sub"] = useruuid tokenstring, err := token.signedstring(backend.privatekey) if err != nil { panic(err) return "", err } return tokenstring, nil }
how fix token parsing system token valid? if need additional information more happy make edit respective information. thank
the error returned jwt.parse()
says
tokenstring should not contain 'bearer '
so if remove "bearer ":
jwtstring = strings.split(jwtstring, "bearer ")[1]
you bit further
the token has been returned
however there's new error:
key of invalid type
sorry it's not complete answer!
Comments
Post a Comment