forked from vmware-archive/atc
/
oauth_begin_handler.go
130 lines (111 loc) · 3.06 KB
/
oauth_begin_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package auth
import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"net/http"
"time"
"github.com/concourse/atc/db"
"strconv"
"code.cloudfoundry.org/lager"
)
const OAuthStateCookie = "_concourse_oauth_state"
type OAuthState struct {
Redirect string `json:"redirect"`
TeamName string `json:"team_name"`
FlyLocalPort string `json:"fly_local_port"`
}
type OAuthBeginHandler struct {
logger lager.Logger
providerFactory ProviderFactory
privateKey *rsa.PrivateKey
teamFactory db.TeamFactory
expire time.Duration
isTLSEnabled bool
}
func NewOAuthBeginHandler(
logger lager.Logger,
providerFactory ProviderFactory,
privateKey *rsa.PrivateKey,
teamFactory db.TeamFactory,
expire time.Duration,
isTLSEnabled bool,
) http.Handler {
return &OAuthBeginHandler{
logger: logger,
providerFactory: providerFactory,
privateKey: privateKey,
teamFactory: teamFactory,
expire: expire,
isTLSEnabled: isTLSEnabled,
}
}
func (handler *OAuthBeginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hLog := handler.logger.Session("oauth-begin")
providerName := r.FormValue(":provider")
teamName := r.FormValue("team_name")
team, found, err := handler.teamFactory.FindTeam(teamName)
if err != nil {
hLog.Error("failed-to-get-team", err, lager.Data{
"teamName": teamName,
"provider": providerName,
})
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
hLog.Info("failed-to-find-team", lager.Data{
"teamName": teamName,
})
w.WriteHeader(http.StatusNotFound)
return
}
provider, found, err := handler.providerFactory.GetProvider(team, providerName)
if err != nil {
handler.logger.Error("failed-to-get-provider", err, lager.Data{
"provider": providerName,
"teamName": teamName,
})
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
handler.logger.Info("team-does-not-have-auth-provider", lager.Data{
"provider": providerName,
})
w.WriteHeader(http.StatusNotFound)
return
}
_, err = strconv.Atoi(r.FormValue("fly_local_port"))
if r.FormValue("fly_local_port") != "" && err != nil {
handler.logger.Error("failed-to-convert-port-to-integer", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
oauthState, err := json.Marshal(OAuthState{
Redirect: r.FormValue("redirect"),
TeamName: teamName,
FlyLocalPort: r.FormValue("fly_local_port"),
})
if err != nil {
handler.logger.Error("failed-to-marshal-state", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
encodedState := base64.RawURLEncoding.EncodeToString(oauthState)
authCodeURL := provider.AuthCodeURL(encodedState)
authCookie := &http.Cookie{
Name: OAuthStateCookie,
Value: encodedState,
Path: "/",
Expires: time.Now().Add(handler.expire),
HttpOnly: true,
}
if handler.isTLSEnabled {
authCookie.Secure = true
}
// TODO: Add SameSite once Golang supports it
// https://github.com/golang/go/issues/15867
http.SetCookie(w, authCookie)
http.Redirect(w, r, authCodeURL, http.StatusTemporaryRedirect)
}