c09197a1ca95486db225634d82772ed1fad60806

Author
Toby Padilla <toby@charm.sh>
Committer
Toby Padilla <toby@charm.sh>
Date

Message

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

Diff

  1diff --git a/config/auth.go b/config/auth.go
  2index 999262c5a07821e8e08a0b3417814c007977ab41..78f6b2d6e27a5def15e9947b4d861c3f94e1c21b 100644
  3--- a/config/auth.go
  4+++ b/config/auth.go
  5@@ -11,7 +11,7 @@ func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
  6 }
  7 
  8 func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool {
  9-	return cfg.AnonReadOnly && cfg.AllowNoKeys
 10+	return (cfg.AnonAccess != "no-access") && cfg.AllowNoKeys
 11 }
 12 
 13 func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {
 14diff --git a/config/config.go b/config/config.go
 15index b64974d8be1017ab8c78129d1c27dd9fa0275dfe..46f7ba7ea87f014db60c0b07ecab34639e7b448c 100644
 16--- a/config/config.go
 17+++ b/config/config.go
 18@@ -16,14 +16,14 @@ import (
 19 )
 20 
 21 type Config struct {
 22-	Name         string `yaml:"name"`
 23-	Host         string `yaml:"host"`
 24-	Port         int    `yaml:"port"`
 25-	AnonReadOnly bool   `yaml:"anon-access"`
 26-	AllowNoKeys  bool   `yaml:"allow-no-keys"`
 27-	Users        []User `yaml:"users"`
 28-	Repos        []Repo `yaml:"repos"`
 29-	Source       *git.RepoSource
 30+	Name        string `yaml:"name"`
 31+	Host        string `yaml:"host"`
 32+	Port        int    `yaml:"port"`
 33+	AnonAccess  string `yaml:"anon-access"`
 34+	AllowNoKeys bool   `yaml:"allow-no-keys"`
 35+	Users       []User `yaml:"users"`
 36+	Repos       []Repo `yaml:"repos"`
 37+	Source      *git.RepoSource
 38 }
 39 
 40 type User struct {
 41@@ -39,21 +39,25 @@ type Repo struct {
 42 	Note string `yaml:"note"`
 43 }
 44 
 45-func NewConfig(host string, port int, anon bool, pk string, rs *git.RepoSource) (*Config, error) {
 46+func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, error) {
 47+	var anonAccess string
 48+	var yamlUsers string
 49+	var displayHost string
 50 	cfg := &Config{}
 51 	cfg.Host = host
 52 	cfg.Port = port
 53-	cfg.AnonReadOnly = anon
 54 	cfg.Source = rs
 55-
 56-	var yamlUsers string
 57-	var h string
 58+	if pk == "" {
 59+		anonAccess = "read-write"
 60+	} else {
 61+		anonAccess = "no-access"
 62+	}
 63 	if host == "" {
 64-		h = "localhost"
 65+		displayHost = "localhost"
 66 	} else {
 67-		h = host
 68+		displayHost = host
 69 	}
 70-	yamlConfig := fmt.Sprintf(defaultConfig, h, port, anon)
 71+	yamlConfig := fmt.Sprintf(defaultConfig, displayHost, port, anonAccess)
 72 	if pk != "" {
 73 		yamlUsers = fmt.Sprintf(hasKeyUserConfig, pk)
 74 	} else {
 75diff --git a/config/defaults.go b/config/defaults.go
 76index 1a7cbb3866df8db37deef9ac6e2bff02f0bc45e0..49b1235b6ec8289c28d1e623be783f6702b0476b 100644
 77--- a/config/defaults.go
 78+++ b/config/defaults.go
 79@@ -8,7 +8,7 @@ host: %s
 80 port: %d
 81 
 82 # Set the access level for anonymous users. Options are: read-write, read-only and no-access
 83-anon-access: %v
 84+anon-access: %s
 85 
 86 # Allow read only even if they don't have private keys, any password will work
 87 allow-no-keys: false
 88diff --git a/main.go b/main.go
 89index 92c1eaaccce8147781f2eca97b717ca0eac3cbe7..3992f3b28707080bf6b2a471f5319afbe83a7195 100644
 90--- a/main.go
 91+++ b/main.go
 92@@ -33,16 +33,9 @@ func main() {
 93 		log.Fatalln(err)
 94 	}
 95 	rs := git.NewRepoSource(scfg.RepoPath)
 96-	if scfg.InitKey == "" {
 97-		cfg, err = config.NewConfig(scfg.Host, scfg.Port, true, "", rs)
 98-		if err != nil {
 99-			log.Fatalln(err)
100-		}
101-	} else {
102-		cfg, err = config.NewConfig(scfg.Host, scfg.Port, false, scfg.InitKey, rs)
103-		if err != nil {
104-			log.Fatalln(err)
105-		}
106+	cfg, err = config.NewConfig(scfg.Host, scfg.Port, scfg.InitKey, rs)
107+	if err != nil {
108+		log.Fatalln(err)
109 	}
110 	s, err := wish.NewServer(
111 		ssh.PublicKeyAuth(cfg.PublicKeyHandler),