1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-04 21:15:24 +02:00

support for license keys

This commit is contained in:
Harvey Kandola 2017-02-22 20:03:40 -08:00
parent b0d70e2657
commit 3f87401d49
10 changed files with 252 additions and 35 deletions

View file

@ -16,13 +16,15 @@ import (
"io/ioutil"
"net/http"
"encoding/xml"
"fmt"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/api/util"
)
// GetGlobalConfig returns installation-wide settings
func GetGlobalConfig(w http.ResponseWriter, r *http.Request) {
method := "GetGlobalConfig"
// GetSMTPConfig returns installation-wide SMTP settings
func GetSMTPConfig(w http.ResponseWriter, r *http.Request) {
method := "GetSMTPConfig"
p := request.GetPersister(r)
if !p.Context.Global {
@ -39,16 +41,16 @@ func GetGlobalConfig(w http.ResponseWriter, r *http.Request) {
json, err := json.Marshal(y)
if err != nil {
writeJSONMarshalError(w, method, "GetGlobalConfig", err)
writeJSONMarshalError(w, method, "SMTP", err)
return
}
util.WriteSuccessBytes(w, json)
}
// SaveGlobalConfig persists global configuration.
func SaveGlobalConfig(w http.ResponseWriter, r *http.Request) {
method := "SaveGlobalConfig"
// SaveSMTPConfig persists global SMTP configuration.
func SaveSMTPConfig(w http.ResponseWriter, r *http.Request) {
method := "SaveSMTPConfig"
p := request.GetPersister(r)
if !p.Context.Global {
@ -79,3 +81,110 @@ func SaveGlobalConfig(w http.ResponseWriter, r *http.Request) {
util.WriteSuccessEmptyJSON(w)
}
// GetLicense returns product license
func GetLicense(w http.ResponseWriter, r *http.Request) {
// method := "GetLicense"
p := request.GetPersister(r)
if !p.Context.Global {
writeForbiddenError(w)
return
}
// SMTP settings
config := request.ConfigString("EDITION-LICENSE", "")
if len(config) == 0 {
config = "{}"
}
x := &licenseXML{Key: "", Signature: ""}
lj := licenseJSON{}
err := json.Unmarshal([]byte(config), &lj)
if err == nil {
x.Key = lj.Key
x.Signature = lj.Signature
} else {
fmt.Println(err)
}
output, err := xml.MarshalIndent(x, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
w.WriteHeader(http.StatusOK)
w.Write(output)
// // marshal as JSON
// var y map[string]interface{}
// json.Unmarshal([]byte(config), &y)
// json, err := json.Marshal(y)
// if err != nil {
// writeJSONMarshalError(w, method, "EDITION-LICENSE", err)
// return
// }
// util.WriteSuccessBytes(w, json)
}
// SaveLicense persists product license
func SaveLicense(w http.ResponseWriter, r *http.Request) {
method := "SaveLicense"
p := request.GetPersister(r)
if !p.Context.Global {
writeForbiddenError(w)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writePayloadError(w, method, err)
return
}
var config string
config = string(body)
lj := licenseJSON{}
x := licenseXML{Key: "", Signature: ""}
err = xml.Unmarshal([]byte(config), &x)
if err == nil {
lj.Key = x.Key
lj.Signature = x.Signature
} else {
fmt.Println(err)
}
j, err := json.Marshal(lj)
js := "{}"
if err == nil {
js = string(j)
}
request.ConfigSet("EDITION-LICENSE", js)
util.WriteSuccessEmptyJSON(w)
}
type licenseXML struct {
XMLName xml.Name `xml:"DocumizeLicense"`
Key string
Signature string
}
type licenseJSON struct {
Key string `json:"key"`
Signature string `json:"signature"`
}
/*
<DocumizeLicense>
<Key>some key</Key>
<Signature>some signature</Signature>
</DocumizeLicense>
*/

View file

@ -229,8 +229,10 @@ func init() {
log.IfErr(Add(RoutePrefixPrivate, "documents/{documentID}/links", []string{"GET", "OPTIONS"}, nil, GetDocumentLinks))
// Global installation-wide config
log.IfErr(Add(RoutePrefixPrivate, "global", []string{"GET", "OPTIONS"}, nil, GetGlobalConfig))
log.IfErr(Add(RoutePrefixPrivate, "global", []string{"PUT", "OPTIONS"}, nil, SaveGlobalConfig))
log.IfErr(Add(RoutePrefixPrivate, "global/smtp", []string{"GET", "OPTIONS"}, nil, GetSMTPConfig))
log.IfErr(Add(RoutePrefixPrivate, "global/smtp", []string{"PUT", "OPTIONS"}, nil, SaveSMTPConfig))
log.IfErr(Add(RoutePrefixPrivate, "global/license", []string{"GET", "OPTIONS"}, nil, GetLicense))
log.IfErr(Add(RoutePrefixPrivate, "global/license", []string{"PUT", "OPTIONS"}, nil, SaveLicense))
// Pinned items
log.IfErr(Add(RoutePrefixPrivate, "pin/{userID}", []string{"POST", "OPTIONS"}, nil, AddPin))

View file

@ -38,6 +38,9 @@ func init() {
environment.GetString(&keyFile, "key", false, "the key.pem file used for https", nil)
environment.GetString(&port, "port", false, "http/https port number", nil)
environment.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
// license state
log.Info(Product.License.Status())
}
var testHost string // used during automated testing

View file

@ -12,6 +12,7 @@
package core
import "fmt"
import "time"
// ProdInfo describes a product
type ProdInfo struct {
@ -21,6 +22,7 @@ type ProdInfo struct {
Major string
Minor string
Patch string
License License
}
// Product returns product edition details
@ -31,6 +33,54 @@ func Product() (p ProdInfo) {
p.Version = fmt.Sprintf("%s.%s.%s", p.Major, p.Minor, p.Patch)
p.Edition = "Community"
p.Title = fmt.Sprintf("%s Edition", p.Edition)
p.License = License{}
return p
return
}
// License holds details of product license.
type License struct {
Name string `json:"name"`
Email string `json:"email"`
Edition string `json:"edition"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Seats int `json:"seats"`
Trial bool `json:"trial"`
}
// IsEmpty determines if we have a license.
func (l *License) IsEmpty() bool {
return l.Seats == 0 && len(l.Name) == 0 && len(l.Email) == 0 && l.Start.Year() == 1 && l.End.Year() == 1
}
// IsValid determines if we have populated and valid license.
// An empty license is valid.
func (l *License) IsValid() bool {
if l.IsEmpty() {
return true
}
now := time.Now().UTC()
return l.End.Day() <= now.Day() && l.End.Month() <= now.Month() && l.End.Year() <= now.Year()
}
// Status returns formatted message stating if license is empty/populated and invalid/valid.
func (l *License) Status() string {
lp := "populated"
if l.IsEmpty() {
lp = "empty"
}
lv := "invalid"
if l.IsValid() {
lv = "valid"
}
return fmt.Sprintf("License is %s and %s", lp, lv)
}
// LicenseData holds encrypted data and is unpacked into License.
type LicenseData struct {
Key string `json:"key"`
Signature string `json:"signature"`
}