Skip to content

Commit

Permalink
Add support for setting "cty" header parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Sanches committed Sep 13, 2018
1 parent e0b68d8 commit 4b3c975
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `Marshal` and `Unmarshal` functions.
- `Marshaler` interface.
- `Unmarshaler` interface.
- Content type header parameter.

### Changed
- Modify `Signer` signature.
Expand Down
7 changes: 4 additions & 3 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package jwt
//
// Parameters are ordered according to the RFC 7515.
type header struct {
Algorithm string `json:"alg,omitempty"`
KeyID string `json:"kid,omitempty"`
Type string `json:"typ,omitempty"`
Algorithm string `json:"alg,omitempty"`
KeyID string `json:"kid,omitempty"`
Type string `json:"typ,omitempty"`
ContentType string `json:"cty,omitempty"`
}
15 changes: 14 additions & 1 deletion jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,29 @@ func (jot *JWT) Algorithm() string {
return jot.header().Algorithm
}

// ContentType returns the JWT's header's content type.
func (jot *JWT) ContentType() string {
return jot.header().ContentType
}

// KeyID returns the JWT's header's key ID.
func (jot *JWT) KeyID() string {
return jot.header().KeyID
}

// SetAlgorithm sets the algorithm a JWT uses based on its signer.
// SetAlgorithm sets the algorithm a JWT uses to be signed.
func (jot *JWT) SetAlgorithm(s Signer) {
jot.header().Algorithm = s.String()
}

// SetContentType sets the JWT's header's content type.
//
// This is useful if a type implements the Marshaler and the Unmarshaler
// types in order to use JWE instead of JWS for signing and verifying.
func (jot *JWT) SetContentType(cty string) {
jot.header().ContentType = cty
}

// SetKeyID sets the key ID assigned to a JWT.
func (jot *JWT) SetKeyID(kid string) {
jot.header().KeyID = kid
Expand Down
6 changes: 6 additions & 0 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func testJWT(t *testing.T, testCases []testCase) {
now := time.Now()
kid := fmt.Sprintf("kid %s %d", t.Name(), i)
typ := "JWT"
cty := "JWT"
iat := now.Unix()
exp := now.Add(30 * time.Minute).Unix()
nbf := now.Add(1 * time.Second).Unix()
Expand All @@ -60,6 +61,7 @@ func testJWT(t *testing.T, testCases []testCase) {
}
jot.SetAlgorithm(tc.signer)
jot.SetKeyID(kid)
jot.SetContentType(cty)

// 1 - Marshal.
payload, err := Marshal(jot)
Expand Down Expand Up @@ -120,6 +122,10 @@ func testJWT(t *testing.T, testCases []testCase) {
t.Errorf("want %s, got %s", want, got)
}

if want, got := cty, jot2.ContentType(); want != got {
t.Errorf("want %s, got %s", want, got)
}

if want, got := iat, jot2.IssuedAt; want != got {
t.Errorf("want %d, got %d", want, got)
}
Expand Down

0 comments on commit 4b3c975

Please sign in to comment.