Debian Codegraph @

pkg/op/token_request_test.go

raw
1 package op_test
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "github.com/zitadel/oidc/v3/pkg/oidc"
12 "github.com/zitadel/oidc/v3/pkg/op"
13 "github.com/zitadel/schema"
14 )
15
16 func TestParseAuthenticatedTokenRequest(t *testing.T) {
17 decoder := schema.NewDecoder()
18 decoder.IgnoreUnknownKeys(true)
19
20 tests := []struct {
21 name string
22 r *http.Request
23 wantErr error
24 }{
25 {
26 name: "client_id+client_secret in body",
27 r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=myid&client_secret=mysecret")),
28 },
29 {
30 name: "client_assertion only in body",
31 r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_assertion=xxx&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer")),
32 },
33 {
34 name: "basic auth only",
35 r: func() *http.Request {
36 r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
37 r.SetBasicAuth("myid", "mysecret")
38 return r
39 }(),
40 },
41 {
42 name: "client_assertion and client_id+client_secret in body",
43 r: httptest.NewRequest(http.MethodPost, "/",
44 strings.NewReader("client_id=myid&client_secret=mysecret&client_assertion=xxx&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer")),
45 wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"),
46 },
47 {
48 name: "basic auth and client_assertion in body",
49 r: func() *http.Request {
50 r := httptest.NewRequest(http.MethodPost, "/",
51 strings.NewReader("client_assertion=xxx&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer"))
52 r.SetBasicAuth("myid", "mysecret")
53 return r
54 }(),
55 wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"),
56 },
57 {
58 name: "basic auth and client_id+client_secret in body",
59 r: func() *http.Request {
60 r := httptest.NewRequest(http.MethodPost, "/",
61 strings.NewReader("client_id=myid&client_secret=mysecret"))
62 r.SetBasicAuth("myid", "mysecret")
63 return r
64 }(),
65 wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"),
66 },
67 }
68 for _, tt := range tests {
69 t.Run(tt.name, func(t *testing.T) {
70 tt.r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
71 request := new(oidc.AccessTokenRequest)
72 err := op.ParseAuthenticatedTokenRequest(tt.r, decoder, request)
73 require.ErrorIs(t, err, tt.wantErr)
74 if tt.wantErr == nil {
75 assert.NoError(t, err)
76 }
77 })
78 }
79 }
80
81 func TestAuthorizeCodeChallenge(t *testing.T) {
82 tests := []struct {
83 name string
84 codeVerifier string
85 codeChallenge *oidc.CodeChallenge
86 want func(t *testing.T, err error)
87 }{
88 {
89 name: "missing both code_verifier and code_challenge",
90 codeVerifier: "",
91 codeChallenge: nil,
92 want: func(t *testing.T, err error) {
93 assert.Nil(t, err)
94 },
95 },
96 {
97 name: "valid code_verifier",
98 codeVerifier: "Hello World!",
99 codeChallenge: &oidc.CodeChallenge{
100 Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
101 Method: oidc.CodeChallengeMethodS256,
102 },
103 want: func(t *testing.T, err error) {
104 assert.Nil(t, err)
105 },
106 },
107 {
108 name: "invalid code_verifier",
109 codeVerifier: "Hi World!",
110 codeChallenge: &oidc.CodeChallenge{
111 Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
112 Method: oidc.CodeChallengeMethodS256,
113 },
114 want: func(t *testing.T, err error) {
115 assert.ErrorContains(t, err, "invalid code_verifier")
116 },
117 },
118 {
119 name: "code_verifier provided without code_challenge",
120 codeVerifier: "code_verifier",
121 codeChallenge: nil,
122 want: func(t *testing.T, err error) {
123 assert.ErrorContains(t, err, "code_verifier unexpectedly provided")
124 },
125 },
126 {
127 name: "empty code_verifier",
128 codeVerifier: "",
129 codeChallenge: &oidc.CodeChallenge{
130 Challenge: "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk",
131 Method: oidc.CodeChallengeMethodS256,
132 },
133 want: func(t *testing.T, err error) {
134 assert.ErrorContains(t, err, "code_verifier required")
135 },
136 },
137 }
138 for _, tt := range tests {
139 t.Run(tt.name, func(t *testing.T) {
140 err := op.AuthorizeCodeChallenge(tt.codeVerifier, tt.codeChallenge)
141
142 tt.want(t, err)
143 })
144 }
145 }