Skip to content

Commit

Permalink
Merge pull request #649 from fairDataSociety/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
asabya authored Oct 22, 2024
2 parents 9d2ec35 + bce6a42 commit a0d5930
Show file tree
Hide file tree
Showing 33 changed files with 2,739 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ cover.out

fairos.aar
fairos-sources.jar
fairos.wasm
fairos.wasm*
fairos.wasm.gz
6 changes: 6 additions & 0 deletions cmd/common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type UserLoginRequest struct {
Password string `json:"password,omitempty"`
}

// UserSignatureLoginRequest is the request body for user login with signature
type UserSignatureLoginRequest struct {
Signature string `json:"signature,omitempty"`
Password string `json:"password,omitempty"`
}

// PodRequest is the request body for pod creation
type PodRequest struct {
PodName string `json:"podName,omitempty"`
Expand Down
22 changes: 18 additions & 4 deletions cmd/dfs-cli/cmd/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ const (
apiDocLoadJson = apiVersion + "/doc/loadjson"
apiDocIndexJson = apiVersion + "/doc/indexjson"

apiUserSignupV2 = apiVersionV2 + "/user/signup"
apiUserLoginV2 = apiVersionV2 + "/user/login"
apiUserPresentV2 = apiVersionV2 + "/user/present"
apiUserDeleteV2 = apiVersionV2 + "/user/delete"
apiUserSignupV2 = apiVersionV2 + "/user/signup"
apiUserLoginV2 = apiVersionV2 + "/user/login"
apiUserSignatureLogin = apiVersionV2 + "/user/login-with-signature"
apiUserPresentV2 = apiVersionV2 + "/user/present"
apiUserDeleteV2 = apiVersionV2 + "/user/delete"
)

func newPrompt() {
Expand Down Expand Up @@ -144,6 +145,7 @@ var userSuggestions = []prompt.Suggest{
{Text: "new", Description: "create a new user (v2)"},
{Text: "del", Description: "delete a existing user (v2)"},
{Text: "login", Description: "login to a existing user (v2)"},
{Text: "signatureLogin", Description: "login with signature"},
{Text: "logout", Description: "logout from a logged-in user"},
{Text: "present", Description: "is user present (v2)"},
{Text: "stat", Description: "shows information about a user"},
Expand Down Expand Up @@ -308,6 +310,17 @@ func executor(in string) {
currentPod = ""
currentDirectory = ""
currentPrompt = getCurrentPrompt()
case "signatureLogin":
if len(blocks) < 3 {
fmt.Println("invalid command. Missing \"signature\" argument")
return
}
signature := blocks[2]
fmt.Println("signatureLogin", signature)
signatureLogin(signature, apiUserSignatureLogin)
currentPod = ""
currentDirectory = ""
currentPrompt = getCurrentPrompt()
case "present":
if len(blocks) < 3 {
fmt.Println("invalid command. Missing \"userName\" argument")
Expand Down Expand Up @@ -1023,6 +1036,7 @@ func help() {
fmt.Println(" - user <new> (user-name) (mnemonic) - create a new user and login as that user")
fmt.Println(" - user <del> - deletes a logged-in user")
fmt.Println(" - user <login> (user-name) - login as a given user")
fmt.Println(" - user <signatureLogin> (signature) - login with signature")
fmt.Println(" - user <logout> - logout a logged-in user")
fmt.Println(" - user <present> (user-name) - returns true if the user is present, false otherwise")
fmt.Println(" - user <stat> - shows information about a user")
Expand Down
30 changes: 30 additions & 0 deletions cmd/dfs-cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ func userLogin(userName, apiEndpoint string) {
fmt.Println(message)
}

func signatureLogin(signature, apiEndpoint string) {
password := getPassword()
loginUser := common.UserSignatureLoginRequest{
Signature: signature,
Password: password,
}
jsonData, err := json.Marshal(loginUser)
if err != nil {
fmt.Println("login user: error marshalling request")
return
}
data, err := fdfsAPI.postReq(http.MethodPost, apiEndpoint, jsonData)
if err != nil {
fmt.Println("login user: ", err)
return
}
var resp api.UserSignupResponse
err = json.Unmarshal(data, &resp)
if err != nil {
fmt.Println("create user: ", err)
return
}

currentUser = resp.Address
message := strings.ReplaceAll(string(data), "\n", "")
fdfsAPI.setAccessToken(resp.AccessToken)

fmt.Println(message)
}

func deleteUser(apiEndpoint string) {
password := getPassword()
delUser := common.UserSignupRequest{
Expand Down
8 changes: 6 additions & 2 deletions cmd/dfs/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -60,7 +62,7 @@ func startDevServer() {
dfsApi := dfs.NewMockDfsAPI(mockClient, users, logger)
handler = api.NewMockHandler(dfsApi, logger, []string{"http://localhost:3000"})
defer handler.Close()
httpPort = ":9093"
httpPort = ":9090"
pprofPort = ":9091"
srv := startHttpService(logger)
fmt.Printf("Server running at:http://127.0.0.1%s\n", httpPort)
Expand All @@ -72,6 +74,8 @@ func startDevServer() {
}()
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

batchOk := make([]byte, 32)
_, _ = rand.Read(batchOk)
fmt.Println(hex.EncodeToString(batchOk))
<-done
}
2 changes: 2 additions & 0 deletions cmd/dfs/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func startHttpService(logger logging.Logger) *http.Server {
router.HandleFunc("/public-file", handler.PublicPodGetFileHandler)
router.HandleFunc("/public-dir", handler.PublicPodGetDirHandler)
router.HandleFunc("/public-kv", handler.PublicPodKVEntryGetHandler)
router.HandleFunc("/public-pod-snapshot", handler.PodReceiveSnapshotHandler).Methods("GET")

redirectHandler := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
Expand Down Expand Up @@ -333,6 +334,7 @@ func startHttpService(logger logging.Logger) *http.Server {
baseRouterV2.Use(handler.LogMiddleware)
baseRouterV2.HandleFunc("/user/signup", handler.UserSignupV2Handler).Methods("POST")
baseRouterV2.HandleFunc("/user/login", handler.UserLoginV2Handler).Methods("POST")
baseRouterV2.HandleFunc("/user/login-with-signature", handler.UserLoginWithSignature).Methods("POST")
baseRouterV2.HandleFunc("/user/present", handler.UserPresentV2Handler).Methods("GET")
userRouterV2 := baseRouterV2.PathPrefix("/user/").Subrouter()
userRouterV2.Use(handler.LoginMiddleware)
Expand Down
Loading

0 comments on commit a0d5930

Please sign in to comment.