mirror of
https://github.com/go-gitea/gitea.git
synced 2025-08-14 13:53:46 +00:00
Implement update patch method for api
This commit is contained in:
parent
cb0e0cef86
commit
c31df2562f
@ -39,9 +39,6 @@ type CreateAuthOauth2Option struct {
|
||||
|
||||
// EditUserOption edit user options
|
||||
type EditAuthOauth2Option struct {
|
||||
// // required: true
|
||||
SourceID int64 `json:"source_id"`
|
||||
|
||||
AuthenticationName string `json:"authentication_name" binding:"Required"`
|
||||
ProviderIconURL string `json:"provider_icon_url"`
|
||||
ProviderClientID string `json:"provider_client_id" binding:"Required"`
|
||||
|
@ -24,15 +24,6 @@ import (
|
||||
func CreateOauthAuth(ctx *context.APIContext) {
|
||||
form := web.GetForm(ctx).(*api.CreateAuthOauth2Option)
|
||||
|
||||
// ??? todo: what should I do here?
|
||||
var scopes []string
|
||||
// for _, s := range strings.Split(form.Oauth2Scopes, ",") {
|
||||
// s = strings.TrimSpace(s)
|
||||
// if s != "" {
|
||||
// scopes = append(scopes, s)
|
||||
// }
|
||||
// }
|
||||
|
||||
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)
|
||||
@ -46,7 +37,7 @@ func CreateOauthAuth(ctx *context.APIContext) {
|
||||
OpenIDConnectAutoDiscoveryURL: form.ProviderAutoDiscoveryURL,
|
||||
CustomURLMapping: nil,
|
||||
IconURL: form.ProviderIconURL,
|
||||
Scopes: scopes,
|
||||
Scopes: generateScopes(),
|
||||
RequiredClaimName: form.RequiredClaimName,
|
||||
RequiredClaimValue: form.RequiredClaimValue,
|
||||
SkipLocalTwoFA: form.SkipLocal2FA,
|
||||
@ -75,6 +66,47 @@ func CreateOauthAuth(ctx *context.APIContext) {
|
||||
|
||||
// EditOauthAuth api for modifying a authentication method
|
||||
func EditOauthAuth(ctx *context.APIContext) {
|
||||
oauthIDString := ctx.PathParam("id")
|
||||
oauthID, oauthIDErr := strconv.Atoi(oauthIDString)
|
||||
if oauthIDErr != nil {
|
||||
ctx.APIErrorInternal(oauthIDErr)
|
||||
}
|
||||
|
||||
form := web.GetForm(ctx).(*api.CreateAuthOauth2Option)
|
||||
|
||||
config := &oauth2.Source{
|
||||
Provider: "openidConnect",
|
||||
ClientID: form.ProviderClientID,
|
||||
ClientSecret: form.ProviderClientSecret,
|
||||
OpenIDConnectAutoDiscoveryURL: form.ProviderAutoDiscoveryURL,
|
||||
CustomURLMapping: nil,
|
||||
IconURL: form.ProviderIconURL,
|
||||
Scopes: generateScopes(),
|
||||
RequiredClaimName: form.RequiredClaimName,
|
||||
RequiredClaimValue: form.RequiredClaimValue,
|
||||
SkipLocalTwoFA: form.SkipLocal2FA,
|
||||
|
||||
GroupClaimName: form.ClaimNameProvidingGroupNameForSource,
|
||||
RestrictedGroup: form.GroupClaimValueForRestrictedUsers,
|
||||
AdminGroup: form.GroupClaimValueForAdministratorUsers,
|
||||
GroupTeamMap: form.MapClaimedGroupsToOrganizationTeams,
|
||||
GroupTeamMapRemoval: form.RemoveUsersFromSyncronizedTeams,
|
||||
}
|
||||
|
||||
updateErr := auth_model.UpdateSource(ctx, &auth_model.Source{
|
||||
ID: int64(oauthID),
|
||||
Type: auth_model.OAuth2,
|
||||
Name: form.AuthenticationName,
|
||||
IsActive: true,
|
||||
Cfg: config,
|
||||
})
|
||||
|
||||
if updateErr != nil {
|
||||
ctx.APIErrorInternal(updateErr)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
// DeleteOauthAuth api for deleting a authentication method
|
||||
@ -85,6 +117,17 @@ func DeleteOauthAuth(ctx *context.APIContext) {
|
||||
ctx.APIErrorInternal(oauthIDErr)
|
||||
}
|
||||
|
||||
source, sourceErr := auth_model.GetSourceByID(ctx, int64(oauthID))
|
||||
if sourceErr != nil {
|
||||
ctx.APIErrorInternal(sourceErr)
|
||||
return
|
||||
}
|
||||
|
||||
if source.Type != auth_model.OAuth2 {
|
||||
ctx.APIErrorNotFound()
|
||||
return
|
||||
}
|
||||
|
||||
err := auth_model.DeleteSource(ctx, int64(oauthID))
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
@ -113,3 +156,17 @@ func SearchOauthAuth(ctx *context.APIContext) {
|
||||
ctx.SetTotalCountHeader(maxResults)
|
||||
ctx.JSON(http.StatusOK, &results)
|
||||
}
|
||||
|
||||
// ??? todo: what should I do here?
|
||||
func generateScopes() []string {
|
||||
var scopes []string
|
||||
|
||||
// for _, s := range strings.Split(form.Oauth2Scopes, ",") {
|
||||
// s = strings.TrimSpace(s)
|
||||
// if s != "" {
|
||||
// scopes = append(scopes, s)
|
||||
// }
|
||||
// }
|
||||
|
||||
return scopes
|
||||
}
|
||||
|
@ -1660,7 +1660,8 @@ func Routes() *web.Router {
|
||||
m.Group("/identity-auth", func() {
|
||||
m.Group("/oauth", func() {
|
||||
m.Get("", admin.SearchOauthAuth)
|
||||
m.Put("/new", bind(api.CreateAuthOauth2Option{}), admin.CreateOauthAuth)
|
||||
m.Put("", bind(api.CreateAuthOauth2Option{}), admin.CreateOauthAuth)
|
||||
m.Patch("/{id}", bind(api.EditAuthOauth2Option{}), admin.EditOauthAuth)
|
||||
m.Delete("/{id}", admin.DeleteOauthAuth)
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user