summaryrefslogtreecommitdiffstats
path: root/app/saml.go
blob: cc39d45407fecb131409c477234c244442226eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"io"
	"mime/multipart"
	"net/http"
	"os"

	"github.com/mattermost/platform/einterfaces"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"
)

func GetSamlMetadata() (string, *model.AppError) {
	samlInterface := einterfaces.GetSamlInterface()

	if samlInterface == nil {
		err := model.NewLocAppError("GetSamlMetadata", "api.admin.saml.not_available.app_error", nil, "")
		err.StatusCode = http.StatusNotImplemented
		return "", err
	}

	if result, err := samlInterface.GetMetadata(); err != nil {
		return "", model.NewLocAppError("GetSamlMetadata", "api.admin.saml.metadata.app_error", nil, "err="+err.Message)
	} else {
		return result, nil
	}
}

func AddSamlCertificate(fileData *multipart.FileHeader) *model.AppError {
	file, err := fileData.Open()
	defer file.Close()
	if err != nil {
		return model.NewLocAppError("AddSamlCertificate", "api.admin.add_certificate.open.app_error", nil, err.Error())
	}

	out, err := os.Create(utils.FindDir("config") + fileData.Filename)
	if err != nil {
		return model.NewLocAppError("AddSamlCertificate", "api.admin.add_certificate.saving.app_error", nil, err.Error())
	}
	defer out.Close()

	io.Copy(out, file)
	return nil
}

func RemoveSamlCertificate(filename string) *model.AppError {
	if err := os.Remove(utils.FindConfigFile(filename)); err != nil {
		return model.NewLocAppError("removeCertificate", "api.admin.remove_certificate.delete.app_error",
			map[string]interface{}{"Filename": filename}, err.Error())
	}

	return nil
}

func GetSamlCertificateStatus() map[string]interface{} {
	status := make(map[string]interface{})

	status["IdpCertificateFile"] = utils.FileExistsInConfigFolder(*utils.Cfg.SamlSettings.IdpCertificateFile)
	status["PrivateKeyFile"] = utils.FileExistsInConfigFolder(*utils.Cfg.SamlSettings.PrivateKeyFile)
	status["PublicCertificateFile"] = utils.FileExistsInConfigFolder(*utils.Cfg.SamlSettings.PublicCertificateFile)

	return status
}