-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlogzio_authentication_handler.go
271 lines (221 loc) · 7.09 KB
/
logzio_authentication_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
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
267
268
269
270
271
package kibana
import (
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
"github.com/parnurzeal/gorequest"
"github.com/xlzd/gotp"
)
const (
auth0MFAInvalidCode = "a0.mfa_invalid_code"
)
func NewLogzAuthenticationHandler(agent *gorequest.SuperAgent) *LogzAuthenticationHandler {
return &LogzAuthenticationHandler{}
}
func (auth *LogzAuthenticationHandler) Initialize(agent *gorequest.SuperAgent) error {
if auth.sessionToken != "" {
auth.setLogzHeaders(agent)
return nil
}
if auth.MfaSecret != "" {
return auth.initializeWithAuth0MFA(agent)
} else {
return auth.initializeWithAuth0(agent)
}
}
// auth0RO sends an Auth0 resource owner request with the given form
func (auth *LogzAuthenticationHandler) auth0RO(form string) (response *Auth0Response, err error) {
request := gorequest.New()
rawResponse, body, errs := request.Post(fmt.Sprintf("%s/oauth/ro", auth.Auth0Uri)).
Set("kbn-version", DefaultKibanaVersion553).
Set("Content-Type", "application/x-www-form-urlencoded").
Type("form").
Send(form).
End()
if errs != nil {
return nil, errs[0]
}
authResponse := &Auth0Response{}
if err := json.Unmarshal([]byte(body), authResponse); err != nil {
return nil, err
}
// Check for Auth0 errors
if authResponse.Error != "" {
error := fmt.Sprintf("Status: %d, Error: %s", rawResponse.StatusCode, authResponse.Error)
if authResponse.ErrorDescription != "" {
error += fmt.Sprintf(", Description: %s", authResponse.ErrorDescription)
}
error += fmt.Sprintf("\nResponse Body: %s", body)
// We still return authResponse here in case the caller wants to access the Auth0 errors
// e.g. to retry on MFA expiry
return authResponse, errors.New(error)
}
return authResponse, nil
}
// initializeWithAuth0 exchanges non-MFA credentials for a session token
func (auth *LogzAuthenticationHandler) initializeWithAuth0(agent *gorequest.SuperAgent) error {
csrfToken, err := auth.getCSRFToken()
if err != nil {
return err
}
form := fmt.Sprintf(`{
"scope": "openid email connection",
"response_type": "code",
"connection": "Username-Password-Authentication",
"username": "%s",
"password": "%s",
"grant_type": "password",
"client_id": "%s"
}`, auth.UserName, auth.Password, auth.ClientId)
authResponse, err := auth.auth0RO(form)
if err != nil {
return err
}
// create a brand new request instead of interfering with the one that
// we have as function argument
agent2 := gorequest.New().Post(fmt.Sprintf("%s/login/jwt", auth.LogzUri))
response, body, errs := agentWithCSRFToken(agent2, csrfToken).
Send(fmt.Sprintf(`{
"jwt": "%s"
}`, authResponse.IdTokens)).
End()
if errs != nil {
return errs[0]
}
if response.StatusCode >= 400 {
return fmt.Errorf("error logging in (%d). %s", response.StatusCode, string(body))
}
jwtResponse := map[string]interface{}{}
if err := json.Unmarshal([]byte(body), &jwtResponse); err != nil {
return err
}
auth.sessionToken = jwtResponse["sessionToken"].(string)
auth.setLogzHeaders(agent)
return nil
}
// initializeWithAuth0MFA exchanges MFA credentials for a session token
func (auth *LogzAuthenticationHandler) initializeWithAuth0MFA(agent *gorequest.SuperAgent) error {
request := gorequest.New()
csrfToken, err := auth.getCSRFToken()
if err != nil {
return err
}
sessionToken, err := auth.getLogzioSessionToken(true)
// If we're still failing, we cannot proceed
if err != nil {
return fmt.Errorf("Error getting MFA code: %s", err)
}
agent2 := request.Post(fmt.Sprintf("%s/login/jwt", auth.LogzUri))
response, body, errs := agentWithCSRFToken(agent2, csrfToken).
Send(fmt.Sprintf(`{
"jwt": "%s"
}`, sessionToken)).
End()
if response.StatusCode >= 400 {
return fmt.Errorf("error logging in (%d). %s", response.StatusCode, string(body))
}
if errs != nil {
return errs[0]
}
jwtResponse := map[string]interface{}{}
if err := json.Unmarshal([]byte(body), &jwtResponse); err != nil {
return err
}
auth.sessionToken = jwtResponse["sessionToken"].(string)
auth.setLogzHeaders(agent)
return nil
}
func (auth *LogzAuthenticationHandler) ChangeAccount(accountId string, agent *HttpAgent) error {
response, body, errs := agent.Get(fmt.Sprintf("%s/user/session/replace/%s", auth.LogzUri, accountId)).End()
if errs != nil {
return errs[0]
}
if response.StatusCode >= 400 {
return errors.New(fmt.Sprintf("Status: %d, %s", response.StatusCode, body))
}
responseMap := map[string]interface{}{}
if err := json.Unmarshal([]byte(body), &responseMap); err != nil {
return err
}
auth.sessionToken = responseMap["sessionToken"].(string)
return nil
}
func (auth *LogzAuthenticationHandler) getCSRFToken() (string, error) {
request := gorequest.New()
response, _, errs := request.Get(fmt.Sprintf("%s/#/login", auth.LogzUri)).
End()
if len(errs) > 0 {
return "", errs[0]
}
csrfToken, err := findCsrfTokenInCookies(response)
if err != nil {
return "", err
}
auth.csrfToken = csrfToken
return csrfToken, nil
}
var (
csrfRegexps = []*regexp.Regexp{
regexp.MustCompile("Logzio-Csrf=([^;]+)"),
regexp.MustCompile("Logzio-Csrf-V2=([^;]+)"),
}
)
func findCsrfTokenInCookies(response gorequest.Response) (string, error) {
for _, cookie := range response.Header["Set-Cookie"] {
token, err := findCsrfTokenInCookieUsingRegexps(cookie, csrfRegexps)
if err == nil && len(token) > 0 {
return token, nil
}
}
return "", errors.New("could not retrieve CSRF token from logz.io cookie")
}
func findCsrfTokenInCookieUsingRegexps(cookie string, regexps []*regexp.Regexp) (string, error) {
for _, regexp := range regexps {
matches := regexp.FindStringSubmatch(cookie)
if len(matches) > 1 {
return matches[1], nil
}
}
return "", fmt.Errorf("Cookie %s didn't match %v", cookie, regexps)
}
func (auth *LogzAuthenticationHandler) getLogzioSessionToken(retry bool) (sessionToken string, err error) {
mfaCode := auth.getMFACode()
form := fmt.Sprintf(`{
"scope": "openid email connection",
"response_type": "code",
"connection": "Username-Password-Authentication",
"username": "%s",
"password": "%s",
"grant_type": "password",
"client_id": "%s",
"mfa_code": "%s"
}`, auth.UserName, auth.Password, auth.ClientId, mfaCode)
authResponse, err := auth.auth0RO(form)
if authResponse != nil && authResponse.Error == auth0MFAInvalidCode && retry {
log.Print("MFA code potentially expired, so we re-generate and try again")
sessionToken, err = auth.getLogzioSessionToken(false)
if err != nil {
return
}
} else if err != nil {
return
}
sessionToken = authResponse.IdTokens
return
}
func (auth *LogzAuthenticationHandler) getMFACode() string {
return gotp.NewDefaultTOTP(auth.MfaSecret).Now()
}
func (auth *LogzAuthenticationHandler) setLogzHeaders(agent *gorequest.SuperAgent) *gorequest.SuperAgent {
return agentWithCSRFToken(agent, auth.csrfToken).
Set("x-auth-token", auth.sessionToken).
Set("Content-Type", "application/json")
}
func agentWithCSRFToken(agent *gorequest.SuperAgent, token string) *gorequest.SuperAgent {
return agent.
Set("X-Logz-CSRF-Token", token).
Set("X-Logz-CSRF-Token-V2", token).
Set("cookie", fmt.Sprintf("Logzio-Csrf=%s; Logzio-Csrf-V2=%s", token, token))
}