Skip to content

Commit 46d91e1

Browse files
committed
feat(auth): Refactor OAuth2 authentication to support multiple flows and improve token management
1 parent a1d5800 commit 46d91e1

File tree

3 files changed

+226
-564
lines changed

3 files changed

+226
-564
lines changed

core/dbio/api/api_test.go

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -240,83 +240,29 @@ func TestOAuth2Authentication(t *testing.T) {
240240
expectError bool
241241
errorMsg string
242242
}{
243-
{
244-
name: "client_credentials_missing_client_id",
245-
auth: Authentication{
246-
"type": AuthTypeOAuth2,
247-
"flow": "client_credentials",
248-
"client_secret": "secret123",
249-
"authentication_url": "https://api.example.com/oauth/token",
250-
},
251-
expectError: true,
252-
errorMsg: "client_id is required",
253-
},
254243
{
255244
name: "client_credentials_missing_client_secret",
256245
auth: Authentication{
257246
"type": AuthTypeOAuth2,
258-
"flow": "client_credentials",
247+
"flow": OAuthFlowClientCredentials,
259248
"client_id": "client123",
260249
"authentication_url": "https://api.example.com/oauth/token",
261250
},
262251
expectError: true,
263252
errorMsg: "client_secret is required",
264253
},
265254
{
266-
name: "client_credentials_missing_auth_url",
267-
auth: Authentication{
268-
"type": AuthTypeOAuth2,
269-
"flow": "client_credentials",
270-
"client_id": "client123",
271-
"client_secret": "secret123",
272-
},
273-
expectError: true,
274-
errorMsg: "authentication_url is required",
275-
},
276-
{
277-
name: "refresh_token_missing_token",
278-
auth: Authentication{
279-
"type": AuthTypeOAuth2,
280-
"flow": "refresh_token",
281-
"authentication_url": "https://api.example.com/oauth/token",
282-
},
283-
expectError: true,
284-
errorMsg: "refresh_token is required",
285-
},
286-
{
287-
name: "password_missing_username",
255+
name: "authorization_code_missing_auth_url",
288256
auth: Authentication{
289257
"type": AuthTypeOAuth2,
290-
"flow": "password",
291-
"password": "pass123",
292-
"authentication_url": "https://api.example.com/oauth/token",
293-
},
294-
expectError: true,
295-
errorMsg: "username is required",
296-
},
297-
{
298-
name: "password_missing_password",
299-
auth: Authentication{
300-
"type": AuthTypeOAuth2,
301-
"flow": "password",
302-
"username": "user123",
303-
"authentication_url": "https://api.example.com/oauth/token",
304-
},
305-
expectError: true,
306-
errorMsg: "password is required",
307-
},
308-
{
309-
name: "authorization_code_missing_code",
310-
auth: Authentication{
311-
"type": AuthTypeOAuth2,
312-
"flow": "authorization_code",
258+
"flow": OAuthFlowAuthorizationCode,
313259
"client_id": "client123",
314260
"client_secret": "secret123",
315261
"redirect_uri": "https://app.example.com/callback",
316-
"authentication_url": "https://api.example.com/oauth/token",
262+
"authorization_url": "https://api.example.com/oauth/authorize",
317263
},
318264
expectError: true,
319-
errorMsg: "authorization code is required",
265+
errorMsg: "authentication_url is required",
320266
},
321267
{
322268
name: "unsupported_flow",
@@ -347,11 +293,8 @@ func TestOAuth2Authentication(t *testing.T) {
347293
assert.NoError(t, err)
348294
authOAuth, ok := authenticator.(*AuthenticatorOAuth2)
349295
if assert.True(t, ok) {
350-
_, err := authOAuth.performOAuth2Flow()
351-
352-
// We expect this to fail at HTTP request stage, not validation
353-
assert.Error(t, err)
354-
assert.Contains(t, err.Error(), tt.errorMsg)
296+
authOAuth.conn = ac
297+
err := authOAuth.Authenticate(ac.Context.Ctx, &ac.State.Auth)
355298

356299
if tt.expectError {
357300
assert.Error(t, err)
@@ -382,7 +325,7 @@ func TestOAuth2FlowValidation(t *testing.T) {
382325

383326
ac.Spec.Authentication = Authentication{
384327
"type": AuthTypeOAuth2,
385-
"flow": "client_credentials",
328+
"flow": OAuthFlowClientCredentials,
386329
"client_id": "{secrets.CLIENT_ID}",
387330
"client_secret": "{secrets.CLIENT_SECRET}",
388331
"authentication_url": "https://api.example.com/oauth/token",
@@ -393,11 +336,13 @@ func TestOAuth2FlowValidation(t *testing.T) {
393336
authenticator, _ := ac.MakeAuthenticator()
394337
authOAuth, ok := authenticator.(*AuthenticatorOAuth2)
395338
if assert.True(t, ok) {
396-
_, err := authOAuth.performOAuth2Flow()
339+
authOAuth.conn = ac
340+
err := authOAuth.Authenticate(ac.Context.Ctx, &ac.State.Auth)
397341

398342
// We expect this to fail at HTTP request stage, not validation
343+
// The new implementation will fail when trying to get token from the oauth2 server
399344
assert.Error(t, err)
400-
assert.Contains(t, err.Error(), "failed to execute OAuth2 request")
345+
assert.Contains(t, err.Error(), "failed to get token")
401346
}
402347
}
403348

0 commit comments

Comments
 (0)