1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 23:59:47 +02:00

major package structure refactoring

This commit is contained in:
Harvey Kandola 2017-07-18 21:55:17 +01:00
parent 7b8cec9a6c
commit cf58f8164d
73 changed files with 549 additions and 389 deletions

View file

@ -1,31 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import (
"bytes"
h "html/template"
txt "text/template"
)
// EncodeTextTemplate encodes input using text/template
func EncodeTextTemplate(html string) (safe string, err error) {
var out bytes.Buffer
t, err := txt.New("foo").Parse(`{{define "T"}}{{.}}{{end}}`)
err = t.ExecuteTemplate(&out, "T", html)
return out.String(), err
}
// EncodeHTMLString encodes HTML string
func EncodeHTMLString(html string) (safe string) {
return h.HTMLEscapeString(html)
}

View file

@ -1,33 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import "testing"
func TestHTMLEncoding(t *testing.T) {
testHTML(t, "<script>alert('test')</script>", "&lt;script&gt;alert(&#39;test&#39;)&lt;/script&gt;")
text(t, "<script>alert('test')</script>", "<script>alert('test')</script>")
}
func testHTML(t *testing.T, in, out string) {
got := EncodeHTMLString(in)
if got != out {
t.Errorf("EncodeHTMLString `%s` got `%s` expected `%s`\n", in, got, out)
}
}
func text(t *testing.T, in, out string) {
got, _ := EncodeTextTemplate(in)
if got != out {
t.Errorf("Html encode `%s` got `%s` expected `%s`\n", in, got, out)
}
}

View file

@ -1,23 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import (
"net/http"
"github.com/gorilla/mux"
)
// Params returns the paramaters to a gorilla mux request.
func Params(r *http.Request) map[string]string {
return mux.Vars(r)
}

View file

@ -1,75 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import (
"strings"
"golang.org/x/net/html"
"github.com/documize/community/core/api/entity"
)
// GetContentLinks returns Documize generated <a> links.
// such links have an identifying attribute e.g. <a data-documize='true'...
func GetContentLinks(body string) (links []entity.Link) {
z := html.NewTokenizer(strings.NewReader(body))
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
// End of the document, we're done
return
case tt == html.StartTagToken:
t := z.Token()
// Check if the token is an <a> tag
isAnchor := t.Data == "a"
if !isAnchor {
continue
}
// Extract the content link
ok, link := getLink(t)
if ok {
links = append(links, link)
}
}
}
}
// Helper function to pull the href attribute from a Token
func getLink(t html.Token) (ok bool, link entity.Link) {
ok = false
// Iterate over all of the Token's attributes until we find an "href"
for _, a := range t.Attr {
switch a.Key {
case "data-documize":
ok = true
case "data-link-id":
link.RefID = strings.TrimSpace(a.Val)
case "data-link-space-id":
link.FolderID = strings.TrimSpace(a.Val)
case "data-link-target-document-id":
link.TargetDocumentID = strings.TrimSpace(a.Val)
case "data-link-target-id":
link.TargetID = strings.TrimSpace(a.Val)
case "data-link-type":
link.LinkType = strings.TrimSpace(a.Val)
}
}
return
}

View file

@ -1,62 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import (
"crypto/rand"
"encoding/hex"
"golang.org/x/crypto/bcrypt"
"github.com/documize/community/core/log"
)
// GenerateRandomPassword provides a string suitable for use as a password.
func GenerateRandomPassword() string {
return GenerateRandom(5)
}
// GenerateSalt provides a string suitable for use as a salt value.
func GenerateSalt() string {
return GenerateRandom(20)
}
// GenerateRandom returns a string of the specified length using crypo/rand
func GenerateRandom(size int) string {
b := make([]byte, size)
_, err := rand.Read(b)
log.IfErr(err)
return hex.EncodeToString(b)
}
// GeneratePassword returns a hashed password.
func GeneratePassword(password string, salt string) string {
pwd := []byte(salt + password)
// Hashing the password with the cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(pwd, 10)
if err != nil {
log.Error("GeneratePassword failed", err)
}
return string(hashedPassword)
}
// MatchPassword copares a hashed password with a clear one.
func MatchPassword(hashedPassword string, password string, salt string) bool {
pwd := []byte(salt + password)
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), pwd)
return err == nil
}