-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 serveUpload(w http.ResponseWriter, r *http.Request) {
- username, password, ok := r.BasicAuth()
- if !ok {
- log.Println(r.RemoteAddr, "unauthenticated", username)
- http.Error(w, "unauthenticated", http.StatusUnauthorized)
- return
- }
- auther, ok := passwords[username]
- if !ok || !auther.Auth(password) {
- log.Println(r.RemoteAddr, "unauthenticated", username)
- http.Error(w, "unauthenticated", http.StatusUnauthorized)
- return
- }
- var err error
- if err = r.ParseMultipartForm(1 << 20); err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- for _, file := range r.MultipartForm.File["content"] {
- filename := file.Filename
- log.Println(r.RemoteAddr, "upload", filename, "by", username)
- dir := filename[:strings.LastIndex(filename, "-")]
- dirPath := filepath.Join(*root, dir)
- path := filepath.Join(dirPath, filename)
- if _, err = os.Stat(path); err == nil {
- log.Println(r.RemoteAddr, "already exists", filename)
- http.Error(w, "Already exists", http.StatusBadRequest)
- return
- }
- if !mkdirForPkg(w, r, dir) {
- return
- }
- internalPath := filepath.Join(dirPath, InternalFlag)
- var dst *os.File
- if _, err = os.Stat(internalPath); os.IsNotExist(err) {
- if dst, err = os.Create(internalPath); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- dst.Close()
- }
- src, err := file.Open()
- defer src.Close()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- dst, err = ioutil.TempFile(dirPath, "")
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- hasher := sha256.New()
- wr := io.MultiWriter(hasher, dst)
- if _, err = io.Copy(wr, src); err != nil {
- os.Remove(dst.Name())
- dst.Close()
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- if err = dst.Sync(); err != nil {
- os.Remove(dst.Name())
- dst.Close()
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- dst.Close()
- if err = os.Rename(dst.Name(), path); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- if err = ioutil.WriteFile(
- path+SHA256Ext,
- hasher.Sum(nil),
- os.FileMode(0600),
- ); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- }
-}
-