wallet-sync-server/server/register.go
Daniel Krol 2fbcf6ee6d Get/Post WalletState, account recover, test client
A few things at once because it was faster to get a demo out the door. Skipping most test implementation though I made failing stubs so I know what to fill in later.

* Get/Post WalletState
* downloadKey/email so that a second client can log in, and/or recover from lost client
* Test client in Python to demonstrate the above
* Organize into packages
2022-01-04 16:07:23 -05:00

66 lines
1.5 KiB
Go

package server
import (
"encoding/json"
"fmt"
"log"
"net/http"
"orblivion/lbry-id/auth"
"orblivion/lbry-id/store"
)
type RegisterRequest struct {
Token auth.AuthTokenString `json:"token"`
PubKey auth.PublicKey `json:"publicKey"`
DeviceID string `json:"deviceId"`
Email string `json:"email"`
}
func (r *RegisterRequest) validate() bool {
return (r.Token != auth.AuthTokenString("") &&
r.PubKey != auth.PublicKey("") &&
r.DeviceID != "" &&
r.Email != "")
}
func (s *Server) register(w http.ResponseWriter, req *http.Request) {
var registerRequest RegisterRequest
if !getPostData(w, req, &registerRequest) {
return
}
if !s.checkAuth(
w,
registerRequest.PubKey,
registerRequest.DeviceID,
registerRequest.Token,
auth.ScopeFull,
) {
return
}
err := s.store.InsertEmail(registerRequest.PubKey, registerRequest.Email)
if err != nil {
if err == store.ErrDuplicateEmail || err == store.ErrDuplicateAccount {
errorJSON(w, http.StatusConflict, "Error registering")
} else {
internalServiceErrorJSON(w, err, "Error registering")
}
log.Print(err)
return
}
var registerResponse struct{} // no data to respond with, but keep it JSON
var response []byte
response, err = json.Marshal(registerResponse)
if err != nil {
internalServiceErrorJSON(w, err, "Error generating register response")
return
}
// TODO StatusCreated also for first walletState and/or for get auth token?
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, string(response))
}