some golfing, start on registration

trunk
vilmibm 2022-04-24 12:33:19 -05:00
parent c2b26da9fc
commit 44343a429e
1 changed files with 42 additions and 21 deletions

View File

@ -193,33 +193,54 @@ func setupAPI(opts Opts) {
})
}))
badMethod := func(w http.ResponseWriter) {
writeErrorResponse(w, 400, BBJResponse{
Error: true,
Data: "bad method",
})
}
invalidArgs := func(w http.ResponseWriter) {
writeErrorResponse(w, 400, BBJResponse{
Error: true,
Data: "invalid args",
})
}
type AuthArgs struct {
Username string `json:"target_user"`
AuthHash string `json:"target_hash"`
}
http.HandleFunc("/user_register", handler(opts, func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
badMethod(w)
return
}
var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w)
return
}
// TODO check if user exists
// TODO compare hash
}))
http.HandleFunc("/check_auth", handler(opts, func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
writeErrorResponse(w, 400, BBJResponse{
Error: true,
Data: "bad method",
})
badMethod(w)
return
}
type args struct {
TargetUser string `json:"target_user"`
TargetHash string `json:"target_hash"`
}
var a args
err := json.NewDecoder(req.Body).Decode(&a)
if err != nil {
writeErrorResponse(w, 400, BBJResponse{
Error: true,
Data: "invalid arguments",
})
var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w)
return
}
opts.Logf("got %s %s", a.TargetUser, a.TargetHash)
opts.Logf("got %s %s", args.Username, args.AuthHash)
db := opts.DB
@ -240,7 +261,7 @@ func setupAPI(opts Opts) {
defer stmt.Close()
var authHash string
err = stmt.QueryRow(a.TargetUser).Scan(&authHash)
err = stmt.QueryRow(args.Username).Scan(&authHash)
if err != nil {
if strings.Contains(err.Error(), "no rows in result") {
opts.Logf("user not found")
@ -260,7 +281,7 @@ func setupAPI(opts Opts) {
// TODO unique constraint on user_name
if authHash != a.TargetHash {
if authHash != args.AuthHash {
http.Error(w, "incorrect password", 403)
writeErrorResponse(w, 403, BBJResponse{
Error: true,