Skip to content

Commit c2f5544

Browse files
committed
Add OAuth2 provider listing and conversion functions
1 parent 17bd1e0 commit c2f5544

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

modules/structs/auth_oauth2.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
package structs
66

7+
type AuthOauth2Option struct {
8+
SourceID int64 `json:"source_id"`
9+
AuthenticationName string `json:"authentication_name" binding:"Required"`
10+
ProviderIconURL string `json:"provider_icon_url"`
11+
}
12+
713
// CreateUserOption create user options
814
type CreateAuthOauth2Option struct {
915
AuthenticationName string `json:"authentication_name" binding:"Required"`

routers/api/v1/admin/auth_oauth.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import (
1111

1212
auth_model "code.gitea.io/gitea/models/auth"
1313
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/routers/api/v1/utils"
1415

16+
"code.gitea.io/gitea/models/auth"
17+
"code.gitea.io/gitea/models/db"
1518
"code.gitea.io/gitea/modules/web"
1619
"code.gitea.io/gitea/services/auth/source/oauth2"
1720
"code.gitea.io/gitea/services/context"
21+
"code.gitea.io/gitea/services/convert"
1822
)
1923

2024
// CreateOauthAuth create a new external authentication for oauth2
@@ -77,5 +81,22 @@ func DeleteOauthAuth(ctx *context.APIContext) {
7781

7882
// // SearchOauthAuth API for getting information of the configured authentication methods according the filter conditions
7983
func SearchOauthAuth(ctx *context.APIContext) {
84+
listOptions := utils.GetListOptions(ctx)
8085

86+
authSources, maxResults, err := db.FindAndCount[auth.Source](ctx, auth.FindSourcesOptions{})
87+
// fmt.Printf("Count: %d, models: %v, err: %v", count, models[0].Name, err)
88+
89+
if err != nil {
90+
ctx.APIErrorInternal(err)
91+
return
92+
}
93+
94+
results := make([]*api.AuthOauth2Option, len(authSources))
95+
for i := range authSources {
96+
results[i] = convert.ToOauthProvider(ctx, authSources[i])
97+
}
98+
99+
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
100+
ctx.SetTotalCountHeader(maxResults)
101+
ctx.JSON(http.StatusOK, &results)
81102
}

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,7 @@ func Routes() *web.Router {
16501650

16511651
m.Group("/admin", func() {
16521652
m.Group("/identity-auth", func() {
1653+
m.Get("", admin.SearchOauthAuth)
16531654
m.Post("/new", admin.CreateOauthAuth)
16541655
})
16551656

services/convert/auth_oauth.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// // Copyright 2020 The Gitea Authors. All rights reserved.
2+
// // SPDX-License-Identifier: MIT
3+
4+
package convert
5+
6+
import (
7+
"context"
8+
9+
auth_model "code.gitea.io/gitea/models/auth"
10+
api "code.gitea.io/gitea/modules/structs"
11+
)
12+
13+
// ToUser convert user_model.User to api.User
14+
// if doer is set, private information is added if the doer has the permission to see it
15+
func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
16+
if provider == nil {
17+
return nil
18+
}
19+
20+
return toOauthProvider(ctx, provider)
21+
}
22+
23+
// ToUsers convert list of user_model.User to list of api.User
24+
func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthOauth2Option {
25+
result := make([]*api.AuthOauth2Option, len(provider))
26+
for i := range provider {
27+
result[i] = ToOauthProvider(ctx, provider[i])
28+
}
29+
return result
30+
}
31+
32+
func toOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
33+
return &api.AuthOauth2Option{
34+
SourceID: provider.ID,
35+
AuthenticationName: provider.Name,
36+
}
37+
}

0 commit comments

Comments
 (0)