summaryrefslogtreecommitdiff
path: root/pkg/web/json_web_key_sets_test.go
blob: b05948163365df84f941a1463754334d792b2d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package web

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/json"
	"encoding/pem"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"mokhan.ca/xlgmokha/idp/pkg/dto"
)

func TestJsonWebKeySets(t *testing.T) {
	key, _ := rsa.GenerateKey(rand.Reader, 1024)
	b := new(bytes.Buffer)
	pem.Encode(b, &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: x509.MarshalPKCS1PrivateKey(key),
	})

	h := NewHttpContext("https://example.org", b.Bytes())

	t.Run(".well-known/jwks.json", func(t *testing.T) {
		w := httptest.NewRecorder()
		r := httptest.NewRequest("GET", "/.well-known/jwks.json", nil)

		h.Router().ServeHTTP(w, r)

		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")

		var c dto.JsonWebKeySet
		json.NewDecoder(w.Body).Decode(&c)

		assert.Equal(t, 1, len(c.Keys))
		assert.Equal(t, "X", c.Keys[0].KeyId)
		assert.Equal(t, "RSA", c.Keys[0].KeyType)
		assert.NotEmpty(t, c.Keys[0].E)
		assert.NotEmpty(t, c.Keys[0].N)
	})
}