Inspect response code on authenticate for correct handling of authentication failures and unexpected status code returns.
diff --git a/authenticate.go b/authenticate.go
index 003fb48..ff609aa 100644
--- a/authenticate.go
+++ b/authenticate.go
@@ -1,6 +1,7 @@
package gophercloud
import (
+ "fmt"
"github.com/racker/perigee"
)
@@ -108,6 +109,20 @@
VersionId, VersionInfo, VersionList string
}
+type AuthError struct {
+ StatusCode int
+}
+
+func (ae *AuthError) Error() string {
+ switch ae.StatusCode {
+ case 401:
+ return "Auth failed. Bad credentials."
+
+ default:
+ return fmt.Sprintf("Auth failed. Status code is: %s.", ae.StatusCode)
+ }
+}
+
//
func getAuthCredentials(options AuthOptions) Auth {
if options.ApiKey == "" {
@@ -143,7 +158,7 @@
return nil, ErrCredentials
}
- err := perigee.Post(p.AuthEndpoint, perigee.Options{
+ resp, err := perigee.Request("POST", p.AuthEndpoint, perigee.Options{
CustomClient: c.httpClient,
ReqBody: &AuthContainer{
Auth: getAuthCredentials(options),
@@ -154,11 +169,21 @@
&access,
},
})
+
if err == nil {
- access.options = options
- access.provider = p
- access.context = c
+ switch resp.StatusCode {
+ case 200:
+ access.options = options
+ access.provider = p
+ access.context = c
+
+ default:
+ err = &AuthError {
+ StatusCode: resp.StatusCode,
+ }
+ }
}
+
return access, err
}