2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
2017-03-13 08:26:23 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2017-03-13 08:26:23 -04:00
|
|
|
)
|
|
|
|
|
|
2017-09-22 12:54:27 -05:00
|
|
|
func (api *API) InitSaml() {
|
|
|
|
|
api.BaseRoutes.SAML.Handle("/metadata", api.ApiHandler(getSamlMetadata)).Methods("GET")
|
2017-03-13 08:26:23 -04:00
|
|
|
|
2017-09-22 12:54:27 -05:00
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/public", api.ApiSessionRequired(addSamlPublicCertificate)).Methods("POST")
|
|
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/private", api.ApiSessionRequired(addSamlPrivateCertificate)).Methods("POST")
|
|
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/idp", api.ApiSessionRequired(addSamlIdpCertificate)).Methods("POST")
|
2017-03-13 08:26:23 -04:00
|
|
|
|
2017-09-22 12:54:27 -05:00
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/public", api.ApiSessionRequired(removeSamlPublicCertificate)).Methods("DELETE")
|
|
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/private", api.ApiSessionRequired(removeSamlPrivateCertificate)).Methods("DELETE")
|
|
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/idp", api.ApiSessionRequired(removeSamlIdpCertificate)).Methods("DELETE")
|
2017-03-13 08:26:23 -04:00
|
|
|
|
2017-09-22 12:54:27 -05:00
|
|
|
api.BaseRoutes.SAML.Handle("/certificate/status", api.ApiSessionRequired(getSamlCertificateStatus)).Methods("GET")
|
2017-03-13 08:26:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSamlMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
|
2017-09-19 18:31:35 -05:00
|
|
|
metadata, err := c.App.GetSamlMetadata()
|
2017-03-13 08:26:23 -04:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\"metadata.xml\"")
|
|
|
|
|
w.Write([]byte(metadata))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 09:39:31 -05:00
|
|
|
func parseSamlCertificateRequest(r *http.Request, maxFileSize int64) (*multipart.FileHeader, *model.AppError) {
|
|
|
|
|
err := r.ParseMultipartForm(maxFileSize)
|
2017-03-13 08:26:23 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, model.NewAppError("addSamlCertificate", "api.admin.add_certificate.no_file.app_error", nil, err.Error(), http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := r.MultipartForm
|
|
|
|
|
|
|
|
|
|
fileArray, ok := m.File["certificate"]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, model.NewAppError("addSamlCertificate", "api.admin.add_certificate.no_file.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(fileArray) <= 0 {
|
|
|
|
|
return nil, model.NewAppError("addSamlCertificate", "api.admin.add_certificate.array.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileArray[0], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addSamlPublicCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 09:39:31 -05:00
|
|
|
fileData, err := parseSamlCertificateRequest(r, *c.App.Config().FileSettings.MaxFileSize)
|
2017-03-13 08:26:23 -04:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.AddSamlPublicCertificate(fileData); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addSamlPrivateCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 09:39:31 -05:00
|
|
|
fileData, err := parseSamlCertificateRequest(r, *c.App.Config().FileSettings.MaxFileSize)
|
2017-03-13 08:26:23 -04:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.AddSamlPrivateCertificate(fileData); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addSamlIdpCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 09:39:31 -05:00
|
|
|
fileData, err := parseSamlCertificateRequest(r, *c.App.Config().FileSettings.MaxFileSize)
|
2017-03-13 08:26:23 -04:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.AddSamlIdpCertificate(fileData); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeSamlPublicCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.RemoveSamlPublicCertificate(); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeSamlPrivateCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.RemoveSamlPrivateCertificate(); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeSamlIdpCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
if err := c.App.RemoveSamlIdpCertificate(); err != nil {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSamlCertificateStatus(c *Context, w http.ResponseWriter, r *http.Request) {
|
2018-11-28 10:56:21 -08:00
|
|
|
if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
|
2017-03-13 08:26:23 -04:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 14:21:22 -05:00
|
|
|
status := c.App.GetSamlCertificateStatus()
|
2017-03-13 08:26:23 -04:00
|
|
|
w.Write([]byte(status.ToJson()))
|
|
|
|
|
}
|