Skip to content

Commit

Permalink
Merge pull request #421 from QuentinPerez/login
Browse files Browse the repository at this point in the history
fix login
  • Loading branch information
Quentin Perez authored Oct 24, 2016
2 parents 54e5d76 + 4c2e1be commit aae17ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

* No entry
* `scw login` fix CheckCredentials ([418](https://github.com/scaleway/scaleway-cli/issues/418))

View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.10...master)

Expand Down
23 changes: 21 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ type ScalewayTokensDefinition struct {
Token ScalewayTokenDefinition `json:"token"`
}

// ScalewayGetTokens represents a list of Scaleway Tokens
type ScalewayGetTokens struct {
Tokens []ScalewayTokenDefinition `json:"tokens"`
}

// ScalewayContainerData represents a Scaleway container data (S3)
type ScalewayContainerData struct {
LastModified string `json:"last_modified"`
Expand Down Expand Up @@ -1908,7 +1913,6 @@ func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) {
// CheckCredentials performs a dummy check to ensure we can contact the API
func (s *ScalewayAPI) CheckCredentials() error {
query := url.Values{}
query.Set("token_id", s.Token)

resp, err := s.GetResponsePaginate(AccountAPI, "tokens", query)
if resp != nil {
Expand All @@ -1917,10 +1921,25 @@ func (s *ScalewayAPI) CheckCredentials() error {
if err != nil {
return err
}
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return err
}
found := false
var tokens ScalewayGetTokens

if _, err := s.handleHTTPError([]int{http.StatusOK}, resp); err != nil {
if err = json.Unmarshal(body, &tokens); err != nil {
return err
}
for _, token := range tokens.Tokens {
if token.ID == s.Token {
found = true
break
}
}
if !found {
return fmt.Errorf("Invalid token %v", s.Token)
}
return nil
}

Expand Down

0 comments on commit aae17ae

Please sign in to comment.