mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-18 21:42:56 +02:00
Add OAuth2 provider listing and conversion functions
This commit is contained in:
parent
17bd1e0909
commit
c2f5544d73
@ -4,6 +4,12 @@
|
||||
|
||||
package structs
|
||||
|
||||
type AuthOauth2Option struct {
|
||||
SourceID int64 `json:"source_id"`
|
||||
AuthenticationName string `json:"authentication_name" binding:"Required"`
|
||||
ProviderIconURL string `json:"provider_icon_url"`
|
||||
}
|
||||
|
||||
// CreateUserOption create user options
|
||||
type CreateAuthOauth2Option struct {
|
||||
AuthenticationName string `json:"authentication_name" binding:"Required"`
|
||||
|
@ -11,10 +11,14 @@ import (
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
|
||||
"code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/auth/source/oauth2"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
)
|
||||
|
||||
// CreateOauthAuth create a new external authentication for oauth2
|
||||
@ -77,5 +81,22 @@ func DeleteOauthAuth(ctx *context.APIContext) {
|
||||
|
||||
// // SearchOauthAuth API for getting information of the configured authentication methods according the filter conditions
|
||||
func SearchOauthAuth(ctx *context.APIContext) {
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
|
||||
authSources, maxResults, err := db.FindAndCount[auth.Source](ctx, auth.FindSourcesOptions{})
|
||||
// fmt.Printf("Count: %d, models: %v, err: %v", count, models[0].Name, err)
|
||||
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
|
||||
results := make([]*api.AuthOauth2Option, 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)
|
||||
}
|
||||
|
@ -1650,6 +1650,7 @@ func Routes() *web.Router {
|
||||
|
||||
m.Group("/admin", func() {
|
||||
m.Group("/identity-auth", func() {
|
||||
m.Get("", admin.SearchOauthAuth)
|
||||
m.Post("/new", admin.CreateOauthAuth)
|
||||
})
|
||||
|
||||
|
37
services/convert/auth_oauth.go
Normal file
37
services/convert/auth_oauth.go
Normal file
@ -0,0 +1,37 @@
|
||||
// // Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// // SPDX-License-Identifier: MIT
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToUser convert user_model.User to api.User
|
||||
// if doer is set, private information is added if the doer has the permission to see it
|
||||
func ToOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
|
||||
if provider == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return toOauthProvider(ctx, provider)
|
||||
}
|
||||
|
||||
// ToUsers convert list of user_model.User to list of api.User
|
||||
func ToOauthProviders(ctx context.Context, provider []*auth_model.Source) []*api.AuthOauth2Option {
|
||||
result := make([]*api.AuthOauth2Option, len(provider))
|
||||
for i := range provider {
|
||||
result[i] = ToOauthProvider(ctx, provider[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toOauthProvider(ctx context.Context, provider *auth_model.Source) *api.AuthOauth2Option {
|
||||
return &api.AuthOauth2Option{
|
||||
SourceID: provider.ID,
|
||||
AuthenticationName: provider.Name,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user