From 69b1938e8787d7614af65e0457b08e18ae5235f2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 Jan 2024 11:31:33 -0500 Subject: [PATCH] feat: add AccountRegisterChallenge handler --- protocols/s5/http.go | 73 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/protocols/s5/http.go b/protocols/s5/http.go index a6be533..8c97440 100644 --- a/protocols/s5/http.go +++ b/protocols/s5/http.go @@ -2,6 +2,8 @@ package s5 import ( "bytes" + "crypto/rand" + "encoding/base64" "encoding/hex" "errors" "git.lumeweb.com/LumeWeb/libs5-go/encoding" @@ -22,15 +24,17 @@ var ( ) const ( - 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" + 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" ) var ( - errUploadingFileErr = errors.New(errUploadingFile) + 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") +}