github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/extension.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/model"
     8  	"html/template"
     9  	"net/http"
    10  )
    11  
    12  func (a *App) isExtensionSupportEnabled() bool {
    13  	return *a.Config().ExtensionSettings.EnableExperimentalExtensions
    14  }
    15  
    16  func (a *App) isExtensionValid(extensionId string) bool {
    17  	extensionIsValid := false
    18  	extensionIDs := a.Config().ExtensionSettings.AllowedExtensionsIDs
    19  
    20  	for _, id := range extensionIDs {
    21  		if extensionId == id {
    22  			extensionIsValid = true
    23  		}
    24  	}
    25  
    26  	return extensionIsValid
    27  }
    28  
    29  func (a *App) ValidateExtension(extensionId string) *model.AppError {
    30  	enabled := a.isExtensionSupportEnabled()
    31  	if !enabled {
    32  		return model.NewAppError("completeSaml", "api.user.saml.extension_unsupported", nil, "", http.StatusInternalServerError)
    33  	}
    34  
    35  	valid := a.isExtensionValid(extensionId)
    36  	if !valid {
    37  		params := map[string]interface{}{"ExtensionId": extensionId}
    38  		return model.NewAppError("completeSaml", "api.user.saml.invalid_extension", params, "", http.StatusInternalServerError)
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  func (a *App) SendMessageToExtension(w http.ResponseWriter, extensionId string, token string) *model.AppError {
    45  	var err error
    46  	var t *template.Template
    47  	if len(extensionId) == 0 {
    48  		return model.NewAppError("completeSaml", "api.user.saml.extension_id.app_error", nil, "", http.StatusInternalServerError)
    49  	}
    50  
    51  	t = template.New("complete_saml_extension_body")
    52  	t, err = t.ParseFiles("templates/complete_saml_extension_body.html")
    53  
    54  	if err != nil {
    55  		return model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
    56  	}
    57  
    58  	w.Header().Set("Content-Type", "text/html")
    59  	w.WriteHeader(http.StatusOK)
    60  
    61  	var errMessage string
    62  	if len(token) == 0 {
    63  		loginError := model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "", http.StatusInternalServerError)
    64  		errMessage = loginError.Message
    65  	}
    66  
    67  	data := struct {
    68  		ExtensionId string
    69  		Token       string
    70  		Error       string
    71  	}{
    72  		extensionId,
    73  		token,
    74  		errMessage,
    75  	}
    76  
    77  	if err := t.Execute(w, data); err != nil {
    78  		return model.NewAppError("completeSaml", "api.user.saml.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
    79  	}
    80  
    81  	return nil
    82  }