diff --git a/modules/structs/auth_oauth2.go b/modules/structs/auth_oauth2.go index 4ba1e46de50..f7e10d7ef3a 100644 --- a/modules/structs/auth_oauth2.go +++ b/modules/structs/auth_oauth2.go @@ -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"` diff --git a/routers/api/v1/admin/auth_oauth.go b/routers/api/v1/admin/auth_oauth.go index 8d03e5e961e..21a4215eb79 100644 --- a/routers/api/v1/admin/auth_oauth.go +++ b/routers/api/v1/admin/auth_oauth.go @@ -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 +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index e579aa6f549..9b29a91e90f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -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) }) })