-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathintrospection_test.go
More file actions
168 lines (147 loc) · 4.58 KB
/
Copy pathintrospection_test.go
File metadata and controls
168 lines (147 loc) · 4.58 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package introspection
import (
"context"
"crypto/rand"
"crypto/rsa"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/dexidp/dex/server/oauth2"
"github.com/dexidp/dex/server/signer"
"github.com/dexidp/dex/server/tokens"
"github.com/dexidp/dex/storage"
"github.com/dexidp/dex/storage/memory"
)
const testIssuer = "https://test.tech/non-root-path"
func testHandler(t *testing.T) *Handler {
t.Helper()
return &Handler{
Issuer: testIssuer,
Logger: slog.New(slog.DiscardHandler),
}
}
// testAccessToken signs a valid RS256 access token for token-type guessing.
func testAccessToken(t *testing.T) string {
t.Helper()
ctx := context.Background()
logger := slog.New(slog.DiscardHandler)
key, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
sig, err := signer.NewMockSigner(key)
require.NoError(t, err)
issURL, err := url.Parse(testIssuer)
require.NoError(t, err)
issuer := tokens.NewIssuer(memory.New(logger), sig, *issURL, time.Hour, time.Now, logger)
token, _, err := issuer.SignIDToken(ctx, tokens.Authorization{
Client: storage.Client{ID: "test"},
Claims: storage.Claims{UserID: "1", Username: "jane"},
Scopes: []string{"openid"},
Nonce: "nonce",
ConnectorID: "test",
}, "", "")
require.NoError(t, err)
return token
}
func TestGetTokenFromRequestSuccess(t *testing.T) {
h := testHandler(t)
accessToken := testAccessToken(t)
tests := []struct {
testName string
expectedToken string
expectedTokenType TokenTypeEnum
}{
{
testName: "Access Token",
expectedToken: accessToken,
expectedTokenType: AccessToken,
},
{
testName: "Refresh token",
expectedToken: "CgR0ZXN0EgNiYXI",
expectedTokenType: RefreshToken,
},
{
testName: "Unknown token",
expectedToken: "AaAaAaA",
expectedTokenType: RefreshToken,
},
}
for _, tc := range tests {
t.Run(tc.testName, func(t *testing.T) {
data := url.Values{}
data.Set("token", tc.expectedToken)
req := httptest.NewRequest(http.MethodPost, "https://test.tech/token/introspect", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
token, tokenType, err := h.getTokenFromRequest(req)
require.NoError(t, err)
require.Equal(t, tc.expectedToken, token)
require.Equal(t, tc.expectedTokenType, tokenType)
})
}
}
func TestGetTokenFromRequestFailure(t *testing.T) {
h := testHandler(t)
// The method is now enforced at the router level (POST only), so
// getTokenFromRequest no longer checks it; only body validation remains.
_, _, err := h.getTokenFromRequest(httptest.NewRequest(http.MethodPost, "https://test.tech/token/introspect", nil))
require.ErrorIs(t, err, &introspectionError{
typ: oauth2.InvalidRequest,
desc: "The POST body can not be empty.",
code: http.StatusBadRequest,
})
req := httptest.NewRequest(http.MethodPost, "https://test.tech/token/introspect", strings.NewReader("token_type_hint=access_token"))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
_, _, err = h.getTokenFromRequest(req)
require.ErrorIs(t, err, &introspectionError{
typ: oauth2.InvalidRequest,
desc: "The POST body doesn't contain 'token' parameter.",
code: http.StatusBadRequest,
})
}
func TestIntrospectErrHelper(t *testing.T) {
h := testHandler(t)
tests := []struct {
testName string
err *introspectionError
resStatusCode int
resBody string
}{
{
testName: "Inactive Token",
err: newIntrospectInactiveTokenError(),
resStatusCode: http.StatusOK,
resBody: "{\"active\":false}\n",
},
{
testName: "Bad Request",
err: newIntrospectBadRequestError("This is a bad request"),
resStatusCode: http.StatusBadRequest,
resBody: `{"error":"invalid_request","error_description":"This is a bad request"}`,
},
{
testName: "Internal Server Error",
err: newIntrospectInternalServerError(),
resStatusCode: http.StatusInternalServerError,
resBody: `{"error":"server_error"}`,
},
}
for _, tc := range tests {
t.Run(tc.testName, func(t *testing.T) {
w1 := httptest.NewRecorder()
h.introspectErrHelper(w1, tc.err.typ, tc.err.desc, tc.err.code)
res := w1.Result()
require.Equal(t, tc.resStatusCode, res.StatusCode)
require.Equal(t, "application/json", res.Header.Get("Content-Type"))
data, err := io.ReadAll(res.Body)
defer res.Body.Close()
require.NoError(t, err)
require.Equal(t, tc.resBody, string(data))
})
}
}