-
Notifications
You must be signed in to change notification settings - Fork 5
/
login.go
266 lines (248 loc) · 6.3 KB
/
login.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/sessions"
)
type LoginStruct struct {
Success bool
Flash Flash
CallbackUrl string
AuthUrl string
ClientID string
CustomName string `json:"custom_name,ommitempty"`
}
type Flash struct {
Type template.JS
Message string
Display bool
}
type ClientStruct struct {
ClientID string
ClientSecret string
}
type OAuthAccessResponse struct {
AccessToken string `json:"access_token"`
}
/*
login function
*/
func Login(w http.ResponseWriter, r *http.Request) {
session := GetSession(w, r, cookieName)
tmpl := template.Must(template.ParseFiles("templates/login.html"))
//already authd, render tmpl
ValidateAuthSession(session, w, r)
flashsession := GetSession(w, r, "flash-cookie")
flashes := flashsession.Flashes()
var flash Flash
if len(flashes) > 0 {
flash = flashes[0].(Flash)
}
err := flashsession.Save(r, w)
if err != nil {
fmt.Println(err)
}
// get auth url from server
oAuthServer, authErr := GetOauthServer()
if authErr != nil {
tmpl.Execute(w, authErr)
RedirectHome(w, r) //FIX go to login and display error
return
}
//if not authd and not a POST, render tmpl
if r.Method != http.MethodPost {
response := LoginStruct{
CallbackUrl: uiUrl,
AuthUrl: oAuthServer,
Flash: flash,
ClientID: clientID,
CustomName: customName,
}
tmpl.Execute(w, response)
return
}
// collect credentials from form
loginCreds := ClientStruct{
ClientID: r.FormValue("clientid"),
ClientSecret: r.FormValue("clientsecret"),
}
// post auth request
resp, err := http.PostForm(oAuthServer+"/oauth/token", url.Values{
"client_id": {loginCreds.ClientID},
"client_secret": {loginCreds.ClientSecret},
"grant_type": {"client_credentials"},
"response_type": {"token"},
"token_format": {"jwt"},
})
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
textBytes := []byte(body)
list := AuthResponse{}
if err := json.Unmarshal([]byte(textBytes), &list); err != nil {
fmt.Println(err)
}
if list.Error != "" {
flash := Flash{
Type: "notice",
Message: list.ErrorDesc,
Display: true,
}
session.AddFlash(flash)
}
if list.Error == "" {
// Authentication goes here
session.Values["access_token"] = list.AccessToken
session.Save(r, w)
flashes := session.Flashes()
if len(flashes) > 0 {
flash := flashes[0].(Flash)
response := LoginStruct{
Success: true,
Flash: flash,
CallbackUrl: uiUrl,
AuthUrl: oAuthServer,
ClientID: clientID,
CustomName: customName,
}
tmpl.Execute(w, response)
}
response := LoginStruct{
Success: true,
CallbackUrl: uiUrl,
AuthUrl: oAuthServer,
ClientID: clientID,
CustomName: customName,
}
tmpl.Execute(w, response)
}
flashes = session.Flashes()
if len(flashes) > 0 {
flash := flashes[0].(Flash)
response := LoginStruct{
Flash: flash,
CallbackUrl: uiUrl,
AuthUrl: oAuthServer,
ClientID: clientID,
CustomName: customName,
}
tmpl.Execute(w, response)
} else {
tmpl.Execute(w, nil)
}
return
}
/*
Logout
*/
func Logout(w http.ResponseWriter, r *http.Request) {
//can this be done better?
session := GetSession(w, r, cookieName)
session.Values["access_token"] = ""
session.Save(r, w)
RedirectLogin(w, r)
return
}
/*
call back function to interact with uaa
*/
func LoginCallback(w http.ResponseWriter, r *http.Request) {
httpClient := http.Client{}
session := GetSession(w, r, cookieName)
param, ok := r.URL.Query()["error_description"]
if ok {
respParam := strings.Join(param, "")
AddFlash(w, r, respParam, "warning")
RedirectLogin(w, r)
return
}
// get auth url from server
oAuthServer, err := GetOauthServer()
if err != nil {
RedirectHome(w, r) //FIX go to login and display error
return
}
// get code from query params
parseErr := r.ParseForm()
if parseErr != nil {
fmt.Fprintf(os.Stdout, "could not parse query: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
code := r.FormValue("code")
// call the uaa auth endpoint
reqURL := fmt.Sprintf("%s/oauth/token?client_id=%s&client_secret=%s&code=%s&grant_type=authorization_code&redirect_uri=%s/login/callback", oAuthServer, clientID, clientSecret, code, uiUrl)
req, err := http.NewRequest(http.MethodPost, reqURL, nil)
if err != nil {
fmt.Fprintf(os.Stdout, "could not create HTTP request: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
req.Header.Set("accept", "application/json")
req.SetBasicAuth(clientID, clientSecret)
// Send out the HTTP request
res, err := httpClient.Do(req)
reqBody, _ := ioutil.ReadAll(res.Body)
textBytes := []byte(reqBody)
if err != nil {
fmt.Fprintf(os.Stdout, "could not send HTTP request: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
defer res.Body.Close()
// Parse the request body into the `OAuthAccessResponse` struct
var t OAuthAccessResponse
if err := json.Unmarshal([]byte(textBytes), &t); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
}
session.Values["access_token"] = t.AccessToken
session.Save(r, w)
// go home now
w.Header().Set("Location", "/")
w.WriteHeader(http.StatusFound)
}
/*
validate session on login page without redirect loop
*/
func ValidateAuthSession(session *sessions.Session, w http.ResponseWriter, r *http.Request) {
accessToken, _ := session.Values["access_token"].(string)
if accessToken != "" {
var p jwt.Parser
token, _, _ := p.ParseUnverified(accessToken, &jwt.StandardClaims{})
if err := token.Claims.Valid(); err == nil {
RedirectHome(w, r)
}
}
return
}
func GetOauthServer() (string, interface{}) {
// get auth url from server
resp, err := http.Get(credhubServer + "/info")
if err != nil {
flash := Flash{
Type: "warning",
Message: "Error connecting to authorization server",
Display: true,
}
response := LoginStruct{
Flash: flash,
}
return "", response
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
authRespBytes := []byte(body)
authResp := AuthServerResponse{}
if authServErr := json.Unmarshal([]byte(authRespBytes), &authResp); err != nil {
fmt.Println(authServErr)
}
oAuthServer := authResp.AuthServer.URL
return oAuthServer, nil
}