16212: Support username/password authentication via PAM.
[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                 json.NewEncoder(w).Encode(resp)
28         } else {
29                 w.Header().Set("Content-Type", "text/html")
30                 w.Write(resp.HTML.Bytes())
31         }
32 }
33
34 type LogoutResponse struct {
35         RedirectLocation string
36 }
37
38 func (resp LogoutResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
39         w.Header().Set("Location", resp.RedirectLocation)
40         w.WriteHeader(http.StatusFound)
41 }