feat: add AccountRegisterChallenge handler

This commit is contained in:
Derrick Hammer 2024-01-16 11:31:33 -05:00
parent a62c6daa4a
commit 69b1938e87
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 67 additions and 6 deletions

View File

@ -2,6 +2,8 @@ package s5
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
@ -27,10 +29,12 @@ const (
errReadFile = "Error reading the file"
errClosingStream = "Error closing the stream"
errUploadingFile = "Error uploading the file"
errAccountGenerateChallenge = "Error generating challenge"
)
var (
errUploadingFileErr = errors.New(errUploadingFile)
errAccountGenerateChallengeErr = errors.New(errAccountGenerateChallenge)
)
type HttpHandlerImpl struct {
@ -154,3 +158,60 @@ func (h *HttpHandlerImpl) SmallFileUpload(jc *jape.Context) {
jc.Encode(map[string]string{"hash": cidStr})
}
func (h *HttpHandlerImpl) AccountRegisterChallenge(jc *jape.Context) {
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
}
if len(decodedKey) != 32 {
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
return
}
result := h.portal.Database().Create(&models.S5Challenge{
Challenge: hex.EncodeToString(challenge),
})
if result.Error != nil {
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
h.portal.Logger().Error(errAccountGenerateChallenge, zap.Error(err))
return
}
jc.Encode(map[string]string{"challenge": base64.RawURLEncoding.EncodeToString(challenge)})
}
func (h *HttpHandlerImpl) AccountRegister(context *jape.Context) {
//TODO implement me
panic("implement me")
}
func (h *HttpHandlerImpl) AccountLoginChallenge(context *jape.Context) {
//TODO implement me
panic("implement me")
}
func (h *HttpHandlerImpl) AccountLogin(context *jape.Context) {
//TODO implement me
panic("implement me")
}