databag/net/server/internal/api_addAccount.go

130 lines
3.1 KiB
Go
Raw Normal View History

2022-01-17 05:11:24 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"crypto/sha256"
"databag/internal/store"
"encoding/hex"
"errors"
"gorm.io/gorm"
"net/http"
"os"
"strings"
2022-01-17 05:11:24 +00:00
)
func AddAccount(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
var token *store.AccountToken
var res error
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
if r.FormValue("token") != "" {
token, _, res = AccessToken(r)
if res != nil || token.TokenType != APPTokenCreate {
ErrResponse(w, http.StatusUnauthorized, res)
return
}
} else {
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
}
}
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
username, password, ret := BasicCredentials(r)
if ret != nil {
ErrResponse(w, http.StatusUnauthorized, ret)
return
}
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
if strings.Contains(username, " ") || strings.Contains(username, "\t") {
ErrResponse(w, http.StatusConflict, errors.New("username has whitespace"))
return
}
2022-07-21 06:26:52 +00:00
2022-07-22 19:28:14 +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-03-11 07:57:27 +00:00
2022-07-22 19:28:14 +00:00
// generate account key
privateKey, publicKey, keyType, err := GenerateRsaKeyPair()
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
privatePem := ExportRsaPrivateKeyAsPemStr(privateKey)
publicPem, err := ExportRsaPublicKeyAsPemStr(publicKey)
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
// compute key fingerprint
msg := []byte(publicPem)
hash := sha256.Sum256(msg)
fingerprint := hex.EncodeToString(hash[:])
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
// create path for account data
path := getStrConfigValue(CNFAssetPath, APPDefaultPath) + "/" + fingerprint
if err := os.Mkdir(path, os.ModePerm); err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
}
2022-02-28 22:59:29 +00:00
2022-07-22 19:28:14 +00:00
// create new account
account := store.Account{
Username: username,
Handle: strings.ToLower(username),
Password: password,
GUID: fingerprint,
}
detail := store.AccountDetail{
PublicKey: publicPem,
PrivateKey: privatePem,
KeyType: keyType,
}
2022-01-18 05:48:42 +00:00
2022-07-22 19:28:14 +00:00
// save account and delete token
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Create(&detail).Error; res != nil {
return res
}
account.AccountDetailID = detail.ID
if res := tx.Create(&account).Error; res != nil {
return res
}
if token != nil {
if res := tx.Delete(token).Error; res != nil {
return res
}
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
// create response
profile := Profile{
GUID: account.GUID,
Handle: account.Username,
Name: detail.Name,
Description: detail.Description,
Location: detail.Location,
Image: detail.Image,
Revision: account.ProfileRevision,
Version: APPVersion,
Node: getStrConfigValue(CNFDomain, ""),
}
2022-01-17 05:11:24 +00:00
2022-07-22 19:28:14 +00:00
// send response
WriteResponse(w, profile)
2022-01-17 05:11:24 +00:00
}