Skip to content

Commit 5010454

Browse files
author
Kevin Marshall
committed
AUTH-7260: Add support for login interstitial auto closure
Adds a switch `--auto-close` which automatically closes Access login interstitial windows/tabs immediately after the user chooses Approve or Deny.
1 parent 08efe4c commit 5010454

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

carrier/carrier.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ const (
2626
)
2727

2828
type StartOptions struct {
29-
AppInfo *token.AppInfo
30-
OriginURL string
31-
Headers http.Header
32-
Host string
33-
TLSClientConfig *tls.Config
29+
AppInfo *token.AppInfo
30+
OriginURL string
31+
Headers http.Header
32+
Host string
33+
TLSClientConfig *tls.Config
34+
AutoCloseInterstitial bool
3435
}
3536

3637
// Connection wraps up all the needed functions to forward over the tunnel
@@ -46,7 +47,6 @@ type StdinoutStream struct{}
4647
// Read will read from Stdin
4748
func (c *StdinoutStream) Read(p []byte) (int, error) {
4849
return os.Stdin.Read(p)
49-
5050
}
5151

5252
// Write will write to Stdout
@@ -139,7 +139,7 @@ func BuildAccessRequest(options *StartOptions, log *zerolog.Logger) (*http.Reque
139139
return nil, err
140140
}
141141

142-
token, err := token.FetchTokenWithRedirect(req.URL, options.AppInfo, log)
142+
token, err := token.FetchTokenWithRedirect(req.URL, options.AppInfo, options.AutoCloseInterstitial, log)
143143
if err != nil {
144144
return nil, err
145145
}

