summaryrefslogtreecommitdiffstats
path: root/api/license.go
blob: 06bde2b6cc20454cf18adf7989fd3f0186bb3e37 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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
		}

		if err := writeFileLocally(data, utils.LicenseLocation()); err != nil {
			c.LogAudit("failed - could not save license file")
			c.Err = model.NewAppError("addLicense", "License did not save properly.", "path="+utils.LicenseLocation())
			utils.RemoveLicense()
			return
		}
	} 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("")

	if ok := utils.RemoveLicense(); !ok {
		c.LogAudit("failed - could not remove license file")
		c.Err = model.NewAppError("removeLicense", "License did not remove properly.", "")
		return
	}

	rdata := map[string]string{}
	rdata["status"] = "ok"
	w.Write([]byte(model.MapToJson(rdata)))
}