Skip to content

Commit

Permalink
fix: signup returns accesstoken
Browse files Browse the repository at this point in the history
  • Loading branch information
asabya committed Oct 25, 2024
1 parent e29c122 commit 0e74c37
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/dfs-cli/cmd/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func actOpenSharedPod(actName string) {
fmt.Println("could not open act: ", err)
return
}
currentPod = "podone"
currentPod = actName
currentDirectory = utils.PathSeparator
message := strings.ReplaceAll(string(data), "\n", "")
fmt.Println(message)
Expand Down
3 changes: 3 additions & 0 deletions cmd/dfs-cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func userNew(userName, mnemonic string) {
fdfsAPI.setAccessToken(resp.AccessToken)

currentUser = userName
message := strings.ReplaceAll(string(data), "\n", "")
fmt.Println(message)

}

func userLogin(userName, apiEndpoint string) {
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/user_signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package api

import (
"encoding/json"
"errors"
"net/http"

"github.com/fairdatasociety/fairOS-dfs/cmd/common"
Expand Down Expand Up @@ -90,12 +91,12 @@ func (h *Handler) UserSignupV2Handler(w http.ResponseWriter, r *http.Request) {
// create user
signUp, err := h.dfsAPI.CreateUserV2(user, password, mnemonic, "")
if err != nil {
if err == u.ErrUserAlreadyPresent {
if errors.Is(err, u.ErrUserAlreadyPresent) {
h.logger.Errorf("user signup: %v", err)
jsonhttp.BadRequest(w, &response{Message: "user signup: " + err.Error()})
return
}
if err == eth.ErrInsufficientBalance {
if errors.Is(err, eth.ErrInsufficientBalance) {
h.logger.Errorf("user signup: %v", err)
if signUp != nil {
jsonhttp.PaymentRequired(w, &UserSignupResponse{
Expand Down Expand Up @@ -130,10 +131,11 @@ func (h *Handler) UserSignupV2Handler(w http.ResponseWriter, r *http.Request) {
// send the response
w.Header().Set("Content-Type", " application/json")
jsonhttp.Created(w, &UserSignupResponse{
Address: signUp.Address,
Mnemonic: mnemonic,
NameHash: "0x" + signUp.NameHash,
PublicKey: signUp.PublicKey,
Message: "user signed-up successfully",
Address: signUp.Address,
Mnemonic: mnemonic,
NameHash: "0x" + signUp.NameHash,
PublicKey: signUp.PublicKey,
Message: "user signed-up successfully",
AccessToken: signUp.AccessToken,
})
}
4 changes: 4 additions & 0 deletions pkg/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (u *Users) LoginUserWithSignature(signature, password string, client blocks
pod := p.NewPod(u.client, fd, acc, tm, sm, u.feedCacheSize, u.feedCacheTTL, u.logger)
acl := acl2.NewACL(u.client, fd, u.logger)
group := p.NewGroup(u.client, fd, acc, acl, u.logger)
actList := act.NewACT(client, fd, acc, tm, u.logger)
if sessionId == "" {
sessionId = auth.GetUniqueSessionId()
}
Expand All @@ -183,6 +184,7 @@ func (u *Users) LoginUserWithSignature(signature, password string, client blocks
group: group,
openPods: make(map[string]*p.Info),
openPodsMu: &sync.RWMutex{},
actList: actList,
}

// set cookie and add user to map
Expand Down Expand Up @@ -329,6 +331,7 @@ func (u *Users) LoginWithWallet(addressHex, signature string, client blockstore.
acl := acl2.NewACL(u.client, fd, u.logger)
group := p.NewGroup(u.client, fd, acc, acl, u.logger)
dir := d.NewDirectory(addressHex, client, fd, accountInfo.GetAddress(), file, tm, u.logger)
actList := act.NewACT(client, fd, acc, tm, u.logger)
if sessionId == "" {
sessionId = auth.GetUniqueSessionId()
}
Expand All @@ -343,6 +346,7 @@ func (u *Users) LoginWithWallet(addressHex, signature string, client blockstore.
group: group,
openPods: make(map[string]*p.Info),
openPodsMu: &sync.RWMutex{},
actList: actList,
}
// set cookie and add user to map
return ui, utils.Encode(nameHash[:]), u.addUserAndSessionToMap(ui)
Expand Down
24 changes: 19 additions & 5 deletions pkg/user/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"regexp"
"sync"

"github.com/fairdatasociety/fairOS-dfs/pkg/act"

"github.com/fairdatasociety/fairOS-dfs/pkg/auth/jwt"

acl2 "github.com/fairdatasociety/fairOS-dfs/pkg/acl/acl"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -44,11 +48,12 @@ const (

// SignupResponse is the response of a successful signup
type SignupResponse struct {
Address string `json:"address"`
Mnemonic string `json:"mnemonic"`
NameHash string `json:"nameHash"`
PublicKey string `json:"publicKey"`
UserInfo *Info `json:"userInfo"`
Address string `json:"address"`
Mnemonic string `json:"mnemonic"`
NameHash string `json:"nameHash"`
PublicKey string `json:"publicKey"`
UserInfo *Info `json:"userInfo"`
AccessToken string `json:"accessToken"`
}

// CreateNewUserV2 creates a new user with the given username and password. if a mnemonic is passed
Expand Down Expand Up @@ -130,6 +135,7 @@ func (u *Users) CreateNewUserV2(userName, passPhrase, mnemonic, sessionId string
pod := p.NewPod(u.client, fd, acc, tm, sm, u.feedCacheSize, u.feedCacheTTL, u.logger)
acl := acl2.NewACL(u.client, fd, u.logger)
group := p.NewGroup(u.client, fd, acc, acl, u.logger)
actList := act.NewACT(u.client, fd, acc, tm, u.logger)
if sessionId == "" {
sessionId = auth.GetUniqueSessionId()
}
Expand All @@ -145,14 +151,22 @@ func (u *Users) CreateNewUserV2(userName, passPhrase, mnemonic, sessionId string
group: group,
openPods: make(map[string]*p.Info),
openPodsMu: &sync.RWMutex{},
actList: actList,
}

// set cookie and add user to map
if err = u.addUserAndSessionToMap(ui); err != nil { // skipcq: TCV-001
return nil, err
}

token, err := jwt.GenerateToken(sessionId)
if err != nil {
u.logger.Errorf("error generating token: %v\n", err)
}

signUp.UserInfo = ui
signUp.PublicKey = hex.EncodeToString(crypto.FromECDSAPub(accountInfo.GetPublicKey()))
signUp.AccessToken = token
return signUp, nil
}

Expand Down

0 comments on commit 0e74c37

Please sign in to comment.