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
|
```console
|
||||||
# wiki --help
|
# wiki --help
|
||||||
Usage of wiki:
|
Usage of wiki:
|
||||||
--data-dir string Directory to store data to (default "./data/")
|
--author-email-header string Header to use as Author email
|
||||||
--listen string Port/IP to listen on (default ":3000")
|
--author-name-header string Header to use as Author name
|
||||||
--log-level string Log level (debug, info, warn, error, fatal) (default "info")
|
--data-dir string Directory to store data to (default "./data/")
|
||||||
--version Prints current version and exits
|
--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
|
To use this you can
|
||||||
|
@ -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.
|
||||||
|
|
18
main.go
18
main.go
|
@ -23,10 +23,12 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg = struct {
|
cfg = struct {
|
||||||
DataDir string `flag:"data-dir" default:"./data/" description:"Directory to store data to"`
|
AuthorNameHeader string `flag:"author-name-header" default:"" description:"Header to use as Author name"`
|
||||||
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
AuthorEmailHeader string `flag:"author-email-header" default:"" description:"Header to use as Author email"`
|
||||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
DataDir string `flag:"data-dir" default:"./data/" description:"Directory to store data to"`
|
||||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
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"
|
version = "dev"
|
||||||
|
@ -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
|
||||||
|
|
23
storage.go
23
storage.go
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue