forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weblogin.go
454 lines (394 loc) · 13.9 KB
/
weblogin.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os/exec"
"runtime"
"strings"
"time"
"github.com/gravitational/teleport/lib/services"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/roundtrip"
"github.com/gravitational/trace"
"github.com/mailgun/lemma/secret"
"github.com/tstranex/u2f"
)
const (
// HTTPS is https prefix
HTTPS = "https"
// WSS is secure web sockets prefix
WSS = "wss"
)
// SSHLoginResponse is a response returned by web proxy
type SSHLoginResponse struct {
// User contains a logged in user informationn
Username string `json:"username"`
// Cert is a signed certificate
Cert []byte `json:"cert"`
// HostSigners is a list of signing host public keys
// trusted by proxy
HostSigners []services.CertAuthorityV1 `json:"host_signers"`
}
type OIDCLoginConsoleReq struct {
RedirectURL string `json:"redirect_url"`
PublicKey []byte `json:"public_key"`
CertTTL time.Duration `json:"cert_ttl"`
ConnectorID string `json:"connector_id"`
}
type OIDCLoginConsoleResponse struct {
RedirectURL string `json:"redirect_url"`
}
// A request from the client for a U2F sign request from the server
type U2fSignRequestReq struct {
User string `json:"user"`
Pass string `json:"pass"`
}
// CreateSSHCertReq are passed by web client
// to authenticate against teleport server and receive
// a temporary cert signed by auth server authority
type CreateSSHCertReq struct {
// User is a teleport username
User string `json:"user"`
// Password is user's pass
Password string `json:"password"`
// HOTPToken is second factor token
// Deprecated: HOTPToken is deprecated, use OTPToken.
HOTPToken string `json:"hotp_token"`
// OTPToken is second factor token
OTPToken string `json:"otp_token"`
// PubKey is a public key user wishes to sign
PubKey []byte `json:"pub_key"`
// TTL is a desired TTL for the cert (max is still capped by server,
// however user can shorten the time)
TTL time.Duration `json:"ttl"`
}
// CreateSSHCertWithU2FReq are passed by web client
// to authenticate against teleport server and receive
// a temporary cert signed by auth server authority
type CreateSSHCertWithU2FReq struct {
// User is a teleport username
User string `json:"user"`
// We only issue U2F sign requests after checking the password, so there's no need to check again.
// U2FSignResponse is the signature from the U2F device
U2FSignResponse u2f.SignResponse `json:"u2f_sign_response"`
// PubKey is a public key user wishes to sign
PubKey []byte `json:"pub_key"`
// TTL is a desired TTL for the cert (max is still capped by server,
// however user can shorten the time)
TTL time.Duration `json:"ttl"`
}
type sealData struct {
Value []byte `json:"value"`
Nonce []byte `json:"nonce"`
}
// SSHAgentOIDCLogin is used by SSH Agent (tsh) to login using OpenID connect
func SSHAgentOIDCLogin(proxyAddr, connectorID string, pubKey []byte, ttl time.Duration, insecure bool, pool *x509.CertPool) (*SSHLoginResponse, error) {
clt, proxyURL, err := initClient(proxyAddr, insecure, pool)
if err != nil {
return nil, trace.Wrap(err)
}
// create one time encoding secret that we will use to verify
// callback from proxy that is received over untrusted channel (HTTP)
keyBytes, err := secret.NewKey()
if err != nil {
return nil, trace.Wrap(err)
}
decryptor, err := secret.New(&secret.Config{KeyBytes: keyBytes})
if err != nil {
return nil, trace.Wrap(err)
}
waitC := make(chan *SSHLoginResponse, 1)
errorC := make(chan error, 1)
proxyURL.Path = "/web/msg/error/login_failed"
redirectErrorURL := proxyURL.String()
proxyURL.Path = "/web/msg/info/login_success"
redirectSuccessURL := proxyURL.String()
makeHandler := func(fn func(http.ResponseWriter, *http.Request) (*SSHLoginResponse, error)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := fn(w, r)
if err != nil {
if trace.IsNotFound(err) {
http.NotFound(w, r)
return
}
errorC <- err
http.Redirect(w, r, redirectErrorURL, http.StatusFound)
return
}
waitC <- response
http.Redirect(w, r, redirectSuccessURL, http.StatusFound)
})
}
server := httptest.NewServer(makeHandler(func(w http.ResponseWriter, r *http.Request) (*SSHLoginResponse, error) {
if r.URL.Path != "/callback" {
return nil, trace.NotFound("path not found")
}
encrypted := r.URL.Query().Get("response")
if encrypted == "" {
return nil, trace.BadParameter("missing required query parameters in %v", r.URL.String())
}
var encryptedData *secret.SealedBytes
err := json.Unmarshal([]byte(encrypted), &encryptedData)
if err != nil {
return nil, trace.BadParameter("failed to decode response in %v", r.URL.String())
}
out, err := decryptor.Open(encryptedData)
if err != nil {
return nil, trace.BadParameter("failed to decode response: in %v, err: %v", r.URL.String(), err)
}
var re *SSHLoginResponse
err = json.Unmarshal([]byte(out), &re)
if err != nil {
return nil, trace.BadParameter("failed to decode response: in %v, err: %v", r.URL.String(), err)
}
return re, nil
}))
defer server.Close()
u, err := url.Parse(server.URL + "/callback")
if err != nil {
return nil, trace.Wrap(err)
}
query := u.Query()
query.Set("secret", secret.KeyToEncodedString(keyBytes))
u.RawQuery = query.Encode()
out, err := clt.PostJSON(clt.Endpoint("webapi", "oidc", "login", "console"), OIDCLoginConsoleReq{
RedirectURL: u.String(),
PublicKey: pubKey,
CertTTL: ttl,
ConnectorID: connectorID,
})
if err != nil {
return nil, trace.Wrap(err)
}
var re *OIDCLoginConsoleResponse
err = json.Unmarshal(out.Bytes(), &re)
if err != nil {
return nil, trace.Wrap(err)
}
fmt.Printf("If browser window does not open automatically, open it by clicking on the link:\n %v\n", re.RedirectURL)
var command = "sensible-browser"
if runtime.GOOS == "darwin" {
command = "open"
}
path, err := exec.LookPath(command)
if err == nil {
exec.Command(path, re.RedirectURL).Start()
}
log.Infof("waiting for response on %v", server.URL)
select {
case err := <-errorC:
log.Debugf("got error: %v", err)
return nil, trace.Wrap(err)
case response := <-waitC:
log.Debugf("got response")
return response, nil
case <-time.After(60 * time.Second):
log.Debugf("got timeout waiting for callback")
return nil, trace.Wrap(trace.Errorf("timeout waiting for callback"))
}
}
// PingResponse contains data about the Teleport server like supported authentication
// types, server version, etc.
type PingResponse struct {
// Auth contains the forms of authentication the auth server supports.
Auth AuthenticationSettings `json:"auth"`
// ServerVersion is the version of Teleport that is running.
ServerVersion string `json:"server_version"`
}
// PingResponse contains the form of authentication the auth server supports.
type AuthenticationSettings struct {
// Type is the type of authentication, can be either local or oidc.
Type string `json:"type"`
// SecondFactor is the type of second factor to use in authentication.
// Supported options are: off, otp, and u2f.
SecondFactor string `json:"second_factor,omitempty"`
// U2F contains the Universal Second Factor settings needed for authentication.
U2F *U2FSettings `json:"u2f,omitempty"`
// OIDC contains the OIDC Connector settings needed for authentication.
OIDC *OIDCSettings `json:"oidc,omitempty"`
}
// U2FSettings contains the AppID for Universal Second Factor.
type U2FSettings struct {
// AppID is the U2F AppID.
AppID string `json:"app_id"`
}
// OIDCSettings contains the Name and Display string for OIDC.
type OIDCSettings struct {
// Name is the internal name of the connector.
Name string `json:"name"`
// Display is the display name for the connector.
Display string `json:"display"`
}
// Ping serves two purposes. The first is to validate the HTTP endpoint of a Teleport proxy. This leads
// to better user experience: users get connection errors before being asked for passwords. The second
// is to return the form of authentication that the server supports. This also leads to better user
// experience: users only get prompted for the type of authentication the server supports.
func Ping(proxyAddr string, insecure bool, pool *x509.CertPool) (*PingResponse, error) {
clt, _, err := initClient(proxyAddr, insecure, pool)
if err != nil {
return nil, trace.Wrap(err)
}
response, err := clt.Get(clt.Endpoint("webapi", "ping"), url.Values{})
if err != nil {
return nil, trace.Wrap(err)
}
var pr *PingResponse
err = json.Unmarshal(response.Bytes(), &pr)
if err != nil {
return nil, trace.Wrap(err)
}
return pr, nil
}
// SSHAgentLogin issues call to web proxy and receives temp certificate
// if credentials are valid
//
// proxyAddr must be specified as host:port
func SSHAgentLogin(proxyAddr, user, password, otpToken string, pubKey []byte, ttl time.Duration, insecure bool, pool *x509.CertPool) (*SSHLoginResponse, error) {
clt, _, err := initClient(proxyAddr, insecure, pool)
if err != nil {
return nil, trace.Wrap(err)
}
re, err := clt.PostJSON(clt.Endpoint("webapi", "ssh", "certs"), CreateSSHCertReq{
User: user,
Password: password,
OTPToken: otpToken,
PubKey: pubKey,
TTL: ttl,
})
if err != nil {
return nil, trace.Wrap(err)
}
var out *SSHLoginResponse
err = json.Unmarshal(re.Bytes(), &out)
if err != nil {
return nil, trace.Wrap(err)
}
return out, nil
}
// SSHAgentU2FLogin requests a U2F sign request (authentication challenge) via the proxy.
// If the credentials are valid, the proxy wiil return a challenge.
// We then call the official u2f-host binary to perform the signing and pass the signature to the proxy.
// If the authentication succeeds, we will get a temporary certificate back
func SSHAgentU2FLogin(proxyAddr, user, password string, pubKey []byte, ttl time.Duration, insecure bool, pool *x509.CertPool) (*SSHLoginResponse, error) {
clt, _, err := initClient(proxyAddr, insecure, pool)
if err != nil {
return nil, trace.Wrap(err)
}
u2fSignRequest, err := clt.PostJSON(clt.Endpoint("webapi", "u2f", "signrequest"), U2fSignRequestReq{
User: user,
Pass: password,
})
if err != nil {
return nil, trace.Wrap(err)
}
// Pass the JSON-encoded data undecoded to the u2f-host binary
facet := "https://" + strings.ToLower(proxyAddr)
cmd := exec.Command("u2f-host", "-aauthenticate", "-o", facet)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, trace.Wrap(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, trace.Wrap(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, trace.Wrap(err)
}
cmd.Start()
stdin.Write(u2fSignRequest.Bytes())
stdin.Close()
fmt.Println("Please press the button on your U2F key")
// The origin URL is passed back base64-encoded and the keyHandle is passed back as is.
// A very long proxy hostname or keyHandle can overflow a fixed-size buffer.
signResponseLen := 500 + len(u2fSignRequest.Bytes()) + len(proxyAddr)*4/3
signResponseBuf := make([]byte, signResponseLen)
signResponseLen, err = io.ReadFull(stdout, signResponseBuf)
// unexpected EOF means we have read the data completely.
if err == nil {
return nil, trace.LimitExceeded("u2f sign response exceeded buffer size")
}
// Read error message (if any). 100 bytes is more than enough for any error message u2f-host outputs
errMsgBuf := make([]byte, 100)
errMsgLen, err := io.ReadFull(stderr, errMsgBuf)
if err == nil {
return nil, trace.LimitExceeded("u2f error message exceeded buffer size")
}
err = cmd.Wait()
if err != nil {
return nil, trace.AccessDenied("u2f-host returned error: " + string(errMsgBuf[:errMsgLen]))
} else if signResponseLen == 0 {
return nil, trace.NotFound("u2f-host returned no error and no sign response")
}
var u2fSignResponse *u2f.SignResponse
err = json.Unmarshal(signResponseBuf[:signResponseLen], &u2fSignResponse)
if err != nil {
return nil, trace.Wrap(err)
}
re, err := clt.PostJSON(clt.Endpoint("webapi", "u2f", "certs"), CreateSSHCertWithU2FReq{
User: user,
U2FSignResponse: *u2fSignResponse,
PubKey: pubKey,
TTL: ttl,
})
if err != nil {
return nil, trace.Wrap(err)
}
var out *SSHLoginResponse
err = json.Unmarshal(re.Bytes(), &out)
if err != nil {
return nil, trace.Wrap(err)
}
return out, nil
}
// initClient creates and initializes HTTPS client for talking to teleport proxy HTTPS
// endpoint.
func initClient(proxyAddr string, insecure bool, pool *x509.CertPool) (*WebClient, *url.URL, error) {
log.Debugf("HTTPS client init(insecure=%v)", insecure)
// validate proxyAddr:
host, port, err := net.SplitHostPort(proxyAddr)
if err != nil || host == "" || port == "" {
if err != nil {
log.Error(err)
}
return nil, nil, trace.BadParameter("'%v' is not a valid proxy address", proxyAddr)
}
proxyAddr = "https://" + net.JoinHostPort(host, port)
u, err := url.Parse(proxyAddr)
if err != nil {
return nil, nil, trace.BadParameter("'%v' is not a valid proxy address", proxyAddr)
}
var opts []roundtrip.ClientParam
if insecure {
// skip https cert verification, oh no!
fmt.Printf("WARNING: You are using insecure connection to SSH proxy %v\n", proxyAddr)
opts = append(opts, roundtrip.HTTPClient(NewInsecureWebClient()))
} else if pool != nil {
// use custom set of trusted CAs
opts = append(opts, roundtrip.HTTPClient(newClientWithPool(pool)))
}
clt, err := NewWebClient(proxyAddr, opts...)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return clt, u, nil
}