Skip to content

Commit

Permalink
Added token validation for login command
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pfohl committed Jun 30, 2023
1 parent effe522 commit c113873
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 9 deletions.
17 changes: 16 additions & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

"github.com/spf13/cobra"

"github.com/opf/openproject-cli/components/common"
"github.com/opf/openproject-cli/components/configuration"
"github.com/opf/openproject-cli/components/parser"
"github.com/opf/openproject-cli/components/paths"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/requests"
"github.com/opf/openproject-cli/components/resources/users"
"github.com/opf/openproject-cli/dtos"
)

Expand Down Expand Up @@ -65,7 +67,20 @@ func login(_ *cobra.Command, _ []string) {
continue
}

token = t
token = common.SanitizeLineBreaks(t)

requests.Init(hostUrl, token)
user, err := users.Me()
if err != nil {
printer.Error(err)
continue
}

if user.Name == "Anonymous" {
printer.ErrorText("no authenticate given")
continue
}

break
}

Expand Down
7 changes: 7 additions & 0 deletions components/common/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package common

import "strings"

func SanitizeLineBreaks(input string) string {
return strings.Replace(input, "\n", "", -1)
}
18 changes: 18 additions & 0 deletions components/common/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common_test

import (
"testing"

"github.com/opf/openproject-cli/components/common"
)

func TestSanitizeLineBreaks(t *testing.T) {
input := "abc\n"
expected := "abc"

result := common.SanitizeLineBreaks(input)

if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
4 changes: 2 additions & 2 deletions components/configuration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/opf/openproject-cli/components/common"
"github.com/opf/openproject-cli/components/errors"
)

Expand Down Expand Up @@ -44,8 +45,7 @@ func ReadConfig() (host, token string, err error) {
return "", "", nil
}

sanitized := strings.Replace(string(file), "\n", "", -1)
parts := strings.Split(sanitized, " ")
parts := strings.Split(common.SanitizeLineBreaks(string(file)), " ")
if len(parts) != 2 {
return "", "", errors.Custom(fmt.Sprintf("Invalid config file at %s. Please remove the file and run `op login` again.", configFile()))
}
Expand Down
4 changes: 4 additions & 0 deletions components/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func Users() string {
return Root() + "/users"
}

func UserMe() string {
return Users() + "/me"
}

func WorkPackage(id uint64) string {
return WorkPackages() + fmt.Sprintf("/%d", id)
}
Expand Down
2 changes: 1 addition & 1 deletion components/printer/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func apiError(status int, err apiErrorModel) {
status,
Yellow(err.ErrorIdentifier),
err.Messsage,
)
)
}

func indent(spaces int) (res string) {
Expand Down
9 changes: 4 additions & 5 deletions components/requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Patch(path string, requestBody *RequestData) (responseBody []byte, err erro

func Do(method string, path string, query *Query, requestData *RequestData) (responseBody []byte, err error) {
if client == nil || hostUnitialised() {
return nil, errors.Custom("Cannot execute requests without initializing request client first. Run `op login`")
return nil, errors.Custom("Cannot execute requests without initializing request client first. Run `op login`")
}

requestUrl := *host
Expand All @@ -58,7 +58,7 @@ func Do(method string, path string, query *Query, requestData *RequestData) (res
body,
)
if err != nil {
return nil, err
return nil, err
}

if requestData != nil {
Expand All @@ -71,14 +71,13 @@ func Do(method string, path string, query *Query, requestData *RequestData) (res

resp, err := client.Do(request)
if err != nil {
return nil, err
return nil, err
}


defer func() { _ = resp.Body.Close() }()
response, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, err
}

if !isSuccess(resp.StatusCode) {
Expand Down
11 changes: 11 additions & 0 deletions components/resources/users/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package users

import (
"github.com/opf/openproject-cli/components/parser"
"github.com/opf/openproject-cli/components/paths"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/requests"
"github.com/opf/openproject-cli/dtos"
Expand Down Expand Up @@ -30,3 +31,13 @@ func ByIds(ids []uint64) []*models.User {
userCollection := parser.Parse[dtos.UserCollectionDto](response)
return userCollection.Convert()
}

func Me() (*models.User, error) {
response, err := requests.Get(paths.UserMe(), nil)
if err != nil {
return nil, err
}

user := parser.Parse[dtos.UserDto](response)
return user.Convert(), nil
}

0 comments on commit c113873

Please sign in to comment.