databag/net/server/internal/api_addAccount.go

125 lines
3.0 KiB
Go
Raw Normal View History

2022-01-17 05:11:24 +00:00
package databag
import (
2022-02-28 22:59:29 +00:00
"os"
2022-03-11 07:57:27 +00:00
"errors"
2022-01-17 05:11:24 +00:00
"net/http"
"crypto/sha256"
"encoding/hex"
2022-01-18 05:48:42 +00:00
"gorm.io/gorm"
2022-01-17 05:11:24 +00:00
"databag/internal/store"
)
func AddAccount(w http.ResponseWriter, r *http.Request) {
2022-03-11 07:57:27 +00:00
var token *store.AccountToken
2022-01-17 05:11:24 +00:00
2022-03-11 07:57:27 +00:00
if r.Header.Get("Authorization") == "" {
if available, err := getAvailableAccounts(); err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
} else if available == 0 {
ErrResponse(w, http.StatusForbidden, errors.New("no open accounts available"))
return
}
} else {
var err error
token, err = BearerAccountToken(r);
if err != nil || token.TokenType != APP_TOKENCREATE {
ErrResponse(w, http.StatusUnauthorized, err)
return
}
2022-01-17 05:11:24 +00:00
}
2022-01-19 23:03:06 +00:00
username, password, ret := BasicCredentials(r);
if ret != nil {
ErrResponse(w, http.StatusUnauthorized, ret)
2022-01-17 05:11:24 +00:00
return
}
2022-03-11 07:57:27 +00:00
// check if username is taken
var count int64
if err := store.DB.Model(&store.Account{}).Where("username = ?", username).Count(&count).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err);
return
}
if count != 0 {
ErrResponse(w, http.StatusConflict, errors.New("username already taken"))
return
}
2022-01-17 05:11:24 +00:00
// generate account key
2022-01-19 23:03:06 +00:00
privateKey, publicKey, keyType, err := GenerateRsaKeyPair()
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-17 05:11:24 +00:00
privatePem := ExportRsaPrivateKeyAsPemStr(privateKey)
publicPem, err := ExportRsaPublicKeyAsPemStr(publicKey)
if err != nil {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusInternalServerError, err)
2022-01-17 05:11:24 +00:00
return
}
// compute key fingerprint
msg := []byte(publicPem)
hash := sha256.Sum256(msg)
fingerprint := hex.EncodeToString(hash[:])
2022-01-17 05:11:24 +00:00
2022-02-28 22:59:29 +00:00
// create path for account data
2022-03-11 07:57:27 +00:00
path := getStrConfigValue(CONFIG_ASSETPATH, APP_DEFAULTPATH) + "/" + fingerprint
2022-02-28 22:59:29 +00:00
if err := os.Mkdir(path, os.ModePerm); err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
}
2022-01-17 05:11:24 +00:00
// create new account
account := store.Account{
Username: username,
Password: password,
Guid: fingerprint,
}
detail := store.AccountDetail{
PublicKey: publicPem,
PrivateKey: privatePem,
2022-01-19 23:03:06 +00:00
KeyType: keyType,
}
2022-01-18 05:48:42 +00:00
// save account and delete token
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-28 22:59:29 +00:00
if res := tx.Create(&detail).Error; res != nil {
return res;
}
account.AccountDetailID = detail.ID
2022-02-28 22:59:29 +00:00
if res := tx.Create(&account).Error; res != nil {
2022-01-18 05:48:42 +00:00
return res;
}
2022-03-11 07:57:27 +00:00
if token != nil {
if res := tx.Delete(token).Error; res != nil {
return res;
}
2022-01-18 05:48:42 +00:00
}
return nil;
});
if err != nil {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusInternalServerError, err)
2022-01-17 05:11:24 +00:00
return
}
// create response
profile := Profile{
Guid: account.Guid,
Handle: account.Username,
Name: detail.Name,
Description: detail.Description,
Location: detail.Location,
Image: detail.Image,
2022-01-17 05:11:24 +00:00
Revision: account.ProfileRevision,
2022-01-17 05:55:25 +00:00
Version: APP_VERSION,
2022-03-17 21:42:51 +00:00
Node: getStrConfigValue(CONFIG_DOMAIN, ""),
2022-01-17 05:11:24 +00:00
}
// send response
WriteResponse(w, profile)
}