summaryrefslogtreecommitdiff
path: root/pkg/web/open_id_configuration_test.go
blob: 1249a14269149f2a1dde9ac6438fce9c842efb87 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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/oauth/pkg/dto"
)

func TestOpenIdConfiguration(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/openid-configuration", func(t *testing.T) {
		w := httptest.NewRecorder()
		r := httptest.NewRequest("GET", "/.well-known/openid-configuration", nil)

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

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

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

		assert.Equal(t, c.Issuer, "https://example.org")
		assert.Equal(t, c.AuthorizationEndpoint, "https://example.org/authorize")
		assert.Equal(t, c.TokenEndpoint, "https://example.org/token")
		assert.Equal(t, c.UserInfoEndpoint, "https://example.org/userinfo")
		assert.Equal(t, c.JwksUri, "https://example.org/.well-known/jwks.json")
		assert.Equal(t, c.RevocationEndpoint, "https://example.org/revoke")
		assert.EqualValues(t, c.ScopesSupported, []string{"openid"})
		assert.EqualValues(t, c.ResponseTypesSupported, []string{
			"code id_token token",
			"code id_token",
			"code token",
			"code",
			"id_token token",
			"id_token",
		})
		assert.EqualValues(t, c.ResponseModesSupported, []string{
			"query",
			"fragment",
			"form_post",
		})
		assert.EqualValues(t, c.SubjectTypesSupported, []string{"public"})
		assert.EqualValues(t, c.IdTokenSigningAlgValuesSupported, []string{"RS256"})
		assert.EqualValues(t, c.ClaimsSupported, []string{
			"aud",
			"exp",
			"iat",
			"iss",
			"sub",
		})
	})
}