Skip to content

Commit

Permalink
login: fix checkCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Perez committed Oct 24, 2016
1 parent 54e5d76 commit 96042df
Showing 1 changed file with 21 additions and 2 deletions.
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 96042df

Please sign in to comment.