| 1 |
package op_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/golang/mock/gomock" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 17 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 18 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 19 |
"github.com/zitadel/oidc/v3/pkg/op/mock" |
| 20 |
"github.com/zitadel/schema" |
| 21 |
) |
| 22 |
|
| 23 |
type testClientJWTProfile struct{} |
| 24 |
|
| 25 |
func (testClientJWTProfile) JWTProfileVerifier(context.Context) *op.JWTProfileVerifier { return nil } |
| 26 |
|
| 27 |
func TestClientJWTAuth(t *testing.T) { |
| 28 |
type args struct { |
| 29 |
ctx context.Context |
| 30 |
ca oidc.ClientAssertionParams |
| 31 |
verifier op.ClientJWTProfile |
| 32 |
} |
| 33 |
tests := []struct { |
| 34 |
name string |
| 35 |
args args |
| 36 |
wantClientID string |
| 37 |
wantErr error |
| 38 |
}{ |
| 39 |
{ |
| 40 |
name: "empty assertion", |
| 41 |
args: args{ |
| 42 |
context.Background(), |
| 43 |
oidc.ClientAssertionParams{}, |
| 44 |
testClientJWTProfile{}, |
| 45 |
}, |
| 46 |
wantErr: op.ErrNoClientCredentials, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "verification error", |
| 50 |
args: args{ |
| 51 |
context.Background(), |
| 52 |
oidc.ClientAssertionParams{ |
| 53 |
ClientAssertion: "foo", |
| 54 |
}, |
| 55 |
testClientJWTProfile{}, |
| 56 |
}, |
| 57 |
wantErr: oidc.ErrParse, |
| 58 |
}, |
| 59 |
} |
| 60 |
for _, tt := range tests { |
| 61 |
t.Run(tt.name, func(t *testing.T) { |
| 62 |
gotClientID, err := op.ClientJWTAuth(tt.args.ctx, tt.args.ca, tt.args.verifier) |
| 63 |
require.ErrorIs(t, err, tt.wantErr) |
| 64 |
assert.Equal(t, tt.wantClientID, gotClientID) |
| 65 |
}) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestClientBasicAuth(t *testing.T) { |
| 70 |
errWrong := errors.New("wrong secret") |
| 71 |
|
| 72 |
type args struct { |
| 73 |
username string |
| 74 |
password string |
| 75 |
} |
| 76 |
tests := []struct { |
| 77 |
name string |
| 78 |
args *args |
| 79 |
storage op.Storage |
| 80 |
wantClientID string |
| 81 |
wantErr error |
| 82 |
}{ |
| 83 |
{ |
| 84 |
name: "no args", |
| 85 |
wantErr: op.ErrNoClientCredentials, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "username unescape err", |
| 89 |
args: &args{ |
| 90 |
username: "%", |
| 91 |
password: "bar", |
| 92 |
}, |
| 93 |
wantErr: op.ErrInvalidAuthHeader, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "password unescape err", |
| 97 |
args: &args{ |
| 98 |
username: "foo", |
| 99 |
password: "%", |
| 100 |
}, |
| 101 |
wantErr: op.ErrInvalidAuthHeader, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "auth error", |
| 105 |
args: &args{ |
| 106 |
username: "foo", |
| 107 |
password: "wrong", |
| 108 |
}, |
| 109 |
storage: func() op.Storage { |
| 110 |
s := mock.NewMockStorage(gomock.NewController(t)) |
| 111 |
s.EXPECT().AuthorizeClientIDSecret(gomock.Any(), "foo", "wrong").Return(errWrong) |
| 112 |
return s |
| 113 |
}(), |
| 114 |
wantErr: errWrong, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "auth error", |
| 118 |
args: &args{ |
| 119 |
username: "foo", |
| 120 |
password: "bar", |
| 121 |
}, |
| 122 |
storage: func() op.Storage { |
| 123 |
s := mock.NewMockStorage(gomock.NewController(t)) |
| 124 |
s.EXPECT().AuthorizeClientIDSecret(gomock.Any(), "foo", "bar").Return(nil) |
| 125 |
return s |
| 126 |
}(), |
| 127 |
wantClientID: "foo", |
| 128 |
}, |
| 129 |
} |
| 130 |
for _, tt := range tests { |
| 131 |
t.Run(tt.name, func(t *testing.T) { |
| 132 |
r := httptest.NewRequest(http.MethodGet, "/foo", nil) |
| 133 |
if tt.args != nil { |
| 134 |
r.SetBasicAuth(tt.args.username, tt.args.password) |
| 135 |
} |
| 136 |
|
| 137 |
gotClientID, err := op.ClientBasicAuth(r, tt.storage) |
| 138 |
require.ErrorIs(t, err, tt.wantErr) |
| 139 |
assert.Equal(t, tt.wantClientID, gotClientID) |
| 140 |
}) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
type errReader struct{} |
| 145 |
|
| 146 |
func (errReader) Read([]byte) (int, error) { |
| 147 |
return 0, io.ErrNoProgress |
| 148 |
} |
| 149 |
|
| 150 |
type testClientProvider struct { |
| 151 |
storage op.Storage |
| 152 |
} |
| 153 |
|
| 154 |
func (testClientProvider) Decoder() httphelper.Decoder { |
| 155 |
return schema.NewDecoder() |
| 156 |
} |
| 157 |
|
| 158 |
func (p testClientProvider) Storage() op.Storage { |
| 159 |
return p.storage |
| 160 |
} |
| 161 |
|
| 162 |
func TestClientIDFromRequest(t *testing.T) { |
| 163 |
type args struct { |
| 164 |
body io.Reader |
| 165 |
p op.ClientProvider |
| 166 |
} |
| 167 |
type basicAuth struct { |
| 168 |
username string |
| 169 |
password string |
| 170 |
} |
| 171 |
tests := []struct { |
| 172 |
name string |
| 173 |
args args |
| 174 |
basicAuth *basicAuth |
| 175 |
wantClientID string |
| 176 |
wantAuthenticated bool |
| 177 |
wantErr bool |
| 178 |
}{ |
| 179 |
{ |
| 180 |
name: "parse error", |
| 181 |
args: args{ |
| 182 |
body: errReader{}, |
| 183 |
}, |
| 184 |
wantErr: true, |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "unauthenticated", |
| 188 |
args: args{ |
| 189 |
body: strings.NewReader( |
| 190 |
url.Values{ |
| 191 |
"client_id": []string{"foo"}, |
| 192 |
}.Encode(), |
| 193 |
), |
| 194 |
p: testClientProvider{ |
| 195 |
storage: mock.NewStorage(t), |
| 196 |
}, |
| 197 |
}, |
| 198 |
wantClientID: "foo", |
| 199 |
wantAuthenticated: false, |
| 200 |
}, |
| 201 |
{ |
| 202 |
name: "authenticated", |
| 203 |
args: args{ |
| 204 |
body: strings.NewReader( |
| 205 |
url.Values{}.Encode(), |
| 206 |
), |
| 207 |
p: testClientProvider{ |
| 208 |
storage: func() op.Storage { |
| 209 |
s := mock.NewMockStorage(gomock.NewController(t)) |
| 210 |
s.EXPECT().AuthorizeClientIDSecret(gomock.Any(), "foo", "bar").Return(nil) |
| 211 |
return s |
| 212 |
}(), |
| 213 |
}, |
| 214 |
}, |
| 215 |
basicAuth: &basicAuth{ |
| 216 |
username: "foo", |
| 217 |
password: "bar", |
| 218 |
}, |
| 219 |
wantClientID: "foo", |
| 220 |
wantAuthenticated: true, |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "missing client id", |
| 224 |
args: args{ |
| 225 |
body: strings.NewReader( |
| 226 |
url.Values{}.Encode(), |
| 227 |
), |
| 228 |
p: testClientProvider{ |
| 229 |
storage: mock.NewStorage(t), |
| 230 |
}, |
| 231 |
}, |
| 232 |
wantErr: true, |
| 233 |
}, |
| 234 |
} |
| 235 |
for _, tt := range tests { |
| 236 |
t.Run(tt.name, func(t *testing.T) { |
| 237 |
r := httptest.NewRequest(http.MethodPost, "/foo", tt.args.body) |
| 238 |
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 239 |
if tt.basicAuth != nil { |
| 240 |
r.SetBasicAuth(tt.basicAuth.username, tt.basicAuth.password) |
| 241 |
} |
| 242 |
|
| 243 |
gotClientID, gotAuthenticated, err := op.ClientIDFromRequest(r, tt.args.p) |
| 244 |
if tt.wantErr { |
| 245 |
require.Error(t, err) |
| 246 |
} else { |
| 247 |
require.NoError(t, err) |
| 248 |
} |
| 249 |
assert.Equal(t, tt.wantClientID, gotClientID) |
| 250 |
assert.Equal(t, tt.wantAuthenticated, gotAuthenticated) |
| 251 |
}) |
| 252 |
} |
| 253 |
} |