mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package databag
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"gorm.io/gorm"
|
|
"databag/internal/store"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func SetNodeClaim(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// confirm node hasn't been configured
|
|
if getBoolConfigValue(CONFIG_CONFIGURED, false) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// extract credentials
|
|
username, password, ok := r.BasicAuth();
|
|
if !ok || username == "" || password == "" {
|
|
log.Printf("SetNodeClaim - invalid credenitals");
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
log.Printf("SetNodeClaim - failed to hash password");
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// store credentials
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_USERNAME, StrValue: username}).Error; res != nil {
|
|
return res
|
|
}
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_PASSWORD, BinValue: hashedPassword}).Error; res != nil {
|
|
return res
|
|
}
|
|
if res := tx.Create(&store.Config{ConfigId: CONFIG_CONFIGURED, BoolValue: true}).Error; res != nil {
|
|
return res
|
|
}
|
|
return nil;
|
|
})
|
|
if(err != nil) {
|
|
log.Printf("SetNodeCalim - failed to store credentials");
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|