Skip to content

[v4] WithIssuer and WithAudience Errors Inconsistent #1488

@theory

Description

@theory

Describe the bug

The messages from WithIssuer and WithAudience are inconsistent. The one from WithIssuer is nice and descriptive:

jwt.Validate: validation failed: "iss" not satisfied: claim "iss" does not have the expected value

But the one from WithAudience is redundant and less informative:

jwt.Validate: validation failed: "aud" not satisfied: "aud" not satisfied

I suggest that the error message from WithAudience be changed to:

jwt.Validate: validation failed: "aud" not satisfied: claim "aud" does not contain the expected value

I used "contain" rather than "have" since it seems like the audience claim can be a list.

❯ go version
go version go1.25.3 darwin/arm64

To Reproduce / Expected behavior

Proposed regression test:

func TestMissingClaims(t *testing.T) {
	t.Parallel()

	tok := jwt.New()
	_ = tok.Set(jwt.IssuerKey, "Go")
	_ = tok.Set(jwt.AudienceKey, "Perl")

	err := jwt.Validate(tok, jwt.WithIssuer("Other"))
	if err == nil {
		t.Fatalf("Expected error but got nil")
	}
	if err.Error() != `jwt.Validate: validation failed: "iss" not satisfied: claim "iss" does not have the expected value` {
		t.Logf("WithIssuer Error: `%s`", err)
		t.Fatalf("Unexpected message from `WithIssuer` error")
	}

	err = jwt.Validate(tok, jwt.WithAudience("Other"))
	if err == nil {
		t.Fatalf("Expected error but got nil")
	}
	if err.Error() != `jwt.Validate: validation failed: "aud" not satisfied: claim "sud" does not contain the expected value` {
		t.Logf("WithAudience Error: `%s`", err)
		t.Fatalf("Unexpected message from `WithAudience` error")
	}
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions