diff --git a/modules/structs/auth.go b/modules/structs/auth.go new file mode 100644 index 0000000000..2ee85a7707 --- /dev/null +++ b/modules/structs/auth.go @@ -0,0 +1,13 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package structs + +type AuthSourceOption struct { + ID int64 `json:"id"` + AuthenticationName string `json:"authentication_name" binding:"Required"` + TypeName string `json:"type_name"` + + IsActive bool `json:"is_active"` + IsSyncEnabled bool `json:"is_sync_enabled"` +} diff --git a/modules/structs/auth_oauth2.go b/modules/structs/auth_oauth2.go index 7b5ed5d4ef..b23533fade 100644 --- a/modules/structs/auth_oauth2.go +++ b/modules/structs/auth_oauth2.go @@ -3,15 +3,6 @@ package structs -type AuthOauth2Option struct { - ID int64 `json:"id"` - AuthenticationName string `json:"authentication_name" binding:"Required"` - TypeName string `json:"type_name"` - - IsActive bool `json:"is_active"` - IsSyncEnabled bool `json:"is_sync_enabled"` -} - // CreateUserOption create user options type CreateAuthOauth2Option struct { AuthenticationName string `json:"authentication_name" binding:"Required"` diff --git a/routers/api/v1/admin/auth.go b/routers/api/v1/admin/auth.go new file mode 100644 index 0000000000..2acfaadc75 --- /dev/null +++ b/routers/api/v1/admin/auth.go @@ -0,0 +1,59 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package admin + +import ( + "net/http" + + auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/routers/api/v1/utils" + "code.gitea.io/gitea/services/context" + "code.gitea.io/gitea/services/convert" +) + +// SearchAuth API for getting information of the configured authentication methods according the filter conditions +func SearchAuth(ctx *context.APIContext) { + // swagger:operation GET /admin/identity-auth admin adminSearchAuth + // --- + // summary: Search authentication sources + // produces: + // - application/json + // parameters: + // - name: page + // in: query + // description: page number of results to return (1-based) + // type: integer + // - name: limit + // in: query + // description: page size of results + // type: integer + // responses: + // "200": + // description: "SearchResults of authentication sources" + // schema: + // type: array + // items: + // "$ref": "#/definitions/AuthOauth2Option" + // "403": + // "$ref": "#/responses/forbidden" + + listOptions := utils.GetListOptions(ctx) + + authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{}) + if err != nil { + ctx.APIErrorInternal(err) + return + } + + results := make([]*api.AuthSourceOption, len(authSources)) + for i := range authSources { + results[i] = convert.ToOauthProvider(ctx, authSources[i]) + } + + ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) + ctx.SetTotalCountHeader(maxResults) + ctx.JSON(http.StatusOK, &results) +} diff --git a/routers/api/v1/admin/auth_oauth.go b/routers/api/v1/admin/auth_oauth.go index c594afa9cf..9bdf4b9556 100644 --- a/routers/api/v1/admin/auth_oauth.go +++ b/routers/api/v1/admin/auth_oauth.go @@ -251,13 +251,15 @@ func SearchOauthAuth(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{}) + authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{ + LoginType: auth_model.OAuth2, + }) if err != nil { ctx.APIErrorInternal(err) return } - results := make([]*api.AuthOauth2Option, len(authSources)) + results := make([]*api.AuthSourceOption, len(authSources)) for i := range authSources { results[i] = convert.ToOauthProvider(ctx, authSources[i]) } diff --git a/services/convert/auth_oauth.go b/services/convert/auth_oauth.go index 93f90b0a52..c01b1bdeac 100644 --- a/services/convert/auth_oauth.go +++ b/services/convert/auth_oauth.go @@ -11,7 +11,7 @@ import ( ) // ToOauthProvider convert auth_model.Source≤ to api.AuthOauth2Option -func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option { +func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthSourceOption { if provider == nil { return nil } @@ -20,16 +20,16 @@ func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.Auth } // ToOauthProviders convert list of auth_model.Source to list of api.AuthOauth2Option -func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthOauth2Option { - result := make([]*api.AuthOauth2Option, len(provider)) +func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthSourceOption { + result := make([]*api.AuthSourceOption, len(provider)) for i := range provider { result[i] = ToOauthProvider(ctx, provider[i]) } return result } -func toOauthProvider(provider *auth_model.Source) *api.AuthOauth2Option { - return &api.AuthOauth2Option{ +func toOauthProvider(provider *auth_model.Source) *api.AuthSourceOption { + return &api.AuthSourceOption{ ID: provider.ID, AuthenticationName: provider.Name, TypeName: provider.Type.String(),