From e3945c076009ffdf259980c6c92c9781a98b4df8 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 4 Aug 2020 17:36:05 +0200 Subject: [PATCH] Add support for custom author configuration through auth proxy Signed-off-by: Knut Ahlers --- README.md | 14 ++++++++++---- main.go | 18 ++++++++++++++---- storage.go | 23 ++++++++++++++++++++--- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 84d66b0..044f1ce 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,12 @@ The software itself has no concept of users or authentication and is held as sim ```console # wiki --help Usage of wiki: - --data-dir string Directory to store data to (default "./data/") - --listen string Port/IP to listen on (default ":3000") - --log-level string Log level (debug, info, warn, error, fatal) (default "info") - --version Prints current version and exits + --author-email-header string Header to use as Author email + --author-name-header string Header to use as Author name + --data-dir string Directory to store data to (default "./data/") + --listen string Port/IP to listen on (default ":3000") + --log-level string Log level (debug, info, warn, error, fatal) (default "info") + --version Prints current version and exits ``` To use this you can @@ -29,3 +31,7 @@ To use this you can - or `go get -u github.com/Luzifer/wiki` the project Given you've used the binary you can now just execute `./wiki` and go to `http://localhost:3000`. Everything you save will be stored in the `./data` directory. + +## Setting the author name of the commit + +If you've put the wiki behind an auth-proxy which is able to set headers containing the username / email of the authenticated user (for example nginx with [nginx-sso](https://github.com/Luzifer/nginx-sso)) you can specify the `--author-email-header` and/or `--author-name-header` and provide the header names you've used there. These values will then be used as the author of the commit while the committer will still be the wiki-user. diff --git a/main.go b/main.go index 15eeafc..34eff97 100644 --- a/main.go +++ b/main.go @@ -23,10 +23,12 @@ import ( var ( cfg = struct { - DataDir string `flag:"data-dir" default:"./data/" description:"Directory to store data to"` - Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` - LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` - VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` + AuthorNameHeader string `flag:"author-name-header" default:"" description:"Header to use as Author name"` + AuthorEmailHeader string `flag:"author-email-header" default:"" description:"Header to use as Author email"` + DataDir string `flag:"data-dir" default:"./data/" description:"Directory to store data to"` + Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` + LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` + VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{} version = "dev" @@ -138,6 +140,14 @@ func handlePageWrite(w http.ResponseWriter, r *http.Request) { return } + if cfg.AuthorNameHeader != "" { + file.AuthorName = r.Header.Get(cfg.AuthorNameHeader) + } + + if cfg.AuthorEmailHeader != "" { + file.AuthorEmail = r.Header.Get(cfg.AuthorEmailHeader) + } + if err := file.Save(sanitizeFilename(vars["page"])); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/storage.go b/storage.go index 7811600..4346778 100644 --- a/storage.go +++ b/storage.go @@ -22,6 +22,9 @@ var errFileNotFound = errors.New("Specified file was not found") type storedFile struct { Meta map[string]interface{} `json:"meta"` Content string `json:"content"` + + AuthorName string `json:"-"` + AuthorEmail string `json:"-"` } func loadStoredFile(filename string) (*storedFile, error) { @@ -120,12 +123,26 @@ func (s storedFile) Save(filename string) error { return errors.Wrap(err, "Unable to add file to index") } - _, err = wt.Commit("Web-Update of "+filename, &git.CommitOptions{Author: s.authorSignature()}) + _, err = wt.Commit("Web-Update of "+filename, &git.CommitOptions{Author: s.authorSignature(), Committer: s.committerSignature()}) return errors.Wrap(err, "Unable to commit file change") } func (s storedFile) authorSignature() *object.Signature { - return &object.Signature{Name: "wiki " + version, Email: "wiki@luzifer.io", When: time.Now()} + sig := &object.Signature{Name: "Web-User", Email: "wiki+author@luzifer.io", When: time.Now()} + + if s.AuthorName != "" { + sig.Name = s.AuthorName + } + + if s.AuthorEmail != "" { + sig.Email = s.AuthorEmail + } + + return sig +} + +func (s storedFile) committerSignature() *object.Signature { + return &object.Signature{Name: "wiki " + version, Email: "wiki+committer@luzifer.io", When: time.Now()} } func (s storedFile) initRepo() (*git.Repository, error) { @@ -145,7 +162,7 @@ func (s storedFile) initRepo() (*git.Repository, error) { return nil, errors.Wrap(err, "Unable to get worktree") } - _, err = wt.Commit("Initial commit", &git.CommitOptions{Author: s.authorSignature()}) + _, err = wt.Commit("Initial commit", &git.CommitOptions{Author: s.authorSignature(), Committer: s.committerSignature()}) return repo, errors.Wrap(err, "Unable to create initial commit") }