forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml.go
201 lines (148 loc) · 4.85 KB
/
saml.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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"
"path/filepath"
)
func GetSamlMetadata() (string, *model.AppError) {
samlInterface := einterfaces.GetSamlInterface()
if samlInterface == nil {
err := model.NewAppError("GetSamlMetadata", "api.admin.saml.not_available.app_error", nil, "", http.StatusNotImplemented)
return "", err
}
if result, err := samlInterface.GetMetadata(); err != nil {
return "", model.NewAppError("GetSamlMetadata", "api.admin.saml.metadata.app_error", nil, "err="+err.Message, err.StatusCode)
} else {
return result, nil
}
}
func WriteSamlFile(fileData *multipart.FileHeader) *model.AppError {
filename := filepath.Base(fileData.Filename)
if filename == "." || filename == string(filepath.Separator) {
return model.NewLocAppError("AddSamlCertificate", "api.admin.add_certificate.saving.app_error", nil, "")
}
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") + 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 AddSamlPublicCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.PublicCertificateFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func AddSamlPrivateCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.PrivateKeyFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func AddSamlIdpCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.IdpCertificateFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func RemoveSamlFile(filename string) *model.AppError {
filename = filepath.Base(filename)
if filename == "." || filename == string(filepath.Separator) {
return model.NewLocAppError("AddSamlCertificate", "api.admin.remove_certificate.delete.app_error", nil, "")
}
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 RemoveSamlPublicCertificate() *model.AppError {
if err := RemoveSamlFile(*utils.Cfg.SamlSettings.PublicCertificateFile); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.PublicCertificateFile = ""
*cfg.SamlSettings.Encrypt = false
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func RemoveSamlPrivateCertificate() *model.AppError {
if err := RemoveSamlFile(*utils.Cfg.SamlSettings.PrivateKeyFile); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.PrivateKeyFile = ""
*cfg.SamlSettings.Encrypt = false
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func RemoveSamlIdpCertificate() *model.AppError {
if err := RemoveSamlFile(*utils.Cfg.SamlSettings.IdpCertificateFile); err != nil {
return err
}
cfg := &model.Config{}
*cfg = *utils.Cfg
*cfg.SamlSettings.IdpCertificateFile = ""
*cfg.SamlSettings.Enable = false
if err := cfg.IsValid(); err != nil {
return err
}
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
return nil
}
func GetSamlCertificateStatus() *model.SamlCertificateStatus {
status := &model.SamlCertificateStatus{}
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
}