forked from vmware-archive/atc
/
process_handler.go
66 lines (54 loc) · 1.45 KB
/
process_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package login
import (
"fmt"
"net/http"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/web"
"github.com/tedsuo/rata"
)
const CookieName = "ATC-Authorization"
const CookieAge = 24 * time.Hour
type processHandler struct {
logger lager.Logger
clientFactory web.ClientFactory
}
func NewProcessBasicAuthHandler(
logger lager.Logger,
clientFactory web.ClientFactory,
) http.Handler {
return &processHandler{
logger: logger,
clientFactory: clientFactory,
}
}
func (h *processHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
teamName := r.FormValue(":team_name")
username := r.FormValue("username")
password := r.FormValue("password")
redirect := r.FormValue("redirect")
if redirect == "" {
indexPath, err := web.Routes.CreatePathForRoute(web.Index, rata.Params{})
if err != nil {
h.logger.Error("failed-to-generate-index-path", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
redirect = indexPath
}
r.SetBasicAuth(username, password)
client := h.clientFactory.Build(r)
team := client.Team(teamName)
token, err := team.AuthToken()
if err != nil {
h.logger.Error("failed-to-get-token", err, lager.Data{})
w.WriteHeader(http.StatusInternalServerError)
}
http.SetCookie(w, &http.Cookie{
Name: CookieName,
Value: fmt.Sprintf("%s %s", token.Type, token.Value),
Path: "/",
Expires: time.Now().Add(CookieAge),
})
http.Redirect(w, r, redirect, http.StatusFound)
}