pomme/internal/api/api.go

74 lines
1.8 KiB
Go

package api
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"git.freecumextremist.com/grumbulon/pomme/internal"
"git.freecumextremist.com/grumbulon/pomme/internal/db"
"github.com/go-chi/render"
"golang.org/x/crypto/bcrypt"
)
func Ingest(w http.ResponseWriter, r *http.Request) {
data := &internal.ZoneRequest{}
log.Println(data)
if err := render.Bind(r, data); err != nil {
http.Error(w, "Unable to parse Zonefile", http.StatusBadRequest)
return
}
zonefile := data.Zone
render.Status(r, http.StatusAccepted)
render.Render(w, r, internal.NewZoneResponse(zonefile))
// todo write to database, maybe?
// todo -- add functions to apply to master zonefile if above check is OK
}
func NewUser(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.Form.Get("username")
if username == "" {
username = autoUname()
}
password := r.Form.Get("password")
if password == "" {
password = "nigga" //testing purposes
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
db := db.InitDb()
db.Create(&internal.User{Username: username, HashedPassword: string(hashedPassword)})
w.Write([]byte(username))
w.Write([]byte("██████████"))
w.Write(hashedPassword)
}
func Login(username, password string) (bool, error) {
username = "user22457"
password = "nigga" //testing purposes
hashedpassword := "$2a$10$uISHvOh/1Thfri1sJQNVmeWHIbIo/V.OmcpQV7UyIoyOwKSnhODtC"
err := bcrypt.CompareHashAndPassword([]byte(hashedpassword), []byte(password))
if err != nil {
return false, err
}
return true, err
}
func autoUname() string {
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("user%d", rand.Intn(99999-00000))
}
func AuthTest(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("██████████"))
}