|
| 1 | +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) |
| 2 | +// go-aah/view source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package view |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "aahframework.org/essentials.v0" |
| 14 | + "aahframework.org/log.v0" |
| 15 | +) |
| 16 | + |
| 17 | +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ |
| 18 | +// type AntiCSRFField and methods |
| 19 | +//________________________________________ |
| 20 | + |
| 21 | +// AntiCSRFField is used to insert Anti-CSRF HTML field dynamically |
| 22 | +// while parsing templates on view engine. |
| 23 | +type AntiCSRFField struct { |
| 24 | + engineName string |
| 25 | + field string |
| 26 | + inserter *strings.Replacer |
| 27 | + leftDelim string |
| 28 | + rightDelim string |
| 29 | +} |
| 30 | + |
| 31 | +// NewAntiCSRFField method creates new instance of Anti-CSRF HTML field |
| 32 | +// parser. |
| 33 | +func NewAntiCSRFField(engineName, leftDelim, rightDelim string) *AntiCSRFField { |
| 34 | + csft := &AntiCSRFField{engineName: engineName, leftDelim: leftDelim, rightDelim: rightDelim} |
| 35 | + |
| 36 | + csft.field = fmt.Sprintf(` <input type="hidden" name="anti_csrf_token" value="%s anitcsrftoken . %s"> |
| 37 | + </form>`, csft.leftDelim, csft.rightDelim) |
| 38 | + csft.inserter = strings.NewReplacer("</form>", csft.field) |
| 39 | + |
| 40 | + return csft |
| 41 | +} |
| 42 | + |
| 43 | +// InsertOnFile method inserts the Anti-CSRF HTML field for given HTML file and |
| 44 | +// writes a processed file into temp directory then return the new file path. |
| 45 | +func (ft *AntiCSRFField) InsertOnFiles(files ...string) []string { |
| 46 | + var ofiles []string |
| 47 | + |
| 48 | + for _, f := range files { |
| 49 | + fpath, err := ft.InsertOnFile(f) |
| 50 | + if err != nil { |
| 51 | + log.Errorf("anitcsrffield: unable to insert Anti-CSRF field for file: %s", f) |
| 52 | + ofiles = append(ofiles, f) |
| 53 | + continue |
| 54 | + } |
| 55 | + ofiles = append(ofiles, fpath) |
| 56 | + } |
| 57 | + |
| 58 | + return ofiles |
| 59 | +} |
| 60 | + |
| 61 | +// InsertOnFile method inserts the Anti-CSRF HTML filed for given HTML file and |
| 62 | +// writes a processed file into temp directory then return the new file path. |
| 63 | +func (ft *AntiCSRFField) InsertOnFile(file string) (string, error) { |
| 64 | + tmpDir, _ := ioutil.TempDir("", ft.engineName+"_anti_csrf") |
| 65 | + |
| 66 | + fileBytes, err := ioutil.ReadFile(file) |
| 67 | + if err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + |
| 71 | + fileStr := string(fileBytes) |
| 72 | + f := StripPathPrefixAt(file, "views") |
| 73 | + fpath := filepath.Join(tmpDir, f) |
| 74 | + if strings.Contains(fileStr, "</form>") { |
| 75 | + log.Tracef("Inserting Anti-CSRF field for file: %s", filepath.Join("views", f)) |
| 76 | + fileStr = ft.InsertOnString(fileStr) |
| 77 | + if err = ess.MkDirAll(filepath.Dir(fpath), 0755); err != nil { |
| 78 | + return "", err |
| 79 | + } |
| 80 | + |
| 81 | + if err = ioutil.WriteFile(fpath, []byte(fileStr), 0755); err != nil { |
| 82 | + return "", err |
| 83 | + } |
| 84 | + |
| 85 | + return fpath, nil |
| 86 | + } |
| 87 | + |
| 88 | + return file, nil |
| 89 | +} |
| 90 | + |
| 91 | +// InsertOnString method inserts the Anti-CSRF HTML field on |
| 92 | +// given HTML string and returns the processed HTML string. |
| 93 | +func (ft *AntiCSRFField) InsertOnString(str string) string { |
| 94 | + return ft.inserter.Replace(str) |
| 95 | +} |
0 commit comments