Repository: lestrrat-go/jwx
Pull request #206 (2020-09-28 06:00:20)

Add missing PrivateParams method


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 7df23e8b..37d7534c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -6,7 +6,7 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        go: [ 'tip', '1.14', '1.13' ]
+        go: [ 'tip', '1.15', '1.14' ]
     name: Go ${{ matrix.go }} test
     steps:
       - name: Checkout repository
diff --git a/Makefile b/Makefile
index f4ee4ad7..c0853a1e 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ generate:
 	@$(MAKE) generate-jwa generate-jwk generate-jws generate-jwt
 
 generate-%:
-	@cd $(patsubst generate-%,%,$@); go generate 
+	@go generate $(shell pwd -P)/$(patsubst generate-%,%,$@)
 
 realclean:
 	rm coverage.out
diff --git a/jwe/headers_gen.go b/jwe/headers_gen.go
index 4d944127..ad26ce59 100644
--- a/jwe/headers_gen.go
+++ b/jwe/headers_gen.go
@@ -60,6 +60,7 @@ type Headers interface {
 	Remove(string) error
 	Encode() ([]byte, error)
 	Decode([]byte) error
+	PrivateParams() map[string]interface{}
 }
 
 type stdHeaders struct {
diff --git a/jwe/internal/cmd/genheader/main.go b/jwe/internal/cmd/genheader/main.go
index a64ccead..6d5008de 100644
--- a/jwe/internal/cmd/genheader/main.go
+++ b/jwe/internal/cmd/genheader/main.go
@@ -265,6 +265,9 @@ func generateHeaders() error {
 	fmt.Fprintf(&buf, "\nEncode() ([]byte, error)")
 	fmt.Fprintf(&buf, "\nDecode([]byte) error")
 
+	// Access private parameters
+	fmt.Fprintf(&buf, "\nPrivateParams() map[string]interface{}")
+
 	fmt.Fprintf(&buf, "\n}")
 
 	fmt.Fprintf(&buf, "\n\ntype stdHeaders struct {")
diff --git a/jws/headers_gen.go b/jws/headers_gen.go
index 98a6f5b5..68baf398 100644
--- a/jws/headers_gen.go
+++ b/jws/headers_gen.go
@@ -46,6 +46,7 @@ type Headers interface {
 	AsMap(ctx context.Context) (map[string]interface{}, error)
 	Get(string) (interface{}, bool)
 	Set(string, interface{}) error
+	PrivateParams() map[string]interface{}
 }
 
 type stdHeaders struct {
diff --git a/jws/internal/cmd/genheader/main.go b/jws/internal/cmd/genheader/main.go
index ae470f5e..359f48a3 100644
--- a/jws/internal/cmd/genheader/main.go
+++ b/jws/internal/cmd/genheader/main.go
@@ -209,6 +209,8 @@ func generateHeaders() error {
 	fmt.Fprintf(&buf, "\nGet(string) (interface{}, bool)")
 	fmt.Fprintf(&buf, "\nSet(string, interface{}) error")
 
+	fmt.Fprintf(&buf, "\nPrivateParams() map[string]interface{}")
+
 	fmt.Fprintf(&buf, "\n}")
 
 	fmt.Fprintf(&buf, "\n\ntype stdHeaders struct {")


Test patch:
diff --git a/jwe/headers_test.go b/jwe/headers_test.go
index ecb07b8e..3b961a71 100644
--- a/jwe/headers_test.go
+++ b/jwe/headers_test.go
@@ -48,6 +48,7 @@ func TestHeaders(t *testing.T) {
 		{Key: jwe.X509CertThumbprintKey, Value: "x5t blah"},
 		{Key: jwe.X509CertThumbprintS256Key, Value: "x5t#256 blah"},
 		{Key: jwe.X509URLKey, Value: "http://github.com/lestrrat-go/jwx"},
+		{Key: "private", Value: "boofoo"},
 	}
 
 	base := jwe.NewHeaders()
@@ -77,6 +78,18 @@ func TestHeaders(t *testing.T) {
 			}
 		}
 	})
+	t.Run("PrivateParams", func(t *testing.T) {
+		h := base
+		pp := h.PrivateParams()
+		v, ok := pp["private"]
+		if !assert.True(t, ok, "key 'private' should exists") {
+			return
+		}
+
+		if !assert.Equal(t, v, "boofoo", "value for 'private' should match") {
+			return
+		}
+	})
 	t.Run("Encode", func(t *testing.T) {
 		h1 := jwe.NewHeaders()
 		h1.Set(jwe.AlgorithmKey, jwa.A128GCMKW)
diff --git a/jwk/headers_test.go b/jwk/headers_test.go
index d81b96c0..6582b185 100644
--- a/jwk/headers_test.go
+++ b/jwk/headers_test.go
@@ -18,6 +18,7 @@ func TestHeader(t *testing.T) {
 			jwk.X509CertThumbprintKey:     "thumbprint",
 			jwk.X509CertThumbprintS256Key: "thumbprint256",
 			jwk.X509URLKey:                "cert1",
+			"private":                     "boofoo",
 		}
 
 		h, err := jwk.New([]byte("dummy"))
@@ -43,6 +44,18 @@ func TestHeader(t *testing.T) {
 				return
 			}
 		}
+
+		t.Run("Private params", func(t *testing.T) {
+			pp := h.PrivateParams()
+			v, ok := pp["private"]
+			if !assert.True(t, ok, "key 'private' should exists") {
+				return
+			}
+
+			if !assert.Equal(t, v, "boofoo", "value for 'private' should match") {
+				return
+			}
+		})
 	})
 	t.Run("RoundtripError", func(t *testing.T) {
 		type dummyStruct struct {
diff --git a/jws/headers_test.go b/jws/headers_test.go
index 2fd1b5c6..a1fba044 100644
--- a/jws/headers_test.go
+++ b/jws/headers_test.go
@@ -38,6 +38,7 @@ func TestHeader(t *testing.T) {
 		jws.X509CertChainKey:      certChain,
 		jws.X509CertThumbprintKey: "QzY0NjREMjkyQTI4RTU2RkE4MUJBRDExNzY1MUY1N0I4QjFCODlBOQ",
 		jws.X509URLKey:            "https://www.x509.com/key.pem",
+		"private":                 "boofoo",
 	}
 
 	t.Run("Type", func(t *testing.T) {
@@ -253,5 +254,16 @@ func TestHeader(t *testing.T) {
 				return
 			}
 		})
+		t.Run("PrivateParams", func(t *testing.T) {
+			pp := h.PrivateParams()
+			v, ok := pp["private"]
+			if !assert.True(t, ok, "key 'private' should exists") {
+				return
+			}
+
+			if !assert.Equal(t, v, "boofoo", "value for 'private' should match") {
+				return
+			}
+		})
 	})
 }