cmd/cloudflared/access/cmd.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func Commands() []*cli.Command {
104104
Name: "no-verbose",
105105
Usage: "print only the jwt to stdout",
106106
},
107+
&cli.BoolFlag{
108+
Name: "auto-close",
109+
Usage: "automatically close the auth interstitial after action",
110+
},
107111
&cli.StringFlag{
108112
Name: appURLFlag,
109113
},
@@ -322,7 +326,7 @@ func curl(c *cli.Context) error {
322326
log.Info().Msg("You don't have an Access token set. Please run access token <access application> to fetch one.")
323327
return run("curl", cmdArgs...)
324328
}
325-
tok, err = token.FetchToken(appURL, appInfo, log)
329+
tok, err = token.FetchToken(appURL, appInfo, c.Bool(cfdflags.AutoCloseInterstitial), log)
326330
if err != nil {
327331
log.Err(err).Msg("Failed to refresh token")
328332
return err
@@ -442,7 +446,7 @@ func sshGen(c *cli.Context) error {
442446
if err != nil {
443447
return err
444448
}
445-
cfdToken, err := token.FetchTokenWithRedirect(fetchTokenURL, appInfo, log)
449+
cfdToken, err := token.FetchTokenWithRedirect(fetchTokenURL, appInfo, c.Bool(cfdflags.AutoCloseInterstitial), log)
446450
if err != nil {
447451
return err
448452
}
@@ -542,7 +546,7 @@ func verifyTokenAtEdge(appUrl *url.URL, appInfo *token.AppInfo, c *cli.Context,
542546
if c.IsSet(sshTokenSecretFlag) {
543547
headers.Add(cfAccessClientSecretHeader, c.String(sshTokenSecretFlag))
544548
}
545-
options := &carrier.StartOptions{AppInfo: appInfo, OriginURL: appUrl.String(), Headers: headers}
549+
options := &carrier.StartOptions{AppInfo: appInfo, OriginURL: appUrl.String(), Headers: headers, AutoCloseInterstitial: c.Bool(cfdflags.AutoCloseInterstitial)}
546550

547551
if valid, err := isTokenValid(options, log); err != nil {
548552
return err

cmd/cloudflared/flags/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,7 @@ const (
163163

164164
// Management hostname to signify incoming management requests
165165
ManagementHostname = "management-hostname"
166+
167+
// Automatically close the login interstitial browser window after the user makes a decision.
168+
AutoCloseInterstitial = "auto-close"
166169
)

cmd/cloudflared/tunnel/login.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/urfave/cli/v2"
1313

1414
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
15+
cfdflags "github.com/cloudflare/cloudflared/cmd/cloudflared/flags"
1516
"github.com/cloudflare/cloudflared/config"
1617
"github.com/cloudflare/cloudflared/credentials"
1718
"github.com/cloudflare/cloudflared/logger"
@@ -97,6 +98,7 @@ func login(c *cli.Context) error {
9798
callbackStoreURL,
9899
false,
99100
false,
101+
c.Bool(cfdflags.AutoCloseInterstitial),
100102
log,
101103
)
102104
if err != nil {

token/token.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,18 @@ func Init(version string) {
185185

186186
// FetchTokenWithRedirect will either load a stored token or generate a new one
187187
// it appends the full url as the redirect URL to the access cli request if opening the browser
188-
func FetchTokenWithRedirect(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) {
189-
return getToken(appURL, appInfo, false, log)
188+
func FetchTokenWithRedirect(appURL *url.URL, appInfo *AppInfo, autoClose bool, log *zerolog.Logger) (string, error) {
189+
return getToken(appURL, appInfo, false, autoClose, log)
190190
}
191191

192192
// FetchToken will either load a stored token or generate a new one
193193
// it appends the host of the appURL as the redirect URL to the access cli request if opening the browser
194-
func FetchToken(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) {
195-
return getToken(appURL, appInfo, true, log)
194+
func FetchToken(appURL *url.URL, appInfo *AppInfo, autoClose bool, log *zerolog.Logger) (string, error) {
195+
return getToken(appURL, appInfo, true, autoClose, log)
196196
}
197197

198198
// getToken will either load a stored token or generate a new one
199-
func getToken(appURL *url.URL, appInfo *AppInfo, useHostOnly bool, log *zerolog.Logger) (string, error) {
199+
func getToken(appURL *url.URL, appInfo *AppInfo, useHostOnly bool, autoClose bool, log *zerolog.Logger) (string, error) {
200200
if token, err := GetAppTokenIfExists(appInfo); token != "" && err == nil {
201201
return token, nil
202202
}
@@ -249,18 +249,19 @@ func getToken(appURL *url.URL, appInfo *AppInfo, useHostOnly bool, log *zerolog.
249249
return appToken, nil
250250
}
251251
}
252-
return getTokensFromEdge(appURL, appInfo.AppAUD, appTokenPath, orgTokenPath, useHostOnly, log)
252+
return getTokensFromEdge(appURL, appInfo.AppAUD, appTokenPath, orgTokenPath, useHostOnly, autoClose, log)
253253
}
254254

255255
// getTokensFromEdge will attempt to use the transfer service to retrieve an app and org token, save them to disk,
256256
// and return the app token.
257-
func getTokensFromEdge(appURL *url.URL, appAUD, appTokenPath, orgTokenPath string, useHostOnly bool, log *zerolog.Logger) (string, error) {
257+
func getTokensFromEdge(appURL *url.URL, appAUD, appTokenPath, orgTokenPath string, useHostOnly bool, autoClose bool, log *zerolog.Logger) (string, error) {
258+
fmt.Println("Get tokens from edge ", autoClose)
258259
// If no org token exists or if it couldn't be exchanged for an app token, then run the transfer service flow.
259260

260261
// this weird parameter is the resource name (token) and the key/value
261262
// we want to send to the transfer service. the key is token and the value
262263
// is blank (basically just the id generated in the transfer service)
263-
resourceData, err := RunTransfer(appURL, appAUD, keyName, keyName, "", true, useHostOnly, log)
264+
resourceData, err := RunTransfer(appURL, appAUD, keyName, keyName, "", true, useHostOnly, autoClose, log)
264265
if err != nil {
265266
return "", errors.Wrap(err, "failed to run transfer service")
266267
}

token/transfer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ const (
2525
// The "dance" we refer to is building a HTTP request, opening that in a browser waiting for
2626
// the user to complete an action, while it long polls in the background waiting for an
2727
// action to be completed to download the resource.
28-
func RunTransfer(transferURL *url.URL, appAUD, resourceName, key, value string, shouldEncrypt bool, useHostOnly bool, log *zerolog.Logger) ([]byte, error) {
28+
func RunTransfer(transferURL *url.URL, appAUD, resourceName, key, value string, shouldEncrypt bool, useHostOnly bool, autoClose bool, log *zerolog.Logger) ([]byte, error) {
2929
encrypterClient, err := NewEncrypter("cloudflared_priv.pem", "cloudflared_pub.pem")
3030
if err != nil {
3131
return nil, err
3232
}
33-
requestURL, err := buildRequestURL(transferURL, appAUD, key, value+encrypterClient.PublicKey(), shouldEncrypt, useHostOnly)
33+
requestURL, err := buildRequestURL(transferURL, appAUD, key, value+encrypterClient.PublicKey(), shouldEncrypt, useHostOnly, autoClose)
3434
if err != nil {
3535
return nil, err
3636
}
@@ -75,7 +75,7 @@ func RunTransfer(transferURL *url.URL, appAUD, resourceName, key, value string,
7575
// BuildRequestURL creates a request suitable for a resource transfer.
7676
// it will return a constructed url based off the base url and query key/value provided.
7777
// cli will build a url for cli transfer request.
78-
func buildRequestURL(baseURL *url.URL, appAUD string, key, value string, cli, useHostOnly bool) (string, error) {
78+
func buildRequestURL(baseURL *url.URL, appAUD string, key, value string, cli, useHostOnly bool, autoClose bool) (string, error) {
7979
q := baseURL.Query()
8080
q.Set(key, value)
8181
q.Set("aud", appAUD)
@@ -90,7 +90,11 @@ func buildRequestURL(baseURL *url.URL, appAUD string, key, value string, cli, us
9090
q.Set("redirect_url", baseURL.String()) // we add the token as a query param on both the redirect_url and the main url
9191
q.Set("send_org_token", "true") // indicates that the cli endpoint should return both the org and app token
9292
q.Set("edge_token_transfer", "true") // use new LoginHelper service built on workers
93-
baseURL.RawQuery = q.Encode() // and this actual baseURL.
93+
if autoClose {
94+
q.Set("close_interstitial", "true") // Automatically close the success window.
95+
}
96+
97+
baseURL.RawQuery = q.Encode() // and this actual baseURL.
9498
baseURL.Path = "cdn-cgi/access/cli"
9599
return baseURL.String(), nil
96100
}

0 commit comments

Comments
 (0)