Auto set anon access if there's an init public key

Toby Padilla created

Change summary

config/auth.go     |  2 +-
config/config.go   | 36 ++++++++++++++++++++----------------
config/defaults.go |  2 +-
main.go            | 13 +++----------
4 files changed, 25 insertions(+), 28 deletions(-)

Detailed changes

config/auth.go 🔗

@@ -11,7 +11,7 @@ func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
 }
 
 func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool {
-	return cfg.AnonReadOnly && cfg.AllowNoKeys
+	return (cfg.AnonAccess != "no-access") && cfg.AllowNoKeys
 }
 
 func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {

config/config.go 🔗

@@ -16,14 +16,14 @@ import (
 )
 
 type Config struct {
-	Name         string `yaml:"name"`
-	Host         string `yaml:"host"`
-	Port         int    `yaml:"port"`
-	AnonReadOnly bool   `yaml:"anon-access"`
-	AllowNoKeys  bool   `yaml:"allow-no-keys"`
-	Users        []User `yaml:"users"`
-	Repos        []Repo `yaml:"repos"`
-	Source       *git.RepoSource
+	Name        string `yaml:"name"`
+	Host        string `yaml:"host"`
+	Port        int    `yaml:"port"`
+	AnonAccess  string `yaml:"anon-access"`
+	AllowNoKeys bool   `yaml:"allow-no-keys"`
+	Users       []User `yaml:"users"`
+	Repos       []Repo `yaml:"repos"`
+	Source      *git.RepoSource
 }
 
 type User struct {
@@ -39,21 +39,25 @@ type Repo struct {
 	Note string `yaml:"note"`
 }
 
-func NewConfig(host string, port int, anon bool, pk string, rs *git.RepoSource) (*Config, error) {
+func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, error) {
+	var anonAccess string
+	var yamlUsers string
+	var displayHost string
 	cfg := &Config{}
 	cfg.Host = host
 	cfg.Port = port
-	cfg.AnonReadOnly = anon
 	cfg.Source = rs
-
-	var yamlUsers string
-	var h string
+	if pk == "" {
+		anonAccess = "read-write"
+	} else {
+		anonAccess = "no-access"
+	}
 	if host == "" {
-		h = "localhost"
+		displayHost = "localhost"
 	} else {
-		h = host
+		displayHost = host
 	}
-	yamlConfig := fmt.Sprintf(defaultConfig, h, port, anon)
+	yamlConfig := fmt.Sprintf(defaultConfig, displayHost, port, anonAccess)
 	if pk != "" {
 		yamlUsers = fmt.Sprintf(hasKeyUserConfig, pk)
 	} else {

config/defaults.go 🔗

@@ -8,7 +8,7 @@ host: %s
 port: %d
 
 # Set the access level for anonymous users. Options are: read-write, read-only and no-access
-anon-access: %v
+anon-access: %s
 
 # Allow read only even if they don't have private keys, any password will work
 allow-no-keys: false

main.go 🔗

@@ -33,16 +33,9 @@ func main() {
 		log.Fatalln(err)
 	}
 	rs := git.NewRepoSource(scfg.RepoPath)
-	if scfg.InitKey == "" {
-		cfg, err = config.NewConfig(scfg.Host, scfg.Port, true, "", rs)
-		if err != nil {
-			log.Fatalln(err)
-		}
-	} else {
-		cfg, err = config.NewConfig(scfg.Host, scfg.Port, false, scfg.InitKey, rs)
-		if err != nil {
-			log.Fatalln(err)
-		}
+	cfg, err = config.NewConfig(scfg.Host, scfg.Port, scfg.InitKey, rs)
+	if err != nil {
+		log.Fatalln(err)
 	}
 	s, err := wish.NewServer(
 		ssh.PublicKeyAuth(cfg.PublicKeyHandler),