1
0
Fork 0
mirror of https://github.com/Luzifer/wiki.git synced 2024-12-20 10:31:18 +00:00

Add support for custom author configuration through auth proxy

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-08-04 17:36:05 +02:00
parent 9d32df5cb5
commit e3945c0760
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 44 additions and 11 deletions

View file

@ -17,6 +17,8 @@ The software itself has no concept of users or authentication and is held as sim
```console ```console
# wiki --help # wiki --help
Usage of wiki: Usage of wiki:
--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/") --data-dir string Directory to store data to (default "./data/")
--listen string Port/IP to listen on (default ":3000") --listen string Port/IP to listen on (default ":3000")
--log-level string Log level (debug, info, warn, error, fatal) (default "info") --log-level string Log level (debug, info, warn, error, fatal) (default "info")
@ -29,3 +31,7 @@ To use this you can
- or `go get -u github.com/Luzifer/wiki` the project - 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. 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.

10
main.go
View file

@ -23,6 +23,8 @@ import (
var ( var (
cfg = struct { cfg = struct {
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"` 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"` 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)"` LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
@ -138,6 +140,14 @@ func handlePageWrite(w http.ResponseWriter, r *http.Request) {
return 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 { if err := file.Save(sanitizeFilename(vars["page"])); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return

View file

@ -22,6 +22,9 @@ var errFileNotFound = errors.New("Specified file was not found")
type storedFile struct { type storedFile struct {
Meta map[string]interface{} `json:"meta"` Meta map[string]interface{} `json:"meta"`
Content string `json:"content"` Content string `json:"content"`
AuthorName string `json:"-"`
AuthorEmail string `json:"-"`
} }
func loadStoredFile(filename string) (*storedFile, error) { 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") 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") return errors.Wrap(err, "Unable to commit file change")
} }
func (s storedFile) authorSignature() *object.Signature { 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) { 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") 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") return repo, errors.Wrap(err, "Unable to create initial commit")
} }