16212: Return 401 or 500 instead of 200 on authentication failure.
[arvados.git] / sdk / go / arvados / login.go
index 75ebc81c142b1ee167bafa7792923513af3c5120..26c04b2c13603a2a7c9aa721b38a1fd5343c5228 100644 (file)
@@ -6,12 +6,15 @@ package arvados
 
 import (
        "bytes"
+       "encoding/json"
        "net/http"
 )
 
 type LoginResponse struct {
-       RedirectLocation string
-       HTML             bytes.Buffer
+       RedirectLocation string       `json:"redirect_location,omitempty"`
+       Token            string       `json:"token,omitempty"`
+       Message          string       `json:"message,omitempty"`
+       HTML             bytes.Buffer `json:"-"`
 }
 
 func (resp LoginResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@@ -19,6 +22,12 @@ func (resp LoginResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        if resp.RedirectLocation != "" {
                w.Header().Set("Location", resp.RedirectLocation)
                w.WriteHeader(http.StatusFound)
+       } else if resp.Token != "" || resp.Message != "" {
+               w.Header().Set("Content-Type", "application/json")
+               if resp.Token == "" {
+                       w.WriteHeader(http.StatusUnauthorized)
+               }
+               json.NewEncoder(w).Encode(resp)
        } else {
                w.Header().Set("Content-Type", "text/html")
                w.Write(resp.HTML.Bytes())