-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 for v0.8 Release
- Loading branch information
Showing
31 changed files
with
583 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016-2017 Jeevanandam M., https://myjeeva.com <[email protected]> | ||
Copyright (c) 2016-2018 Jeevanandam M., https://myjeeva.com <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# view - aah framework | ||
[](https://travis-ci.org/go-aah/view) [](https://codecov.io/gh/go-aah/view/branch/master) [](https://goreportcard.com/report/aahframework.org/view.v0) [](https://github.com/go-aah/view/releases/latest) [](https://godoc.org/aahframework.org/view.v0) [](LICENSE) [](https://twitter.com/aahframework) | ||
[](https://travis-ci.org/go-aah/view) [](https://codecov.io/gh/go-aah/view/branch/master) [](https://goreportcard.com/report/aahframework.org/view.v0) [](https://github.com/go-aah/view/releases/latest) [](https://godoc.org/aahframework.org/view.v0) [](LICENSE) [](https://twitter.com/aahframework) | ||
|
||
***v0.7 [released](https://github.com/go-aah/view/releases/latest) and tagged on Oct 04, 2017*** | ||
***v0.8 [released](https://github.com/go-aah/view/releases/latest) and tagged on Mar 26, 2018*** | ||
|
||
Go HTML template library which supports partial template inheritance, imports, etc. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) | ||
// go-aah/view source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package view | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
|
||
"aahframework.org/essentials.v0" | ||
"aahframework.org/log.v0" | ||
) | ||
|
||
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | ||
// type AntiCSRFField and methods | ||
//________________________________________ | ||
|
||
// AntiCSRFField is used to insert Anti-CSRF HTML field dynamically | ||
// while parsing templates on view engine. | ||
type AntiCSRFField struct { | ||
engineName string | ||
field string | ||
inserter *strings.Replacer | ||
leftDelim string | ||
rightDelim string | ||
} | ||
|
||
// NewAntiCSRFField method creates new instance of Anti-CSRF HTML field | ||
// parser. | ||
func NewAntiCSRFField(engineName, leftDelim, rightDelim string) *AntiCSRFField { | ||
csft := &AntiCSRFField{engineName: engineName, leftDelim: leftDelim, rightDelim: rightDelim} | ||
|
||
csft.field = fmt.Sprintf(` <input type="hidden" name="anti_csrf_token" value="%s anitcsrftoken . %s"> | ||
</form>`, csft.leftDelim, csft.rightDelim) | ||
csft.inserter = strings.NewReplacer("</form>", csft.field) | ||
|
||
return csft | ||
} | ||
|
||
// InsertOnFile method inserts the Anti-CSRF HTML field for given HTML file and | ||
// writes a processed file into temp directory then return the new file path. | ||
func (ft *AntiCSRFField) InsertOnFiles(files ...string) []string { | ||
var ofiles []string | ||
|
||
for _, f := range files { | ||
fpath, err := ft.InsertOnFile(f) | ||
if err != nil { | ||
log.Errorf("anitcsrffield: unable to insert Anti-CSRF field for file: %s", f) | ||
ofiles = append(ofiles, f) | ||
continue | ||
} | ||
ofiles = append(ofiles, fpath) | ||
} | ||
|
||
return ofiles | ||
} | ||
|
||
// InsertOnFile method inserts the Anti-CSRF HTML filed for given HTML file and | ||
// writes a processed file into temp directory then return the new file path. | ||
func (ft *AntiCSRFField) InsertOnFile(file string) (string, error) { | ||
tmpDir, _ := ioutil.TempDir("", ft.engineName+"_anti_csrf") | ||
|
||
fileBytes, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
fileStr := string(fileBytes) | ||
f := StripPathPrefixAt(file, "views") | ||
fpath := filepath.Join(tmpDir, f) | ||
if strings.Contains(fileStr, "</form>") { | ||
log.Tracef("Inserting Anti-CSRF field for file: %s", filepath.Join("views", f)) | ||
fileStr = ft.InsertOnString(fileStr) | ||
if err = ess.MkDirAll(filepath.Dir(fpath), 0755); err != nil { | ||
return "", err | ||
} | ||
|
||
if err = ioutil.WriteFile(fpath, []byte(fileStr), 0755); err != nil { | ||
return "", err | ||
} | ||
|
||
return fpath, nil | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
// InsertOnString method inserts the Anti-CSRF HTML field on | ||
// given HTML string and returns the processed HTML string. | ||
func (ft *AntiCSRFField) InsertOnString(str string) string { | ||
return ft.inserter.Replace(str) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) | ||
// go-aah/view source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package view | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"aahframework.org/test.v0/assert" | ||
) | ||
|
||
func TestAntiCSRFFieldNoFormTag(t *testing.T) { | ||
acsrf := NewAntiCSRFField("go", "{{", "}}") | ||
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "testhtml-noform.html") | ||
|
||
files := acsrf.InsertOnFiles(fpath) | ||
bytes, err := ioutil.ReadFile(files[0]) | ||
assert.Nil(t, err) | ||
assert.False(t, strings.Contains(string(bytes), "{{ anti_csrf_token . }}")) | ||
} | ||
|
||
func TestAntiCSRFFieldFormTag(t *testing.T) { | ||
acsrf := NewAntiCSRFField("go", "%%", "%%") | ||
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "testhtml-form.html") | ||
|
||
files := acsrf.InsertOnFiles(fpath) | ||
bytes, err := ioutil.ReadFile(files[0]) | ||
assert.Nil(t, err) | ||
assert.True(t, strings.Contains(string(bytes), "%% anitcsrftoken . %%")) | ||
} | ||
|
||
func TestAntiCSRFFieldFormTagDelim(t *testing.T) { | ||
acsrf := NewAntiCSRFField("go", "[[", "]]") | ||
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "not-exists.html") | ||
|
||
files := acsrf.InsertOnFiles(fpath) | ||
assert.NotNil(t, files) | ||
assert.Equal(t, fpath, files[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.