Skip to content

Commit cb0e0ce

Browse files
committed
Refactor OAuth authentication methods for improved error handling and code clarity
1 parent 4787ea4 commit cb0e0ce

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

routers/api/v1/admin/auth_oauth.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ func CreateOauthAuth(ctx *context.APIContext) {
5858
GroupTeamMapRemoval: form.RemoveUsersFromSyncronizedTeams,
5959
}
6060

61-
auth_model.CreateSource(ctx, &auth_model.Source{
61+
createErr := auth_model.CreateSource(ctx, &auth_model.Source{
6262
Type: auth_model.OAuth2,
6363
Name: form.AuthenticationName,
6464
IsActive: true,
6565
Cfg: config,
6666
})
6767

68+
if createErr != nil {
69+
ctx.APIErrorInternal(createErr)
70+
return
71+
}
72+
6873
ctx.Status(http.StatusCreated)
6974
}
7075

@@ -74,14 +79,13 @@ func EditOauthAuth(ctx *context.APIContext) {
7479

7580
// DeleteOauthAuth api for deleting a authentication method
7681
func DeleteOauthAuth(ctx *context.APIContext) {
77-
oauthIdString := ctx.PathParam("id")
78-
oauthID, oauthIdErr := strconv.Atoi(oauthIdString)
79-
if oauthIdErr != nil {
80-
ctx.APIErrorInternal(oauthIdErr)
82+
oauthIDString := ctx.PathParam("id")
83+
oauthID, oauthIDErr := strconv.Atoi(oauthIDString)
84+
if oauthIDErr != nil {
85+
ctx.APIErrorInternal(oauthIDErr)
8186
}
8287

8388
err := auth_model.DeleteSource(ctx, int64(oauthID))
84-
8589
if err != nil {
8690
ctx.APIErrorInternal(err)
8791
return
@@ -95,8 +99,6 @@ func SearchOauthAuth(ctx *context.APIContext) {
9599
listOptions := utils.GetListOptions(ctx)
96100

97101
authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
98-
// fmt.Printf("Count: %d, models: %v, err: %v", count, models[0].Name, err)
99-
100102
if err != nil {
101103
ctx.APIErrorInternal(err)
102104
return

services/convert/auth_oauth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.Auth
1717
return nil
1818
}
1919

20-
return toOauthProvider(ctx, provider)
20+
return toOauthProvider(provider)
2121
}
2222

2323
// ToUsers convert list of user_model.User to list of api.User
@@ -29,7 +29,7 @@ func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api
2929
return result
3030
}
3131

32-
func toOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
32+
func toOauthProvider(provider *auth_model.Source) *api.AuthOauth2Option {
3333
return &api.AuthOauth2Option{
3434
ID: provider.ID,
3535
AuthenticationName: provider.Name,

0 commit comments

Comments
 (0)