mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-07 19:03:17 +03:00
Split up whitelist func, made identity url get safer.
This commit is contained in:
parent
a1455a8eba
commit
912175e65a
39
server.go
39
server.go
@ -268,28 +268,35 @@ func (s *Server) Op(fingerprint string) {
|
|||||||
// Whitelist adds the given fingerprint to the whitelist
|
// Whitelist adds the given fingerprint to the whitelist
|
||||||
func (s *Server) Whitelist(fingerprint string) error {
|
func (s *Server) Whitelist(fingerprint string) error {
|
||||||
if strings.HasPrefix(fingerprint, "github.com/") {
|
if strings.HasPrefix(fingerprint, "github.com/") {
|
||||||
logger.Infof("Adding github account %s to whitelist", fingerprint)
|
return s.whitelistIdentityUrl(fingerprint)
|
||||||
|
} else {
|
||||||
|
return s.whitelistFingerprint(fingerprint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
keys, err := getGithubPubKeys(fingerprint)
|
func (s *Server) whitelistIdentityUrl(user string) error {
|
||||||
|
logger.Infof("Adding github account %s to whitelist", user)
|
||||||
|
|
||||||
|
user = strings.Replace(user, "github.com/", "", -1)
|
||||||
|
keys, err := getGithubPubKeys(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(keys) == 0 {
|
if len(keys) == 0 {
|
||||||
return fmt.Errorf("No github user %s", fingerprint)
|
return fmt.Errorf("No keys for github user %s", user)
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
fingerprint = Fingerprint(key)
|
fingerprint := Fingerprint(key)
|
||||||
|
s.whitelistFingerprint(fingerprint)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) whitelistFingerprint(fingerprint string) error {
|
||||||
logger.Infof("Adding whitelist: %s", fingerprint)
|
logger.Infof("Adding whitelist: %s", fingerprint)
|
||||||
s.Lock()
|
s.Lock()
|
||||||
s.whitelist[fingerprint] = struct{}{}
|
s.whitelist[fingerprint] = struct{}{}
|
||||||
s.Unlock()
|
s.Unlock()
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.Infof("Adding whitelist: %s", fingerprint)
|
|
||||||
s.Lock()
|
|
||||||
s.whitelist[fingerprint] = struct{}{}
|
|
||||||
s.Unlock()
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,8 +306,8 @@ var client = http.Client{
|
|||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
}
|
}
|
||||||
// Returns an array of public keys for the given github user URL
|
// Returns an array of public keys for the given github user URL
|
||||||
func getGithubPubKeys(url string) ([]ssh.PublicKey, error) {
|
func getGithubPubKeys(user string) ([]ssh.PublicKey, error) {
|
||||||
resp, err := client.Get("http://" + url + ".keys")
|
resp, err := client.Get("http://github.com/" + user + ".keys")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -312,6 +319,12 @@ func getGithubPubKeys(url string) ([]ssh.PublicKey, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bodyStr := string(body)
|
bodyStr := string(body)
|
||||||
|
|
||||||
|
// More informative error than that from base64 DecodeString
|
||||||
|
if bodyStr == "Not Found" {
|
||||||
|
return nil, fmt.Errorf("No github user %s found", user)
|
||||||
|
}
|
||||||
|
|
||||||
pubs := []ssh.PublicKey{}
|
pubs := []ssh.PublicKey{}
|
||||||
for _, key := range strings.SplitN(bodyStr, "\n", -1) {
|
for _, key := range strings.SplitN(bodyStr, "\n", -1) {
|
||||||
splitKey := strings.SplitN(key, " ", -1)
|
splitKey := strings.SplitN(key, " ", -1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user