summaryrefslogtreecommitdiffstats
path: root/app/extension.go
blob: d0226bba5869a1ed52287196abc20a71ac5f98d0 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"github.com/mattermost/mattermost-server/model"
	"html/template"
	"net/http"
)

func (a *App) isExtensionSupportEnabled() bool {
	return *a.Config().ExtensionSettings.EnableExperimentalExtensions
}

func (a *App) isExtensionValid(extensionId string) bool {
	extensionIsValid := false
	extensionIDs := a.Config().ExtensionSettings.AllowedExtensionsIDs

	for _, id := range extensionIDs {
		if extensionId == id {
			extensionIsValid = true
		}
	}

	return extensionIsValid
}

func (a *App) ValidateExtension(extensionId string) *model.AppError {
	enabled := a.isExtensionSupportEnabled()
	if !enabled {
		return model.NewAppError("completeSaml", "api.user.saml.extension_unsupported", nil, "", http.StatusInternalServerError)
	}

	valid := a.isExtensionValid(extensionId)
	if !valid {
		params := map[string]interface{}{"ExtensionId": extensionId}
		return model.NewAppError("completeSaml", "api.user.saml.invalid_extension", params, "", http.StatusInternalServerError)
	}

	return nil
}

func (a *App) SendMessageToExtension(w http.ResponseWriter, extensionId string, token string) *model.AppError {
	var err error
	var t *template.Template
	if len(extensionId) == 0 {
		return model.NewAppError("completeSaml", "api.user.saml.extension_id.app_error", nil, "", http.StatusInternalServerError)
	}

	t = template.New("complete_saml_extension_body")
	t, err = t.ParseFiles("templates/complete_saml_extension_body.html")

	if err != nil {
		return model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)

	var errMessage string
	if len(token) == 0 {
		loginError := model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "", http.StatusInternalServerError)
		errMessage = loginError.Message
	}

	data := struct {
		ExtensionId string
		Token       string
		Error       string
	}{
		extensionId,
		token,
		errMessage,
	}

	if err := t.Execute(w, data); err != nil {
		return model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
	}

	return nil
}