Skip to content

Commit

Permalink
User may have more than one public key
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 12, 2021
1 parent 2a62d6a commit 2148bae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
10 changes: 8 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"strings"

"gopkg.in/yaml.v2"

"fmt"
Expand Down Expand Up @@ -28,7 +30,7 @@ type Config struct {
type User struct {
Name string `yaml:"name"`
Admin bool `yaml:"admin"`
PublicKey string `yaml:"public-key"`
PublicKeys []string `yaml:"public-keys"`
CollabRepos []string `yaml:"collab-repos"`
}

Expand Down Expand Up @@ -64,7 +66,11 @@ func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, e
}
yamlConfig := fmt.Sprintf(defaultConfig, displayHost, port, anonAccess)
if pk != "" {
yamlUsers = fmt.Sprintf(hasKeyUserConfig, pk)
pks := ""
for _, key := range strings.Split(strings.TrimSpace(pk), "\n") {
pks += fmt.Sprintf(" - %s\n", key)
}
yamlUsers = fmt.Sprintf(hasKeyUserConfig, pks)
} else {
yamlUsers = defaultUserConfig
}
Expand Down
12 changes: 6 additions & 6 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ const hasKeyUserConfig = `
users:
- name: admin
admin: true
public-key:
%s`
public-keys:
%s`

const defaultUserConfig = `
# users:
# - name: admin
# admin: true
# public-key:
# KEY TEXT`
# public-keys:
# - KEY TEXT`

const exampleUserConfig = `
# - name: Example User
# collab-repos:
# - REPO
# public-key:
# KEY TEXT`
# public-keys:
# - KEY TEXT`
31 changes: 17 additions & 14 deletions internal/config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"log"
"strings"

gm "github.com/charmbracelet/wish/git"
"github.com/gliderlabs/ssh"
Expand Down Expand Up @@ -43,22 +44,24 @@ func (cfg *Config) accessForKey(repo string, pk ssh.PublicKey) gm.AccessLevel {
private = true
}
for _, u := range cfg.Users {
apk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(u.PublicKey))
if err != nil {
log.Printf("error: malformed authorized key: '%s'", u.PublicKey)
return gm.NoAccess
}
if ssh.KeysEqual(pk, apk) {
if u.Admin {
return gm.AdminAccess
for _, k := range u.PublicKeys {
apk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(strings.TrimSpace(k)))
if err != nil {
log.Printf("error: malformed authorized key: '%s'", k)
return gm.NoAccess
}
for _, r := range u.CollabRepos {
if repo == r {
return gm.ReadWriteAccess
if ssh.KeysEqual(pk, apk) {
if u.Admin {
return gm.AdminAccess
}
for _, r := range u.CollabRepos {
if repo == r {
return gm.ReadWriteAccess
}
}
if !private {
return gm.ReadOnlyAccess
}
}
if !private {
return gm.ReadOnlyAccess
}
}
}
Expand Down

0 comments on commit 2148bae

Please sign in to comment.