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:
parent
9d32df5cb5
commit
e3945c0760
3 changed files with 44 additions and 11 deletions
14
README.md
14
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.
|
||||
|
|
18
main.go
18
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
|
||||
|
|
23
storage.go
23
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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue