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

jws: Decode ignores private claims #189

Closed
SamWhited opened this issue Jun 30, 2016 · 2 comments
Closed

jws: Decode ignores private claims #189

SamWhited opened this issue Jun 30, 2016 · 2 comments

Comments

@SamWhited
Copy link
Member

SamWhited commented Jun 30, 2016

jws.Encode properly encodes private claims, however, jws.Decode ignores private claims and the resulting structure only contains the standard claims. It should also populate the "Private Claims" field.

The following modification to TestSignAndVerify will currently fail:

func TestSignAndVerify(t *testing.T) {
    header := &Header{
        Algorithm: "RS256",
        Typ:       "JWT",
    }
    payload := &ClaimSet{
        Iss: "http://google.com/",
        Aud: "",
        Exp: 3610,
        Iat: 10,
        PrivateClaims: map[string]interface{}{
            "priv": 1.0,
        },
    }

    privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
    if err != nil {
        t.Fatal(err)
    }

    token, err := Encode(header, payload, privateKey)
    if err != nil {
        t.Fatal(err)
    }

    claims, err := Decode(token)
    if err != nil {
        t.Fatal(err)
    }

    priv, ok := claims.PrivateClaims["priv"]
    if !ok || priv.(float64) != payload.PrivateClaims["priv"] {
        t.Fatal("Private claims did not decode")
    }

    err = Verify(token, &privateKey.PublicKey)
    if err != nil {
        t.Fatal(err)
    }
}

EDIT: Since there doesn't appear to be an easy way to fix this issue in the jws package with encoding/json, I've temporarily vendored an inefficient patch in my code that decodes the JWS twice: once into the claims struct, and once into the PirvateClaims map and then deletes the standard claims from the private claims map. Obviously this is not ideal.

@ramoas
Copy link

ramoas commented Jul 21, 2016

@SamWhited Yes, reuse of the existing types is currently difficult if you need customization. I recently raised Issue #193 to request extensibility of Header along the lines of ClaimSet.PrivateClaims. Hopefully, some more consideration for extensibility for both types will be provided so you're not forced into vendoring or outright code duplication.

@SamWhited
Copy link
Member Author

Closing since the JWS package is now deprecated and there are several duplicate issues.

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

2 participants