diff --git a/cmd/dfs-cli/cmd/fdfs-api.go b/cmd/dfs-cli/cmd/fdfs-api.go index 5deb940a..8f48d963 100644 --- a/cmd/dfs-cli/cmd/fdfs-api.go +++ b/cmd/dfs-cli/cmd/fdfs-api.go @@ -42,9 +42,9 @@ const ( // fdfsClient is the http client for dfs type fdfsClient struct { - url string - client *http.Client - cookie *http.Cookie + url string + client *http.Client + accessToken string } func newFdfsClient(fdfsServer string) (*fdfsClient, error) { @@ -97,6 +97,14 @@ func (s *fdfsClient) CheckConnection() bool { return err == nil } +func (s *fdfsClient) setAccessToken(token string) { + s.accessToken = token +} + +func (s *fdfsClient) getAccessToken() string { + return s.accessToken +} + func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, error) { // prepare the request fullUrl := fmt.Sprintf(s.url + urlPath) @@ -118,8 +126,8 @@ func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, } } - if s.cookie != nil { - req.AddCookie(s.cookie) + if s.getAccessToken() != "" { + req.Header.Add("Authorization", "Bearer "+s.getAccessToken()) } // execute the request response, err := s.client.Do(req) @@ -150,10 +158,6 @@ func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, return nil, errors.New(resp.Message) } - if len(response.Cookies()) > 0 { - s.cookie = response.Cookies()[0] - } - data, err := io.ReadAll(response.Body) if err != nil { return nil, errors.New("error downloading data") @@ -190,8 +194,8 @@ func (s *fdfsClient) getReq(urlPath, argsString string) ([]byte, error) { } } - if s.cookie != nil { - req.AddCookie(s.cookie) + if s.getAccessToken() != "" { + req.Header.Add("Authorization", "Bearer "+s.getAccessToken()) } // execute the request @@ -219,10 +223,6 @@ func (s *fdfsClient) getReq(urlPath, argsString string) ([]byte, error) { return nil, errors.New(resp.Message) } - if len(response.Cookies()) > 0 { - s.cookie = response.Cookies()[0] - } - data, err := io.ReadAll(response.Body) if err != nil { return nil, errors.New("error downloading data") @@ -283,8 +283,8 @@ func (s *fdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int6 req.Header.Set(api.CompressionHeader, compValue) } - if s.cookie != nil { - req.AddCookie(s.cookie) + if s.getAccessToken() != "" { + req.Header.Add("Authorization", "Bearer "+s.getAccessToken()) } // execute the request @@ -338,8 +338,8 @@ func (s *fdfsClient) downloadMultipartFile(method, urlPath string, arguments map req.Header.Add("Content-Type", contentType) req.Header.Add("Content-Length", strconv.Itoa(len(body.Bytes()))) - if s.cookie != nil { - req.AddCookie(s.cookie) + if s.getAccessToken() != "" { + req.Header.Add("Authorization", "Bearer "+s.getAccessToken()) } // execute the request diff --git a/cmd/dfs-cli/cmd/user.go b/cmd/dfs-cli/cmd/user.go index 5c30eac8..3301baf8 100644 --- a/cmd/dfs-cli/cmd/user.go +++ b/cmd/dfs-cli/cmd/user.go @@ -66,6 +66,9 @@ func userNew(userName, mnemonic string) { fmt.Println("Please store the 12 words mnemonic safely") fmt.Println("if you loose that, you cannot recover the data in-case of an emergency.") fmt.Println("you can also use that mnemonic to access the data in-case this device is lost") + + fdfsAPI.setAccessToken(resp.AccessToken) + currentUser = userName } @@ -85,8 +88,17 @@ func userLogin(userName, apiEndpoint string) { 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 = userName message := strings.ReplaceAll(string(data), "\n", "") + fdfsAPI.setAccessToken(resp.AccessToken) + fmt.Println(message) } diff --git a/pkg/auth/jwt/jwt.go b/pkg/auth/jwt/jwt.go index ea8af127..0cbe3c9d 100644 --- a/pkg/auth/jwt/jwt.go +++ b/pkg/auth/jwt/jwt.go @@ -44,7 +44,7 @@ func GenerateToken(sessionId string) (string, error) { return token.SignedString(secret) } -func Parse(tokenStr string) (string, error) { +func parse(tokenStr string) (string, error) { claims := &Claims{} token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) { return secret, nil @@ -77,5 +77,5 @@ func GetSessionIdFromToken(request *http.Request) (sessionId string, err error) if tokenStr == "" { return "", ErrNoTokenInRequest } - return Parse(tokenStr) + return parse(tokenStr) }