forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml.go
188 lines (140 loc) · 4.89 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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
func (a *App) GetSamlMetadata() (string, *model.AppError) {
if a.Saml == nil {
err := model.NewAppError("GetSamlMetadata", "api.admin.saml.not_available.app_error", nil, "", http.StatusNotImplemented)
return "", err
}
if result, err := a.Saml.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.NewAppError("AddSamlCertificate", "api.admin.add_certificate.saving.app_error", nil, "", http.StatusBadRequest)
}
file, err := fileData.Open()
if err != nil {
return model.NewAppError("AddSamlCertificate", "api.admin.add_certificate.open.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer file.Close()
configDir, _ := utils.FindDir("config")
out, err := os.Create(configDir + filename)
if err != nil {
return model.NewAppError("AddSamlCertificate", "api.admin.add_certificate.saving.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer out.Close()
io.Copy(out, file)
return nil
}
func (a *App) AddSamlPublicCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.PublicCertificateFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func (a *App) AddSamlPrivateCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.PrivateKeyFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func (a *App) AddSamlIdpCertificate(fileData *multipart.FileHeader) *model.AppError {
if err := WriteSamlFile(fileData); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.IdpCertificateFile = fileData.Filename
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func RemoveSamlFile(filename string) *model.AppError {
filename = filepath.Base(filename)
if filename == "." || filename == string(filepath.Separator) {
return model.NewAppError("AddSamlCertificate", "api.admin.remove_certificate.delete.app_error", nil, "", http.StatusBadRequest)
}
if err := os.Remove(utils.FindConfigFile(filename)); err != nil {
return model.NewAppError("removeCertificate", "api.admin.remove_certificate.delete.app_error", map[string]interface{}{"Filename": filename}, err.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) RemoveSamlPublicCertificate() *model.AppError {
if err := RemoveSamlFile(*a.Config().SamlSettings.PublicCertificateFile); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.PublicCertificateFile = ""
*cfg.SamlSettings.Encrypt = false
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func (a *App) RemoveSamlPrivateCertificate() *model.AppError {
if err := RemoveSamlFile(*a.Config().SamlSettings.PrivateKeyFile); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.PrivateKeyFile = ""
*cfg.SamlSettings.Encrypt = false
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func (a *App) RemoveSamlIdpCertificate() *model.AppError {
if err := RemoveSamlFile(*a.Config().SamlSettings.IdpCertificateFile); err != nil {
return err
}
cfg := a.Config().Clone()
*cfg.SamlSettings.IdpCertificateFile = ""
*cfg.SamlSettings.Enable = false
if err := cfg.IsValid(); err != nil {
return err
}
a.UpdateConfig(func(dest *model.Config) { *dest = *cfg })
a.PersistConfig()
return nil
}
func (a *App) GetSamlCertificateStatus() *model.SamlCertificateStatus {
status := &model.SamlCertificateStatus{}
status.IdpCertificateFile = utils.FileExistsInConfigFolder(*a.Config().SamlSettings.IdpCertificateFile)
status.PrivateKeyFile = utils.FileExistsInConfigFolder(*a.Config().SamlSettings.PrivateKeyFile)
status.PublicCertificateFile = utils.FileExistsInConfigFolder(*a.Config().SamlSettings.PublicCertificateFile)
return status
}