Skip to content

Commit 098de09

Browse files
committed
Refactor OAuth authentication structures and endpoints to unify AuthSourceOption usage
1 parent 983e648 commit 098de09

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

modules/structs/auth.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package structs
5+
6+
type AuthSourceOption struct {
7+
ID int64 `json:"id"`
8+
AuthenticationName string `json:"authentication_name" binding:"Required"`
9+
TypeName string `json:"type_name"`
10+
11+
IsActive bool `json:"is_active"`
12+
IsSyncEnabled bool `json:"is_sync_enabled"`
13+
}

modules/structs/auth_oauth2.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33

44
package structs
55

6-
type AuthOauth2Option struct {
7-
ID int64 `json:"id"`
8-
AuthenticationName string `json:"authentication_name" binding:"Required"`
9-
TypeName string `json:"type_name"`
10-
11-
IsActive bool `json:"is_active"`
12-
IsSyncEnabled bool `json:"is_sync_enabled"`
13-
}
14-
156
// CreateUserOption create user options
167
type CreateAuthOauth2Option struct {
178
AuthenticationName string `json:"authentication_name" binding:"Required"`

routers/api/v1/admin/auth.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package admin
5+
6+
import (
7+
"net/http"
8+
9+
auth_model "code.gitea.io/gitea/models/auth"
10+
"code.gitea.io/gitea/models/db"
11+
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/routers/api/v1/utils"
13+
"code.gitea.io/gitea/services/context"
14+
"code.gitea.io/gitea/services/convert"
15+
)
16+
17+
// SearchAuth API for getting information of the configured authentication methods according the filter conditions
18+
func SearchAuth(ctx *context.APIContext) {
19+
// swagger:operation GET /admin/identity-auth admin adminSearchAuth
20+
// ---
21+
// summary: Search authentication sources
22+
// produces:
23+
// - application/json
24+
// parameters:
25+
// - name: page
26+
// in: query
27+
// description: page number of results to return (1-based)
28+
// type: integer
29+
// - name: limit
30+
// in: query
31+
// description: page size of results
32+
// type: integer
33+
// responses:
34+
// "200":
35+
// description: "SearchResults of authentication sources"
36+
// schema:
37+
// type: array
38+
// items:
39+
// "$ref": "#/definitions/AuthOauth2Option"
40+
// "403":
41+
// "$ref": "#/responses/forbidden"
42+
43+
listOptions := utils.GetListOptions(ctx)
44+
45+
authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
46+
if err != nil {
47+
ctx.APIErrorInternal(err)
48+
return
49+
}
50+
51+
results := make([]*api.AuthSourceOption, len(authSources))
52+
for i := range authSources {
53+
results[i] = convert.ToOauthProvider(ctx, authSources[i])
54+
}
55+
56+
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
57+
ctx.SetTotalCountHeader(maxResults)
58+
ctx.JSON(http.StatusOK, &results)
59+
}

routers/api/v1/admin/auth_oauth.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,15 @@ func SearchOauthAuth(ctx *context.APIContext) {
251251

252252
listOptions := utils.GetListOptions(ctx)
253253

254-
authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
254+
authSources, maxResults, err := db.FindAndCount[auth_model.Source](ctx, auth_model.FindSourcesOptions{
255+
LoginType: auth_model.OAuth2,
256+
})
255257
if err != nil {
256258
ctx.APIErrorInternal(err)
257259
return
258260
}
259261

260-
results := make([]*api.AuthOauth2Option, len(authSources))
262+
results := make([]*api.AuthSourceOption, len(authSources))
261263
for i := range authSources {
262264
results[i] = convert.ToOauthProvider(ctx, authSources[i])
263265
}

services/convert/auth_oauth.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// ToOauthProvider convert auth_model.Source≤ to api.AuthOauth2Option
14-
func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
14+
func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthSourceOption {
1515
if provider == nil {
1616
return nil
1717
}
@@ -20,16 +20,16 @@ func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.Auth
2020
}
2121

2222
// ToOauthProviders convert list of auth_model.Source to list of api.AuthOauth2Option
23-
func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthOauth2Option {
24-
result := make([]*api.AuthOauth2Option, len(provider))
23+
func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthSourceOption {
24+
result := make([]*api.AuthSourceOption, len(provider))
2525
for i := range provider {
2626
result[i] = ToOauthProvider(ctx, provider[i])
2727
}
2828
return result
2929
}
3030

31-
func toOauthProvider(provider *auth_model.Source) *api.AuthOauth2Option {
32-
return &api.AuthOauth2Option{
31+
func toOauthProvider(provider *auth_model.Source) *api.AuthSourceOption {
32+
return &api.AuthSourceOption{
3333
ID: provider.ID,
3434
AuthenticationName: provider.Name,
3535
TypeName: provider.Type.String(),

0 commit comments

Comments
 (0)