Skip to content

Commit

Permalink
Add kid into Header (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
danikarik authored Jan 14, 2021
1 parent 7c73a54 commit 831cdfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ func (t *Token) Signature() []byte {
}

// Header representa JWT header data.
// See: https://tools.ietf.org/html/rfc7519#section-5
// See: https://tools.ietf.org/html/rfc7519#section-5, https://tools.ietf.org/html/rfc7517
//
type Header struct {
Algorithm Algorithm `json:"alg"`
Type string `json:"typ,omitempty"` // only "JWT" can be here
ContentType string `json:"cty,omitempty"`
KeyID string `json:"kid,omitempty"`
}

// MarshalJSON implements the json.Marshaler interface.
Expand All @@ -80,6 +81,10 @@ func (h *Header) MarshalJSON() ([]byte, error) {
buf.WriteString(`","cty":"`)
buf.WriteString(h.ContentType)
}
if h.KeyID != "" {
buf.WriteString(`","kid":"`)
buf.WriteString(h.KeyID)
}
buf.WriteString(`"}`)

return buf.Bytes(), nil
Expand Down
4 changes: 4 additions & 0 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func TestMarshalHeader(t *testing.T) {
&Header{Algorithm: RS256, Type: "JwT", ContentType: "token"},
`{"alg":"RS256","typ":"JwT","cty":"token"}`,
)
f(
&Header{Algorithm: RS256, Type: "JwT", ContentType: "token", KeyID: "test"},
`{"alg":"RS256","typ":"JwT","cty":"token","kid":"test"}`,
)
}

func TestSecurePrint(t *testing.T) {
Expand Down

0 comments on commit 831cdfc

Please sign in to comment.