forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authexternalbrowser.go
332 lines (298 loc) · 9.28 KB
/
authexternalbrowser.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (c) 2019-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/pkg/browser"
)
const (
successHTML = `<!DOCTYPE html><html><head><meta charset="UTF-8"/>
<title>SAML Response for Snowflake</title></head>
<body>
Your identity was confirmed and propagated to Snowflake %v.
You can close this window now and go back where you started from.
</body></html>`
)
const (
bufSize = 8192
)
// Builds a response to show to the user after successfully
// getting a response from Snowflake.
func buildResponse(application string) bytes.Buffer {
body := fmt.Sprintf(successHTML, application)
t := &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: io.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Request: nil,
Header: make(http.Header),
}
var b bytes.Buffer
t.Write(&b)
return b
}
// This opens a socket that listens on all available unicast
// and any anycast IP addresses locally. By specifying "0", we are
// able to bind to a free port.
func createLocalTCPListener() (*net.TCPListener, error) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
return nil, err
}
tcpListener, ok := l.(*net.TCPListener)
if !ok {
return nil, fmt.Errorf("failed to assert type as *net.TCPListener")
}
return tcpListener, nil
}
// Opens a browser window (or new tab) with the configured login Url.
// This can / will fail if running inside a shell with no display, ie
// ssh'ing into a box attempting to authenticate via external browser.
func openBrowser(loginURL string) error {
err := browser.OpenURL(loginURL)
if err != nil {
logger.Infof("failed to open a browser. err: %v", err)
return err
}
return nil
}
// Gets the IDP Url and Proof Key from Snowflake.
// Note: FuncPostAuthSaml will return a fully qualified error if
// there is something wrong getting data from Snowflake.
func getIdpURLProofKey(
ctx context.Context,
sr *snowflakeRestful,
authenticator string,
application string,
account string,
user string,
callbackPort int) (string, string, error) {
headers := make(map[string]string)
headers[httpHeaderContentType] = headerContentTypeApplicationJSON
headers[httpHeaderAccept] = headerContentTypeApplicationJSON
headers[httpHeaderUserAgent] = userAgent
clientEnvironment := authRequestClientEnvironment{
Application: application,
Os: operatingSystem,
OsVersion: platform,
}
requestMain := authRequestData{
ClientAppID: clientType,
ClientAppVersion: SnowflakeGoDriverVersion,
AccountName: account,
LoginName: user,
ClientEnvironment: clientEnvironment,
Authenticator: authenticator,
BrowserModeRedirectPort: strconv.Itoa(callbackPort),
}
authRequest := authRequest{
Data: requestMain,
}
jsonBody, err := json.Marshal(authRequest)
if err != nil {
logger.WithContext(ctx).Errorf("failed to serialize json. err: %v", err)
return "", "", err
}
respd, err := sr.FuncPostAuthSAML(ctx, sr, headers, jsonBody, sr.LoginTimeout)
if err != nil {
return "", "", err
}
if !respd.Success {
logger.Errorln("Authentication FAILED")
sr.TokenAccessor.SetTokens("", "", -1)
code, err := strconv.Atoi(respd.Code)
if err != nil {
code = -1
return "", "", err
}
return "", "", &SnowflakeError{
Number: code,
SQLState: SQLStateConnectionRejected,
Message: respd.Message,
}
}
return respd.Data.SSOURL, respd.Data.ProofKey, nil
}
// Gets the login URL for multiple SAML
func getLoginURL(sr *snowflakeRestful, user string, callbackPort int) (string, string, error) {
proofKey := generateProofKey()
params := &url.Values{}
params.Add("login_name", user)
params.Add("browser_mode_redirect_port", strconv.Itoa(callbackPort))
params.Add("proof_key", proofKey)
url := sr.getFullURL(consoleLoginRequestPath, params)
return url.String(), proofKey, nil
}
func generateProofKey() string {
randomness := getSecureRandom(32)
return base64.StdEncoding.WithPadding(base64.StdPadding).EncodeToString(randomness)
}
// The response returned from Snowflake looks like so:
// GET /?token=encodedSamlToken
// Host: localhost:54001
// Connection: keep-alive
// Upgrade-Insecure-Requests: 1
// User-Agent: userAgentStr
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
// Referer: https://myaccount.snowflakecomputing.com/fed/login
// Accept-Encoding: gzip, deflate, br
// Accept-Language: en-US,en;q=0.9
// This extracts the token portion of the response.
func getTokenFromResponse(response string) (string, error) {
start := "GET /?token="
arr := strings.Split(response, "\r\n")
if !strings.HasPrefix(arr[0], start) {
logger.Errorf("response is malformed. ")
return "", &SnowflakeError{
Number: ErrFailedToParseResponse,
SQLState: SQLStateConnectionRejected,
Message: errMsgFailedToParseResponse,
MessageArgs: []interface{}{response},
}
}
token := strings.TrimPrefix(arr[0], start)
token = strings.Split(token, " ")[0]
return token, nil
}
type authenticateByExternalBrowserResult struct {
escapedSamlResponse []byte
proofKey []byte
err error
}
func authenticateByExternalBrowser(
ctx context.Context,
sr *snowflakeRestful,
authenticator string,
application string,
account string,
user string,
password string,
externalBrowserTimeout time.Duration,
disableConsoleLogin ConfigBool,
) ([]byte, []byte, error) {
resultChan := make(chan authenticateByExternalBrowserResult, 1)
go func() {
resultChan <- doAuthenticateByExternalBrowser(ctx, sr, authenticator, application, account, user, password, disableConsoleLogin)
}()
select {
case <-time.After(externalBrowserTimeout):
return nil, nil, errors.New("authentication timed out")
case result := <-resultChan:
return result.escapedSamlResponse, result.proofKey, result.err
}
}
// Authentication by an external browser takes place via the following:
// - the golang snowflake driver communicates to Snowflake that the user wishes to
// authenticate via external browser
// - snowflake sends back the IDP Url configured at the Snowflake side for the
// provided account, or use the multiple SAML way via console login
// - the default browser is opened to that URL
// - user authenticates at the IDP, and is redirected to Snowflake
// - Snowflake directs the user back to the driver
// - authenticate is complete!
func doAuthenticateByExternalBrowser(
ctx context.Context,
sr *snowflakeRestful,
authenticator string,
application string,
account string,
user string,
password string,
disableConsoleLogin ConfigBool,
) authenticateByExternalBrowserResult {
l, err := createLocalTCPListener()
if err != nil {
return authenticateByExternalBrowserResult{nil, nil, err}
}
defer l.Close()
callbackPort := l.Addr().(*net.TCPAddr).Port
var loginURL string
var proofKey string
if disableConsoleLogin == ConfigBoolTrue {
// Gets the IDP URL and Proof Key from Snowflake
loginURL, proofKey, err = getIdpURLProofKey(ctx, sr, authenticator, application, account, user, callbackPort)
} else {
// Multiple SAML way to do authentication via console login
loginURL, proofKey, err = getLoginURL(sr, user, callbackPort)
}
if err != nil {
return authenticateByExternalBrowserResult{nil, nil, err}
}
if err = openBrowser(loginURL); err != nil {
return authenticateByExternalBrowserResult{nil, nil, err}
}
encodedSamlResponseChan := make(chan string)
errChan := make(chan error)
var encodedSamlResponse string
var errFromGoroutine error
conn, err := l.Accept()
if err != nil {
logger.WithContext(ctx).Errorf("unable to accept connection. err: %v", err)
log.Fatal(err)
}
go func(c net.Conn) {
var buf bytes.Buffer
total := 0
encodedSamlResponse := ""
var errAccept error
for {
b := make([]byte, bufSize)
n, err := c.Read(b)
if err != nil {
if err != io.EOF {
logger.Infof("error reading from socket. err: %v", err)
errAccept = &SnowflakeError{
Number: ErrFailedToGetExternalBrowserResponse,
SQLState: SQLStateConnectionRejected,
Message: errMsgFailedToGetExternalBrowserResponse,
MessageArgs: []interface{}{err},
}
}
break
}
total += n
buf.Write(b)
if n < bufSize {
// We successfully read all data
s := string(buf.Bytes()[:total])
encodedSamlResponse, errAccept = getTokenFromResponse(s)
break
}
buf.Grow(bufSize)
}
if encodedSamlResponse != "" {
httpResponse := buildResponse(application)
c.Write(httpResponse.Bytes())
}
c.Close()
encodedSamlResponseChan <- encodedSamlResponse
errChan <- errAccept
}(conn)
encodedSamlResponse = <-encodedSamlResponseChan
errFromGoroutine = <-errChan
if errFromGoroutine != nil {
return authenticateByExternalBrowserResult{nil, nil, errFromGoroutine}
}
escapedSamlResponse, err := url.QueryUnescape(encodedSamlResponse)
if err != nil {
logger.WithContext(ctx).Errorf("unable to unescape saml response. err: %v", err)
return authenticateByExternalBrowserResult{nil, nil, err}
}
return authenticateByExternalBrowserResult{[]byte(escapedSamlResponse), []byte(proofKey), nil}
}