X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=passwd.go;h=dae17a5364b687107f8590caac1231bae71198b3;hb=1e4532a9b2f2e3c512be1a63538f79d5ad6749c8;hp=29419564ce9c22b3fb4ceab0f0d0cb7e8658edee;hpb=58cc9588d92ab293d301e7267851bd9c4167e508;p=gocheese.git diff --git a/passwd.go b/passwd.go index 2941956..dae17a5 100644 --- a/passwd.go +++ b/passwd.go @@ -1,7 +1,7 @@ /* GoCheese -- Python private package repository and caching proxy -Copyright (C) 2019 Sergey Matveev - 2019 Elena Balakhonova +Copyright (C) 2019-2020 Sergey Matveev + 2019-2020 Elena Balakhonova This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,30 +19,65 @@ along with this program. If not, see . package main import ( + "errors" "io/ioutil" "log" + "os" "strings" ) +var passwords map[string]Auther = make(map[string]Auther) + +type Auther interface { + Auth(password string) bool +} + +func strToAuther(verifier string) (string, Auther, error) { + st := strings.SplitN(verifier, "$", 3) + if len(st) != 3 || st[0] != "" { + return "", nil, errors.New("invalid verifier structure") + } + algorithm := st[1] + var auther Auther + var err error + switch algorithm { + case "argon2i": + auther, err = parseArgon2i(st[2]) + case "sha256": + auther, err = parseSHA256(st[2]) + default: + err = errors.New("unknown hashing algorithm") + } + return algorithm, auther, err +} + func refreshPasswd() { passwd, err := ioutil.ReadFile(*passwdPath) + if os.IsNotExist(err) { + return + } if err != nil { log.Fatal(err) } + passwordsNew := make(map[string]Auther) for i, credentials := range strings.Split(strings.TrimRight(string(passwd), "\n"), "\n") { + if len(credentials) == 0 || strings.HasPrefix(credentials, "#") { + continue + } splitted := strings.Split(credentials, ":") if len(splitted) != 2 { log.Fatalf("%s:%d: Wrong login:password format", *passwdPath, i) } login := splitted[0] - if _, exists := passwords[login]; exists { + if _, exists := passwordsNew[login]; exists { log.Fatalf("%s:%d: %s: already exists", *passwdPath, i, login) } _, auther, err := strToAuther(splitted[1]) if err != nil { log.Fatalf("%s:%d: %s: %s", *passwdPath, i, login, err) } - passwords[login] = auther - log.Println("Added password for " + login) + passwordsNew[login] = auther + log.Println("added password for " + login) } + passwords = passwordsNew }