[MM-30539] New renewal link logic (#16539)

* New renewal link logic

Endpoint and logic that returns the renewal link to be used to start the
license renewal process.

* Limit access for restricted sysadmins

* Include active users in the renewal token
Этот коммит содержится в:
Mario de Frutos Dieguez
2020-12-18 16:40:46 +01:00
коммит произвёл GitHub
родитель e057e5b10b
Коммит 94fe01dc1d
27 изменённых файлов: 1867 добавлений и 13 удалений

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

@@ -6,10 +6,12 @@ package api4
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/mattermost/mattermost-server/v5/app"
"github.com/mattermost/mattermost-server/v5/audit"
"github.com/mattermost/mattermost-server/v5/model"
)
@@ -18,6 +20,7 @@ func (api *API) InitLicense() {
api.BaseRoutes.ApiRoot.Handle("/trial-license", api.ApiSessionRequired(requestTrialLicense)).Methods("POST")
api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(addLicense)).Methods("POST")
api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(removeLicense)).Methods("DELETE")
api.BaseRoutes.ApiRoot.Handle("/license/renewal", api.ApiSessionRequired(requestRenewalLink)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/license/client", api.ApiHandler(getClientLicense)).Methods("GET")
}
@@ -210,3 +213,35 @@ func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
ReturnStatusOK(w)
}
func requestRenewalLink(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec := c.MakeAuditRecord("requestRenewalLink", audit.Fail)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_SYSCONSOLE_WRITE_ABOUT) {
c.SetPermissionError(model.PERMISSION_SYSCONSOLE_WRITE_ABOUT)
return
}
if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
c.Err = model.NewAppError("requestRenewalLink", "api.restricted_system_admin", nil, "", http.StatusForbidden)
return
}
renewalToken, err := c.App.Srv().GenerateRenewalToken(app.JWTDefaultTokenExpiration)
if err != nil {
c.Err = err
return
}
renewalLink := app.LicenseRenewalURL + "?token=" + renewalToken
auditRec.Success()
c.LogAudit("success")
_, werr := w.Write([]byte(fmt.Sprintf(`{"renewal_link": "%s"}`, renewalLink)))
if werr != nil {
c.Err = model.NewAppError("requestRenewalLink", "api.license.request_renewal_link.app_error", nil, werr.Error(), http.StatusForbidden)
return
}
}