[MM-32044] Reset SAML auth data (#17161)

Automatic Merge
Этот коммит содержится в:
Max Erenberg
2021-04-12 18:46:30 -04:00
коммит произвёл GitHub
родитель 2de65cfb11
Коммит 869da7a78b
16 изменённых файлов: 292 добавлений и 3 удалений

Просмотреть файл

@@ -363,6 +363,8 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
api.BaseRoutes.Jobs = api.BaseRoutes.ApiRoot.PathPrefix("/jobs").Subrouter()
api.BaseRoutes.SAML = api.BaseRoutes.ApiRoot.PathPrefix("/saml").Subrouter()
api.InitUserLocal()
api.InitTeamLocal()
api.InitChannelLocal()
@@ -381,6 +383,7 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app.
api.InitImportLocal()
api.InitExportLocal()
api.InitJobLocal()
api.InitSamlLocal()
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))

Просмотреть файл

@@ -4,6 +4,7 @@
package api4
import (
"encoding/json"
"io/ioutil"
"mime"
"mime/multipart"
@@ -27,6 +28,12 @@ func (api *API) InitSaml() {
api.BaseRoutes.SAML.Handle("/certificate/status", api.ApiSessionRequired(getSamlCertificateStatus)).Methods("GET")
api.BaseRoutes.SAML.Handle("/metadatafromidp", api.ApiHandler(getSamlMetadataFromIdp)).Methods("POST")
api.BaseRoutes.SAML.Handle("/reset_auth_data", api.ApiSessionRequired(resetAuthDataToEmail)).Methods("POST")
}
func (api *API) InitSamlLocal() {
api.BaseRoutes.SAML.Handle("/reset_auth_data", api.ApiLocal(resetAuthDataToEmail)).Methods("POST")
}
func getSamlMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -247,3 +254,28 @@ func getSamlMetadataFromIdp(c *Context, w http.ResponseWriter, r *http.Request)
w.Write([]byte(metadata.ToJson()))
}
func resetAuthDataToEmail(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
type ResetAuthDataParams struct {
IncludeDeleted bool `json:"include_deleted"`
DryRun bool `json:"dry_run"`
SpecifiedUserIDs []string `json:"user_ids"`
}
var params *ResetAuthDataParams
jsonErr := json.NewDecoder(r.Body).Decode(&params)
if jsonErr != nil {
c.Err = model.NewAppError("resetAuthDataToEmail", "model.utils.decode_json.app_error", nil, jsonErr.Error(), http.StatusBadRequest)
return
}
numAffected, appErr := c.App.ResetSamlAuthDataToEmail(params.IncludeDeleted, params.DryRun, params.SpecifiedUserIDs)
if appErr != nil {
c.Err = appErr
return
}
b, _ := json.Marshal(map[string]interface{}{"num_affected": numAffected})
w.Write(b)
}

Просмотреть файл

@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v5/einterfaces/mocks"
"github.com/mattermost/mattermost-server/v5/model"
)
@@ -50,3 +51,23 @@ func TestSamlCompleteCSRFPass(t *testing.T) {
require.NotEqual(t, http.StatusUnauthorized, resp.StatusCode)
defer resp.Body.Close()
}
func TestSamlResetId(t *testing.T) {
th := SetupEnterprise(t).InitBasic()
defer th.TearDown()
th.App.Srv().Saml = &mocks.SamlInterface{}
user := th.BasicUser
_, appErr := th.App.UpdateUserAuth(user.Id, &model.UserAuth{
AuthData: model.NewString(model.NewId()),
AuthService: model.USER_AUTH_SERVICE_SAML,
})
require.Nil(t, appErr)
_, resp := th.Client.ResetSamlAuthDataToEmail(false, false, nil)
CheckForbiddenStatus(t, resp)
numAffected, resp := th.SystemAdminClient.ResetSamlAuthDataToEmail(false, false, nil)
CheckOKStatus(t, resp)
require.Equal(t, int64(1), numAffected)
}