From c830bc13e4f4aeddfd769acddaee31b80f6ea35a Mon Sep 17 00:00:00 2001 From: Tim Riedl Date: Sat, 19 Apr 2025 00:01:37 +0200 Subject: [PATCH] Refactor AuthOauth2Option structure and improve error handling in CreateOauthAuth --- modules/structs/auth_oauth2.go | 8 ++++++-- routers/api/v1/admin/auth_oauth.go | 7 ++----- services/convert/auth_oauth.go | 7 ++++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/structs/auth_oauth2.go b/modules/structs/auth_oauth2.go index 93dfabffb6..4ba1e46de5 100644 --- a/modules/structs/auth_oauth2.go +++ b/modules/structs/auth_oauth2.go @@ -5,9 +5,13 @@ package structs type AuthOauth2Option struct { - SourceID int64 `json:"source_id"` + ID int64 `json:"id"` AuthenticationName string `json:"authentication_name" binding:"Required"` - ProviderIconURL string `json:"provider_icon_url"` + Type int `json:"type"` + TypeName string `json:"type_name"` + + IsActive bool `json:"is_active"` + IsSyncEnabled bool `json:"is_sync_enabled"` } // CreateUserOption create user options diff --git a/routers/api/v1/admin/auth_oauth.go b/routers/api/v1/admin/auth_oauth.go index ac563707bd..247d173f70 100644 --- a/routers/api/v1/admin/auth_oauth.go +++ b/routers/api/v1/admin/auth_oauth.go @@ -35,9 +35,8 @@ func CreateOauthAuth(ctx *context.APIContext) { discoveryURL, err := url.Parse(form.ProviderAutoDiscoveryURL) if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") { - fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", form.ProviderAutoDiscoveryURL) - - // todo: implement handling + _ = fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", form.ProviderAutoDiscoveryURL) + ctx.HTTPError(http.StatusBadRequest, fmt.Sprintf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", form.ProviderAutoDiscoveryURL)) } config := &oauth2.Source{ @@ -67,8 +66,6 @@ func CreateOauthAuth(ctx *context.APIContext) { }) ctx.Status(http.StatusCreated) - - // ctx.JSON(http.StatusCreated, convert.ToUser(ctx, u, ctx.Doer)) } // EditOauthAuth api for modifying a authentication method diff --git a/services/convert/auth_oauth.go b/services/convert/auth_oauth.go index c2f12f19a3..a86c0fc9a7 100644 --- a/services/convert/auth_oauth.go +++ b/services/convert/auth_oauth.go @@ -31,7 +31,12 @@ func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api func toOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option { return &api.AuthOauth2Option{ - SourceID: provider.ID, + ID: provider.ID, AuthenticationName: provider.Name, + Type: provider.Type.Int(), + TypeName: provider.Type.String(), + + IsActive: provider.IsActive, + IsSyncEnabled: provider.IsSyncEnabled, } }