]> Cypherpunks.ru repositories - gocheese.git/blob - passwd.go
Move strToAuther out for clarity
[gocheese.git] / passwd.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019 Sergey Matveev <stargrave@stargrave.org>
4               2019 Elena Balakhonova <balakhonova_e@riseup.net>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package main
20
21 import (
22         "errors"
23         "io/ioutil"
24         "log"
25         "strings"
26 )
27
28 func strToAuther(verifier string) (string, Auther, error) {
29         st := strings.SplitN(verifier, "$", 3)
30         if len(st) != 3 || st[0] != "" {
31                 return "", nil, errors.New("invalid verifier structure")
32         }
33         algorithm := st[1]
34         var auther Auther
35         var err error
36         switch algorithm {
37         case "argon2i":
38                 auther, err = parseArgon2i(st[2])
39         case "sha256":
40                 auther, err = parseSHA256(st[2])
41         default:
42                 err = errors.New("unknown hashing algorithm")
43         }
44         return algorithm, auther, err
45 }
46
47 func refreshPasswd() {
48         passwd, err := ioutil.ReadFile(*passwdPath)
49         if err != nil {
50                 log.Fatal(err)
51         }
52         passwordsNew := make(map[string]Auther)
53         for i, credentials := range strings.Split(strings.TrimRight(string(passwd), "\n"), "\n") {
54                 if len(credentials) == 0 || strings.HasPrefix(credentials, "#") {
55                         continue
56                 }
57                 splitted := strings.Split(credentials, ":")
58                 if len(splitted) != 2 {
59                         log.Fatalf("%s:%d: Wrong login:password format", *passwdPath, i)
60                 }
61                 login := splitted[0]
62                 if _, exists := passwordsNew[login]; exists {
63                         log.Fatalf("%s:%d: %s: already exists", *passwdPath, i, login)
64                 }
65                 _, auther, err := strToAuther(splitted[1])
66                 if err != nil {
67                         log.Fatalf("%s:%d: %s: %s", *passwdPath, i, login, err)
68                 }
69                 passwordsNew[login] = auther
70                 log.Println("Added password for " + login)
71         }
72         passwords = passwordsNew
73 }