1
0
mirror of https://github.com/Luzifer/wiki.git synced 2024-09-16 14:18:29 +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,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.

18
main.go
View File

@ -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

View File

@ -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")
}