2024-01-15 04:54:43 +00:00
|
|
|
package s5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-16 18:32:47 +00:00
|
|
|
"crypto/ed25519"
|
2024-01-16 16:31:33 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2024-01-16 07:01:18 +00:00
|
|
|
"encoding/hex"
|
2024-01-15 04:54:43 +00:00
|
|
|
"errors"
|
2024-01-16 21:16:37 +00:00
|
|
|
"fmt"
|
2024-01-15 04:54:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-17 19:46:37 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/metadata"
|
2024-01-15 19:41:24 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-16 07:01:18 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-01-15 04:54:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
2024-01-16 18:32:47 +00:00
|
|
|
emailverifier "github.com/AfterShip/email-verifier"
|
2024-01-17 17:33:05 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-15 04:54:43 +00:00
|
|
|
"go.sia.tech/jape"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"io"
|
2024-01-17 17:38:52 +00:00
|
|
|
"math"
|
2024-01-15 04:54:43 +00:00
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2024-01-15 13:29:09 +00:00
|
|
|
"strings"
|
2024-01-16 18:32:47 +00:00
|
|
|
"time"
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-16 16:31:33 +00:00
|
|
|
errMultiformParse = "Error parsing multipart form"
|
|
|
|
errRetrievingFile = "Error retrieving the file"
|
|
|
|
errReadFile = "Error reading the file"
|
|
|
|
errClosingStream = "Error closing the stream"
|
|
|
|
errUploadingFile = "Error uploading the file"
|
|
|
|
errAccountGenerateChallenge = "Error generating challenge"
|
2024-01-16 18:32:47 +00:00
|
|
|
errAccountRegister = "Error registering account"
|
2024-01-16 18:51:03 +00:00
|
|
|
errAccountLogin = "Error logging in account"
|
2024-01-17 17:33:05 +00:00
|
|
|
errFailedToGetPins = "Failed to get pins"
|
2024-01-17 18:04:32 +00:00
|
|
|
errFailedToDelPin = "Failed to delete pin"
|
2024-01-17 18:13:37 +00:00
|
|
|
errFailedToAddPin = "Failed to add pin"
|
2024-01-17 19:46:37 +00:00
|
|
|
errorNotMultiform = "Not a multipart form"
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-16 16:31:33 +00:00
|
|
|
errUploadingFileErr = errors.New(errUploadingFile)
|
|
|
|
errAccountGenerateChallengeErr = errors.New(errAccountGenerateChallenge)
|
2024-01-16 18:32:47 +00:00
|
|
|
errAccountRegisterErr = errors.New(errAccountRegister)
|
|
|
|
errInvalidChallengeErr = errors.New("Invalid challenge")
|
|
|
|
errInvalidSignatureErr = errors.New("Invalid signature")
|
|
|
|
errPubkeyNotSupported = errors.New("Only ed25519 keys are supported")
|
|
|
|
errInvalidEmail = errors.New("Invalid email")
|
|
|
|
errEmailAlreadyExists = errors.New("Email already exists")
|
|
|
|
errGeneratingPassword = errors.New("Error generating password")
|
|
|
|
errPubkeyAlreadyExists = errors.New("Pubkey already exists")
|
2024-01-16 21:05:28 +00:00
|
|
|
errPubkeyNotExist = errors.New("Pubkey does not exist")
|
2024-01-16 18:51:03 +00:00
|
|
|
errAccountLoginErr = errors.New(errAccountLogin)
|
2024-01-17 17:33:05 +00:00
|
|
|
errFailedToGetPinsErr = errors.New(errFailedToGetPins)
|
2024-01-17 18:04:32 +00:00
|
|
|
errFailedToDelPinErr = errors.New(errFailedToDelPin)
|
2024-01-17 18:13:37 +00:00
|
|
|
errFailedToAddPinErr = errors.New(errFailedToAddPin)
|
2024-01-17 19:46:37 +00:00
|
|
|
errNotMultiformErr = errors.New(errorNotMultiform)
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
2024-01-16 16:42:50 +00:00
|
|
|
type HttpHandler struct {
|
2024-01-16 18:32:47 +00:00
|
|
|
portal interfaces.Portal
|
|
|
|
verifier *emailverifier.Verifier
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 16:42:50 +00:00
|
|
|
func NewHttpHandler(portal interfaces.Portal) *HttpHandler {
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
verifier := emailverifier.NewVerifier()
|
|
|
|
|
|
|
|
verifier.DisableSMTPCheck()
|
|
|
|
verifier.DisableGravatarCheck()
|
|
|
|
verifier.DisableDomainSuggest()
|
|
|
|
verifier.DisableAutoUpdateDisposable()
|
|
|
|
|
|
|
|
return &HttpHandler{
|
|
|
|
portal: portal,
|
|
|
|
verifier: verifier,
|
|
|
|
}
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 16:42:50 +00:00
|
|
|
func (h *HttpHandler) SmallFileUpload(jc jape.Context) {
|
2024-01-15 20:16:48 +00:00
|
|
|
var rs io.ReadSeeker
|
|
|
|
var bufferSize int64
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
r := jc.Request
|
2024-01-15 13:29:09 +00:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
2024-01-15 04:54:43 +00:00
|
|
|
// Parse the multipart form
|
|
|
|
err := r.ParseMultipartForm(h.portal.Config().GetInt64("core.post-upload-limit"))
|
|
|
|
|
|
|
|
if jc.Check(errMultiformParse, err) != nil {
|
|
|
|
h.portal.Logger().Error(errMultiformParse, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the file from the form data
|
|
|
|
file, _, err := r.FormFile("file")
|
|
|
|
if jc.Check(errRetrievingFile, err) != nil {
|
|
|
|
h.portal.Logger().Error(errRetrievingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func(file multipart.File) {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
|
|
|
h.portal.Logger().Error(errClosingStream, zap.Error(err))
|
|
|
|
}
|
|
|
|
}(file)
|
|
|
|
|
2024-01-15 20:16:48 +00:00
|
|
|
rs = file
|
2024-01-15 04:54:43 +00:00
|
|
|
} else {
|
2024-01-15 20:16:48 +00:00
|
|
|
data, err := io.ReadAll(r.Body)
|
2024-01-15 04:54:43 +00:00
|
|
|
if jc.Check(errReadFile, err) != nil {
|
|
|
|
h.portal.Logger().Error(errReadFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:16:48 +00:00
|
|
|
buffer := bytes.NewReader(data)
|
|
|
|
bufferSize = int64(buffer.Len())
|
|
|
|
rs = buffer
|
|
|
|
|
2024-01-15 04:54:43 +00:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
h.portal.Logger().Error(errClosingStream, zap.Error(err))
|
|
|
|
}
|
|
|
|
}(r.Body)
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:05:09 +00:00
|
|
|
hash, err := h.portal.Storage().GetHash(rs)
|
2024-01-17 16:04:24 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2024-01-16 06:05:09 +00:00
|
|
|
|
|
|
|
if exists, upload := h.portal.Storage().FileExists(hash); exists {
|
2024-01-17 16:31:48 +00:00
|
|
|
cid, err := encoding.CIDFromHash(hash, upload.Size, types.CIDTypeRaw, types.HashTypeBlake3)
|
2024-01-16 06:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jc.Encode(map[string]string{"hash": cidStr})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = h.portal.Storage().PutFile(rs, "s5", false)
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 16:31:48 +00:00
|
|
|
cid, err := encoding.CIDFromHash(hash, uint64(bufferSize), types.CIDTypeRaw, types.HashTypeBlake3)
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 07:01:18 +00:00
|
|
|
tx := h.portal.Database().Create(&models.Upload{
|
|
|
|
Hash: hex.EncodeToString(hash),
|
|
|
|
Size: uint64(bufferSize),
|
|
|
|
Protocol: "s5",
|
2024-01-17 14:05:39 +00:00
|
|
|
UserID: uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)),
|
2024-01-16 07:01:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if tx.Error != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
jc.Encode(&SmallUploadResponse{
|
|
|
|
CID: cidStr,
|
|
|
|
})
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
2024-01-16 16:31:33 +00:00
|
|
|
|
2024-01-16 16:42:50 +00:00
|
|
|
func (h *HttpHandler) AccountRegisterChallenge(jc jape.Context) {
|
2024-01-16 16:31:33 +00:00
|
|
|
var pubkey string
|
|
|
|
if jc.DecodeForm("pubKey", &pubkey) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err := rand.Read(challenge)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:22:48 +00:00
|
|
|
if len(decodedKey) != 33 && int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
2024-01-16 16:31:33 +00:00
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result := h.portal.Database().Create(&models.S5Challenge{
|
2024-01-16 19:22:16 +00:00
|
|
|
Pubkey: pubkey,
|
2024-01-16 20:12:05 +00:00
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
2024-01-16 18:51:03 +00:00
|
|
|
Type: "register",
|
2024-01-16 16:31:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
jc.Encode(&AccountRegisterChallengeResponse{
|
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
|
|
|
})
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
func (h *HttpHandler) AccountRegister(jc jape.Context) {
|
|
|
|
var request AccountRegisterRequest
|
|
|
|
|
|
|
|
if jc.Decode(&request) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountRegisterErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountRegister, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(request.Pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:26:36 +00:00
|
|
|
if len(decodedKey) != 33 {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var challenge models.S5Challenge
|
|
|
|
|
2024-01-16 18:51:03 +00:00
|
|
|
result := h.portal.Database().Model(&models.S5Challenge{}).Where(&models.S5Challenge{Pubkey: request.Pubkey, Type: "register"}).First(&challenge)
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedResponse, err := base64.RawURLEncoding.DecodeString(request.Response)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:32:53 +00:00
|
|
|
if len(decodedResponse) != 65 {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedChallenge, err := base64.RawURLEncoding.DecodeString(challenge.Challenge)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:45:07 +00:00
|
|
|
if !bytes.Equal(decodedResponse[1:33], decodedChallenge) {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSignature, err := base64.RawURLEncoding.DecodeString(request.Signature)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:37:08 +00:00
|
|
|
if !ed25519.Verify(decodedKey[1:], decodedResponse, decodedSignature) {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(errInvalidSignatureErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:16:37 +00:00
|
|
|
if request.Email == "" {
|
|
|
|
request.Email = fmt.Sprintf("%s@%s", hex.EncodeToString(decodedKey[1:]), "example.com")
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
verify, _ := h.verifier.Verify(request.Email)
|
|
|
|
|
|
|
|
if !verify.Syntax.Valid {
|
|
|
|
errored(errInvalidEmail)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accountExists, _ := h.portal.Accounts().EmailExists(request.Email)
|
|
|
|
|
|
|
|
if accountExists {
|
|
|
|
errored(errEmailAlreadyExists)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:47:58 +00:00
|
|
|
pubkeyExists, _ := h.portal.Accounts().PubkeyExists(hex.EncodeToString(decodedKey[1:]))
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
if pubkeyExists {
|
|
|
|
errored(errPubkeyAlreadyExists)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err = rand.Read(passwd)
|
|
|
|
|
|
|
|
if accountExists {
|
|
|
|
errored(errGeneratingPassword)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newAccount, err := h.portal.Accounts().CreateAccount(request.Email, string(passwd))
|
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:30:00 +00:00
|
|
|
rawPubkey := hex.EncodeToString(decodedKey[1:])
|
|
|
|
|
|
|
|
err = h.portal.Accounts().AddPubkeyToAccount(*newAccount, rawPubkey)
|
2024-01-16 18:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:30:00 +00:00
|
|
|
jwt, err := h.portal.Accounts().LoginPubkey(rawPubkey)
|
2024-01-16 18:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:48:45 +00:00
|
|
|
result = h.portal.Database().Delete(&challenge)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:38:10 +00:00
|
|
|
setAuthCookie(jwt, jc)
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
func (h *HttpHandler) AccountLoginChallenge(jc jape.Context) {
|
2024-01-16 18:51:03 +00:00
|
|
|
var pubkey string
|
|
|
|
if jc.DecodeForm("pubKey", &pubkey) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountLoginErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountLogin, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err := rand.Read(challenge)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:23:00 +00:00
|
|
|
errored(errAccountGenerateChallengeErr)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:22:48 +00:00
|
|
|
if len(decodedKey) != 33 && int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:05:28 +00:00
|
|
|
pubkeyExists, _ := h.portal.Accounts().PubkeyExists(hex.EncodeToString(decodedKey[1:]))
|
|
|
|
|
|
|
|
if pubkeyExists {
|
|
|
|
errored(errPubkeyNotExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:51:03 +00:00
|
|
|
result := h.portal.Database().Create(&models.S5Challenge{
|
2024-01-16 20:12:05 +00:00
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
2024-01-16 18:51:03 +00:00
|
|
|
Type: "login",
|
|
|
|
})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-01-16 20:23:00 +00:00
|
|
|
errored(result.Error)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&AccountLoginChallengeResponse{
|
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
|
|
|
})
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
func (h *HttpHandler) AccountLogin(jc jape.Context) {
|
2024-01-16 18:56:25 +00:00
|
|
|
var request AccountLoginRequest
|
|
|
|
|
|
|
|
if jc.Decode(&request) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountLoginErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errAccountLogin, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(request.Pubkey)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(decodedKey) != 32 {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var challenge models.S5Challenge
|
|
|
|
|
|
|
|
result := h.portal.Database().Model(&models.S5Challenge{}).Where(&models.S5Challenge{Pubkey: request.Pubkey, Type: "login"}).First(&challenge)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedResponse, err := base64.RawURLEncoding.DecodeString(request.Response)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:32:53 +00:00
|
|
|
if len(decodedResponse) != 65 {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedChallenge, err := base64.RawURLEncoding.DecodeString(challenge.Challenge)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:45:07 +00:00
|
|
|
if !bytes.Equal(decodedResponse[1:33], decodedChallenge) {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSignature, err := base64.RawURLEncoding.DecodeString(request.Signature)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:37:08 +00:00
|
|
|
if !ed25519.Verify(decodedKey[1:], decodedResponse, decodedSignature) {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(errInvalidSignatureErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jwt, err := h.portal.Accounts().LoginPubkey(request.Pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(errAccountLoginErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:48:45 +00:00
|
|
|
result = h.portal.Database().Delete(&challenge)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
errored(errAccountLoginErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:56:25 +00:00
|
|
|
setAuthCookie(jwt, jc)
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
2024-01-16 18:38:10 +00:00
|
|
|
|
2024-01-17 16:52:54 +00:00
|
|
|
func (h *HttpHandler) AccountInfo(jc jape.Context) {
|
|
|
|
_, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(AuthUserIDKey).(uint64))
|
|
|
|
|
|
|
|
info := &AccountInfoResponse{
|
|
|
|
Email: user.Email,
|
|
|
|
QuotaExceeded: false,
|
|
|
|
EmailConfirmed: false,
|
|
|
|
IsRestricted: false,
|
2024-01-17 17:38:52 +00:00
|
|
|
Tier: AccountTier{
|
|
|
|
Id: 1,
|
|
|
|
Name: "default",
|
|
|
|
UploadBandwidth: math.MaxUint64,
|
|
|
|
StorageLimit: math.MaxUint64,
|
|
|
|
Scopes: []interface{}{},
|
|
|
|
},
|
2024-01-17 16:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(info)
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:03:08 +00:00
|
|
|
func (h *HttpHandler) AccountStats(jc jape.Context) {
|
|
|
|
_, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(AuthUserIDKey).(uint64))
|
|
|
|
|
|
|
|
info := &AccountStatsResponse{
|
|
|
|
AccountInfoResponse: AccountInfoResponse{
|
|
|
|
Email: user.Email,
|
|
|
|
QuotaExceeded: false,
|
|
|
|
EmailConfirmed: false,
|
|
|
|
IsRestricted: false,
|
2024-01-17 17:38:52 +00:00
|
|
|
Tier: AccountTier{
|
|
|
|
Id: 1,
|
|
|
|
Name: "default",
|
|
|
|
UploadBandwidth: math.MaxUint64,
|
|
|
|
StorageLimit: math.MaxUint64,
|
|
|
|
Scopes: []interface{}{},
|
|
|
|
},
|
2024-01-17 17:03:08 +00:00
|
|
|
},
|
|
|
|
Stats: AccountStats{
|
|
|
|
Total: AccountStatsTotal{
|
|
|
|
UsedStorage: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(info)
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:33:05 +00:00
|
|
|
func (h *HttpHandler) AccountPins(jc jape.Context) {
|
|
|
|
var cursor uint64
|
|
|
|
|
|
|
|
if jc.DecodeForm("cursor", &cursor) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToGetPinsErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errFailedToGetPins, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
pins, err := h.portal.Accounts().AccountPins(jc.Request.Context().Value(AuthUserIDKey).(uint64), cursor)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pinsList := make([][]byte, len(pins))
|
|
|
|
|
|
|
|
for i, pin := range pins {
|
|
|
|
hash, err := hex.DecodeString(pin.Upload.Hash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pinsList[i] = encoding.MultihashFromBytes(hash, types.HashTypeBlake3).FullBytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := msgpack.Marshal(pinsList)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Custom(jc.Request, result)
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = jc.ResponseWriter.Write(result)
|
|
|
|
}
|
|
|
|
|
2024-01-17 18:04:32 +00:00
|
|
|
func (h *HttpHandler) AccountPinDelete(jc jape.Context) {
|
|
|
|
var cid string
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToDelPinErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errFailedToDelPin, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedCid, err := encoding.CIDFromString(cid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := hex.EncodeToString(decodedCid.Hash.HashBytes())
|
|
|
|
|
|
|
|
err = h.portal.Accounts().DeletePinByHash(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNoContent)
|
2024-01-17 18:13:37 +00:00
|
|
|
}
|
2024-01-17 18:04:32 +00:00
|
|
|
|
2024-01-17 18:13:37 +00:00
|
|
|
func (h *HttpHandler) AccountPin(jc jape.Context) {
|
|
|
|
var cid string
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToAddPinErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errFailedToAddPin, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedCid, err := encoding.CIDFromString(cid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := hex.EncodeToString(decodedCid.Hash.HashBytes())
|
|
|
|
|
|
|
|
err = h.portal.Accounts().PinByHash(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNoContent)
|
2024-01-17 18:04:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 19:46:37 +00:00
|
|
|
func (h *HttpHandler) DirectoryUpload(jc jape.Context) {
|
|
|
|
var tryFiles []string
|
|
|
|
var errorPages map[int]string
|
|
|
|
var name string
|
|
|
|
|
|
|
|
if jc.DecodeForm("tryFiles", &tryFiles) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if jc.DecodeForm("errorPages", &errorPages) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if jc.DecodeForm("name", &name) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := jc.Request
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(contentType, "multipart/form-data") {
|
|
|
|
_ = jc.Error(errNotMultiformErr, http.StatusBadRequest)
|
|
|
|
h.portal.Logger().Error(errorNotMultiform)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.ParseMultipartForm(h.portal.Config().GetInt64("core.post-upload-limit"))
|
|
|
|
|
|
|
|
if jc.Check(errMultiformParse, err) != nil {
|
|
|
|
h.portal.Logger().Error(errMultiformParse, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadMap := make(map[string]models.Upload, len(r.MultipartForm.File))
|
|
|
|
mimeMap := make(map[string]string, len(r.MultipartForm.File))
|
|
|
|
|
|
|
|
for _, files := range r.MultipartForm.File {
|
|
|
|
for _, fileHeader := range files {
|
|
|
|
// Open the file.
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func(file multipart.File) {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
|
|
|
h.portal.Logger().Error(errClosingStream, zap.Error(err))
|
|
|
|
}
|
|
|
|
}(file)
|
|
|
|
|
|
|
|
var rs io.ReadSeeker
|
|
|
|
|
|
|
|
hash, err := h.portal.Storage().GetHash(rs)
|
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists, upload := h.portal.Storage().FileExists(hash); exists {
|
|
|
|
uploadMap[fileHeader.Filename] = upload
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = h.portal.Storage().PutFile(rs, "s5", false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upload, err := h.portal.Storage().CreateUpload(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(fileHeader.Size), "s5")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the read pointer back to the start of the file.
|
|
|
|
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a snippet of the file to determine its MIME type.
|
|
|
|
buffer := make([]byte, 512) // 512 bytes should be enough for http.DetectContentType to determine the type
|
|
|
|
if _, err := file.Read(buffer); err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the read pointer back to the start of the file.
|
|
|
|
if _, err := file.Seek(0, 0); err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect MIME type.
|
|
|
|
mimeType := http.DetectContentType(buffer)
|
|
|
|
|
|
|
|
uploadMap[fileHeader.Filename] = *upload
|
|
|
|
mimeMap[fileHeader.Filename] = mimeType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filesMap := make(map[string]metadata.WebAppMetadataFileReference, len(uploadMap))
|
|
|
|
|
|
|
|
for name, file := range uploadMap {
|
|
|
|
hashDecoded, err := hex.DecodeString(file.Hash)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := encoding.CIDFromHash(hashDecoded, file.Size, types.CIDTypeRaw, types.HashTypeBlake3)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filesMap[name] = *metadata.NewWebAppMetadataFileReference(cid, mimeMap[name])
|
|
|
|
}
|
|
|
|
|
|
|
|
app := metadata.NewWebAppMetadata(name, tryFiles, *metadata.NewExtraMetadata(map[int]interface{}{}), errorPages, filesMap)
|
|
|
|
|
|
|
|
appData, err := msgpack.Marshal(app)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rs = bytes.NewReader(appData)
|
|
|
|
|
|
|
|
hash, err := h.portal.Storage().GetHash(rs)
|
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists, upload := h.portal.Storage().FileExists(hash); exists {
|
|
|
|
cid, err := encoding.CIDFromHash(hash, upload.Size, types.CIDTypeMetadataWebapp, types.HashTypeBlake3)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
|
|
|
h.portal.Logger().Error(errUploadingFile, zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jc.Encode(map[string]string{"hash": cidStr})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = h.portal.Storage().PutFile(rs, "s5", false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := encoding.CIDFromHash(hash, uint64(len(appData)), types.CIDTypeRaw, types.HashTypeBlake3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&AppUploadResponse{CID: cidStr})
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:38:10 +00:00
|
|
|
func setAuthCookie(jwt string, jc jape.Context) {
|
|
|
|
authCookie := http.Cookie{
|
|
|
|
Name: "s5-auth-token",
|
|
|
|
Value: jwt,
|
|
|
|
Path: "/",
|
|
|
|
HttpOnly: true,
|
|
|
|
MaxAge: int(time.Hour.Seconds() * 24),
|
|
|
|
Secure: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(jc.ResponseWriter, &authCookie)
|
|
|
|
}
|