summaryrefslogtreecommitdiffstats
path: root/api/license.go
diff options
context:
space:
mode:
authorJoramWilander <jwawilander@gmail.com>2016-01-04 12:44:22 -0500
committerJoramWilander <jwawilander@gmail.com>2016-01-14 08:24:09 -0500
commit9110dd54a15f3d0fcf6f60936e01d816b667b93c (patch)
treebe6ddca1c4dc47b71de4c7ad8af59cb21a594b56 /api/license.go
parent53b0cd8f2a24798c67505aa447b1d53b9f14197e (diff)
downloadchat-9110dd54a15f3d0fcf6f60936e01d816b667b93c.tar.gz
chat-9110dd54a15f3d0fcf6f60936e01d816b667b93c.tar.bz2
chat-9110dd54a15f3d0fcf6f60936e01d816b667b93c.zip
Added license validation and settings
Diffstat (limited to 'api/license.go')
-rw-r--r--api/license.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/api/license.go b/api/license.go
new file mode 100644
index 000000000..9ed2d2afb
--- /dev/null
+++ b/api/license.go
@@ -0,0 +1,95 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api
+
+import (
+ "bytes"
+ l4g "code.google.com/p/log4go"
+ "github.com/gorilla/mux"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+ "io"
+ "net/http"
+ "strings"
+)
+
+func InitLicense(r *mux.Router) {
+ l4g.Debug("Initializing license api routes")
+
+ sr := r.PathPrefix("/license").Subrouter()
+ sr.Handle("/add", ApiAdminSystemRequired(addLicense)).Methods("POST")
+ sr.Handle("/remove", ApiAdminSystemRequired(removeLicense)).Methods("POST")
+}
+
+func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.LogAudit("attempt")
+ err := r.ParseMultipartForm(model.MAX_FILE_SIZE)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ m := r.MultipartForm
+
+ fileArray, ok := m.File["license"]
+ if !ok {
+ c.Err = model.NewAppError("addLicense", "No file under 'license' in request", "")
+ c.Err.StatusCode = http.StatusBadRequest
+ return
+ }
+
+ if len(fileArray) <= 0 {
+ c.Err = model.NewAppError("addLicense", "Empty array under 'license' in request", "")
+ c.Err.StatusCode = http.StatusBadRequest
+ return
+ }
+
+ fileData := fileArray[0]
+
+ file, err := fileData.Open()
+ defer file.Close()
+ if err != nil {
+ c.Err = model.NewAppError("addLicense", "Could not open license file", err.Error())
+ return
+ }
+
+ buf := bytes.NewBuffer(nil)
+ io.Copy(buf, file)
+
+ data := buf.Bytes()
+
+ var license *model.License
+ if success, licenseStr := utils.ValidateLicense(data); success {
+ license = model.LicenseFromJson(strings.NewReader(licenseStr))
+
+ if ok := utils.SetLicense(license); !ok {
+ c.LogAudit("failed - expired or non-started license")
+ c.Err = model.NewAppError("addLicense", "License is either expired or has not yet started.", "")
+ return
+ }
+
+ go func() {
+ if err := writeFileLocally(data, utils.LICENSE_FILE_LOC); err != nil {
+ l4g.Error("Could not save license file")
+ }
+ }()
+ } else {
+ c.LogAudit("failed - invalid license")
+ c.Err = model.NewAppError("addLicense", "Invalid license file", "")
+ return
+ }
+
+ c.LogAudit("success")
+ w.Write([]byte(license.ToJson()))
+}
+
+func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.LogAudit("")
+
+ utils.RemoveLicense()
+
+ rdata := map[string]string{}
+ rdata["status"] = "ok"
+ w.Write([]byte(model.MapToJson(rdata)))
+}