26c04b2c13603a2a7c9aa721b38a1fd5343c5228
[arvados.git] / sdk / go / arvados / login.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "bytes"
9         "encoding/json"
10         "net/http"
11 )
12
13 type LoginResponse struct {
14         RedirectLocation string       `json:"redirect_location,omitempty"`
15         Token            string       `json:"token,omitempty"`
16         Message          string       `json:"message,omitempty"`
17         HTML             bytes.Buffer `json:"-"`
18 }
19
20 func (resp LoginResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
21         w.Header().Set("Cache-Control", "no-store")
22         if resp.RedirectLocation != "" {
23                 w.Header().Set("Location", resp.RedirectLocation)
24                 w.WriteHeader(http.StatusFound)
25         } else if resp.Token != "" || resp.Message != "" {
26                 w.Header().Set("Content-Type", "application/json")
27                 if resp.Token == "" {
28                         w.WriteHeader(http.StatusUnauthorized)
29                 }
30                 json.NewEncoder(w).Encode(resp)
31         } else {
32                 w.Header().Set("Content-Type", "text/html")
33                 w.Write(resp.HTML.Bytes())
34         }
35 }
36
37 type LogoutResponse struct {
38         RedirectLocation string
39 }
40
41 func (resp LogoutResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
42         w.Header().Set("Location", resp.RedirectLocation)
43         w.WriteHeader(http.StatusFound)
44 }