go-chi / jwtauth

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

Missing error handling when set claim fields #90

Closed weidonglian closed 2 months ago

weidonglian commented 5 months ago
func (ja *JWTAuth) Encode(claims map[string]interface{}) (t jwt.Token, tokenString string, err error) {
    t = jwt.New()
    for k, v := range claims {
        t.Set(k, v)
    }
    payload, err := ja.sign(t)
    if err != nil {
        return nil, "", err
    }
    tokenString = string(payload)
    return
}

If t.Set(k,v) returns error, we should handle it and error out earlier, instead of now just ignore it ? Look at the API t.Set(k, v), depending on the key's name, it may fail if we provide a wrong type for that key, e.g. the sub has to be string.