/
passwords.go
50 lines (42 loc) · 1.26 KB
/
passwords.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package api
import (
"encoding/json"
"net/http"
"github.com/pass-wall/passwall-server/internal/app"
"github.com/pass-wall/passwall-server/internal/storage"
"github.com/pass-wall/passwall-server/model"
"github.com/spf13/viper"
)
// FindSamePassword ...
func FindSamePassword(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var password model.Password
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&password); err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
schema := r.Context().Value("schema").(string)
urls, err := app.FindSamePassword(s, password, schema)
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, urls)
}
}
// Password generates new password
func GeneratePassword(w http.ResponseWriter, r *http.Request) {
generatedPass, err := app.GenerateSecureKey(viper.GetInt("server.generatedPasswordLength"))
if err != nil {
RespondWithError(w, http.StatusSeeOther, err.Error())
}
password := generatedPass
response := model.Response{
Code: http.StatusOK,
Status: Success,
Message: password,
}
RespondWithJSON(w, http.StatusOK, response)
}