WIP user reg

trunk
vilmibm 2022-04-28 13:40:48 -05:00
parent 44343a429e
commit 9703d88c66
1 changed files with 54 additions and 21 deletions

View File

@ -193,6 +193,13 @@ func setupAPI(opts Opts) {
}) })
})) }))
serverErr := func(w http.ResponseWriter) {
writeErrorResponse(w, 500, BBJResponse{
Error: true,
Data: "server error",
})
}
badMethod := func(w http.ResponseWriter) { badMethod := func(w http.ResponseWriter) {
writeErrorResponse(w, 400, BBJResponse{ writeErrorResponse(w, 400, BBJResponse{
Error: true, Error: true,
@ -207,25 +214,56 @@ func setupAPI(opts Opts) {
}) })
} }
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) { http.HandleFunc("/user_register", handler(opts, func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" { if req.Method != "POST" {
badMethod(w) badMethod(w)
return return
} }
type AuthArgs struct {
Username string `json:"user_name"`
AuthHash string `json:"auth_hash"`
}
var args AuthArgs var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil { if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w) invalidArgs(w)
return return
} }
// TODO check if user exists db := opts.DB
// TODO compare hash stmt, err := db.Prepare("select auth_hash from users where user_name = ?")
if err != nil {
opts.Logf("user_register error: %s", err.Error())
serverErr(w)
return
}
defer stmt.Close()
opts.Logf("querying for %s", args.Username)
var authHash string
err = stmt.QueryRow(args.Username).Scan(&authHash)
if err == nil {
opts.Logf("found %s", args.Username)
// code 4 apparently
writeErrorResponse(w, 403, BBJResponse{
Error: true,
Data: "user already exists",
})
return
} else if err != nil && !strings.Contains(err.Error(), "no rows in result") {
opts.Logf("user_register error: %s", err.Error())
serverErr(w)
return
}
// TODO register
writeResponse(w, BBJResponse{
Data: true, // TODO probably something else
// TODO prob usermap
})
})) }))
http.HandleFunc("/check_auth", handler(opts, func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/check_auth", handler(opts, func(w http.ResponseWriter, req *http.Request) {
@ -234,6 +272,11 @@ func setupAPI(opts Opts) {
return return
} }
type AuthArgs struct {
Username string `json:"target_user"`
AuthHash string `json:"target_hash"`
}
var args AuthArgs var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil { if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w) invalidArgs(w)
@ -244,18 +287,10 @@ func setupAPI(opts Opts) {
db := opts.DB db := opts.DB
serverErr := func(err error) {
opts.Logf("check_auth error: %s", err.Error())
writeErrorResponse(w, 500, BBJResponse{
Error: true,
Data: "server error",
})
return
}
stmt, err := db.Prepare("select auth_hash from users where user_name = ?") stmt, err := db.Prepare("select auth_hash from users where user_name = ?")
if err != nil { if err != nil {
serverErr(err) opts.Logf("check_auth error: %s", err.Error())
serverErr(w)
return return
} }
defer stmt.Close() defer stmt.Close()
@ -271,10 +306,7 @@ func setupAPI(opts Opts) {
}) })
} else { } else {
opts.Logf("check_auth error: %s", err.Error()) opts.Logf("check_auth error: %s", err.Error())
writeErrorResponse(w, 500, BBJResponse{ serverErr(w)
Error: true,
Data: "server error",
})
} }
return return
} }
@ -290,6 +322,7 @@ func setupAPI(opts Opts) {
return return
} }
// TODO include usermap?
writeResponse(w, BBJResponse{ writeResponse(w, BBJResponse{
Data: true, Data: true,
}) })