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) {
writeErrorResponse(w, 400, BBJResponse{
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) {
if req.Method != "POST" {
badMethod(w)
return
}
type AuthArgs struct {
Username string `json:"user_name"`
AuthHash string `json:"auth_hash"`
}
var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w)
return
}
// TODO check if user exists
// TODO compare hash
db := opts.DB
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) {
@ -234,6 +272,11 @@ func setupAPI(opts Opts) {
return
}
type AuthArgs struct {
Username string `json:"target_user"`
AuthHash string `json:"target_hash"`
}
var args AuthArgs
if err := json.NewDecoder(req.Body).Decode(&args); err != nil {
invalidArgs(w)
@ -244,18 +287,10 @@ func setupAPI(opts Opts) {
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 = ?")
if err != nil {
serverErr(err)
opts.Logf("check_auth error: %s", err.Error())
serverErr(w)
return
}
defer stmt.Close()
@ -271,10 +306,7 @@ func setupAPI(opts Opts) {
})
} else {
opts.Logf("check_auth error: %s", err.Error())
writeErrorResponse(w, 500, BBJResponse{
Error: true,
Data: "server error",
})
serverErr(w)
}
return
}
@ -290,6 +322,7 @@ func setupAPI(opts Opts) {
return
}
// TODO include usermap?
writeResponse(w, BBJResponse{
Data: true,
})