Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #580, change cli to use access_token for longer sessions #585

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions cmd/dfs-cli/cmd/fdfs-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions cmd/dfs-cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,5 +77,5 @@ func GetSessionIdFromToken(request *http.Request) (sessionId string, err error)
if tokenStr == "" {
return "", ErrNoTokenInRequest
}
return Parse(tokenStr)
return parse(tokenStr)
}
Loading