Skip to content

Commit

Permalink
Added logic for parsing username from token (#29)
Browse files Browse the repository at this point in the history
* Added logic for parsing username from token

Signed-off-by: Jonsy13 <[email protected]>

* Removed error, as token check is present

Signed-off-by: Jonsy13 <[email protected]>
  • Loading branch information
Jonsy13 authored Sep 23, 2021
1 parent e515dc4 commit fe00c06
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/fatih/color v1.12.0
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/imdario/mergo v0.3.11 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package apis
import (
"encoding/json"
"errors"
"github.com/litmuschaos/litmusctl/pkg/utils"
"io/ioutil"
"net/http"

"github.com/litmuschaos/litmusctl/pkg/utils"

"github.com/litmuschaos/litmusctl/pkg/types"
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package apis
import (
"encoding/json"
"errors"
"github.com/litmuschaos/litmusctl/pkg/utils"
"io/ioutil"
"net/http"

"github.com/litmuschaos/litmusctl/pkg/utils"

"github.com/litmuschaos/litmusctl/pkg/types"
)

Expand Down Expand Up @@ -148,7 +149,7 @@ type Project struct {

// GetProjectDetails fetches details of the input user
func GetProjectDetails(c types.Credentials) (ProjectDetails, error) {
query := `{"query":"query {\n getUser(username: \"` + c.Username + `\"){\n id name created_at projects{ id name members{ role user_id } }\n}\n}"}`
query := `{"query":"query {\n getUser(username: \"` + c.Username + `\"){\n id created_at projects{ id name members{ role user_id } }\n}\n}"}`
resp, err := SendRequest(SendRequestParams{Endpoint: c.Endpoint + utils.GQLAPIPath, Token: c.Token}, []byte(query))
if err != nil {
return ProjectDetails{}, err
Expand Down
15 changes: 11 additions & 4 deletions pkg/cmd/config/setAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/golang-jwt/jwt"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/config"
"github.com/litmuschaos/litmusctl/pkg/types"
Expand Down Expand Up @@ -102,11 +103,17 @@ var setAccountCmd = &cobra.Command{

resp, err := apis.Auth(authInput)
utils.PrintError(err)
// Decoding token
token, _ := jwt.Parse(resp.AccessToken, nil)
if token == nil {
os.Exit(1)
}
claims, _ := token.Claims.(jwt.MapClaims)

var user = types.User{
ExpiresIn: fmt.Sprint(time.Now().Add(time.Second * time.Duration(resp.ExpiresIn)).Unix()),
Token: resp.AccessToken,
Username: authInput.Username,
Username: claims["username"].(string),
}

var users []types.User
Expand All @@ -127,7 +134,7 @@ var setAccountCmd = &cobra.Command{
APIVersion: "v1",
Kind: "Config",
CurrentAccount: authInput.Endpoint,
CurrentUser: authInput.Username,
CurrentUser: claims["username"].(string),
Accounts: accounts,
}

Expand All @@ -143,13 +150,13 @@ var setAccountCmd = &cobra.Command{
var updateLitmusCtlConfig = types.UpdateLitmusCtlConfig{
Account: account,
CurrentAccount: authInput.Endpoint,
CurrentUser: authInput.Username,
CurrentUser: claims["username"].(string),
}

err = config.UpdateLitmusCtlConfig(updateLitmusCtlConfig, configFilePath)
utils.PrintError(err)
}
utils.White_B.Printf("\naccount.username/%s configured", authInput.Username)
utils.White_B.Printf("\naccount.username/%s configured", claims["username"].(string))

} else {
utils.Red.Println("\nError: some flags are missing. Run 'litmusctl config set-account --help' for usage. ")
Expand Down

0 comments on commit fe00c06

Please sign in to comment.