Skip to content

Commit ca7d679

Browse files
author
Dalton
committed
AUTH-2902 redirect with just the root host on curl commands
1 parent af0d04d commit ca7d679

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

cmd/cloudflared/token/token.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ func isTokenLocked(lockFilePath string) bool {
128128
}
129129

130130
// FetchTokenWithRedirect will either load a stored token or generate a new one
131-
// it appends a redirect URL to the access cli request if opening the browser
131+
// it appends the full url as the redirect URL to the access cli request if opening the browser
132132
func FetchTokenWithRedirect(appURL *url.URL, logger logger.Service) (string, error) {
133-
return getToken(appURL, true, logger)
133+
return getToken(appURL, false, logger)
134134
}
135135

136136
// FetchToken will either load a stored token or generate a new one
137-
// it doesn't append a redirect URL to the access cli request if opening the browser
137+
// it appends the host of the appURL as the redirect URL to the access cli request if opening the browser
138138
func FetchToken(appURL *url.URL, logger logger.Service) (string, error) {
139-
return getToken(appURL, false, logger)
139+
return getToken(appURL, true, logger)
140140
}
141141

142142
// getToken will either load a stored token or generate a new one
143-
func getToken(appURL *url.URL, shouldRedirect bool, logger logger.Service) (string, error) {
143+
func getToken(appURL *url.URL, useHostOnly bool, logger logger.Service) (string, error) {
144144
if token, err := GetTokenIfExists(appURL); token != "" && err == nil {
145145
return token, nil
146146
}
@@ -166,7 +166,7 @@ func getToken(appURL *url.URL, shouldRedirect bool, logger logger.Service) (stri
166166
// this weird parameter is the resource name (token) and the key/value
167167
// we want to send to the transfer service. the key is token and the value
168168
// is blank (basically just the id generated in the transfer service)
169-
token, err := transfer.Run(appURL, keyName, keyName, "", path, true, shouldRedirect, logger)
169+
token, err := transfer.Run(appURL, keyName, keyName, "", path, true, useHostOnly, logger)
170170
if err != nil {
171171
return "", err
172172
}

cmd/cloudflared/transfer/transfer.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ const (
2828
// The "dance" we refer to is building a HTTP request, opening that in a browser waiting for
2929
// the user to complete an action, while it long polls in the background waiting for an
3030
// action to be completed to download the resource.
31-
func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncrypt bool, shouldRedirect bool, logger logger.Service) ([]byte, error) {
31+
func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncrypt bool, useHostOnly bool, logger logger.Service) ([]byte, error) {
3232
encrypterClient, err := encrypter.New("cloudflared_priv.pem", "cloudflared_pub.pem")
3333
if err != nil {
3434
return nil, err
3535
}
36-
requestURL, err := buildRequestURL(transferURL, key, value+encrypterClient.PublicKey(), shouldEncrypt, shouldRedirect)
36+
requestURL, err := buildRequestURL(transferURL, key, value+encrypterClient.PublicKey(), shouldEncrypt, useHostOnly)
3737
if err != nil {
3838
return nil, err
3939
}
@@ -82,18 +82,18 @@ func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncr
8282
// BuildRequestURL creates a request suitable for a resource transfer.
8383
// it will return a constructed url based off the base url and query key/value provided.
8484
// cli will build a url for cli transfer request.
85-
func buildRequestURL(baseURL *url.URL, key, value string, cli, shouldRedirect bool) (string, error) {
85+
func buildRequestURL(baseURL *url.URL, key, value string, cli, useHostOnly bool) (string, error) {
8686
q := baseURL.Query()
8787
q.Set(key, value)
8888
baseURL.RawQuery = q.Encode()
89+
if useHostOnly {
90+
baseURL.Path = ""
91+
}
8992
if !cli {
9093
return baseURL.String(), nil
9194
}
92-
93-
if shouldRedirect {
94-
q.Set("redirect_url", baseURL.String()) // we add the token as a query param on both the redirect_url and the main url
95-
}
96-
baseURL.RawQuery = q.Encode() // and this actual baseURL.
95+
q.Set("redirect_url", baseURL.String()) // we add the token as a query param on both the redirect_url and the main url
96+
baseURL.RawQuery = q.Encode() // and this actual baseURL.
9797
baseURL.Path = "cdn-cgi/access/cli"
9898
return baseURL.String(), nil
9999
}

cmd/cloudflared/tunnel/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func login(c *cli.Context) error {
4040
return err
4141
}
4242

43-
_, err = transfer.Run(loginURL, "cert", "callback", callbackStoreURL, path, false, true, logger)
43+
_, err = transfer.Run(loginURL, "cert", "callback", callbackStoreURL, path, false, false, logger)
4444
if err != nil {
4545
fmt.Fprintf(os.Stderr, "Failed to write the certificate due to the following error:\n%v\n\nYour browser will download the certificate instead. You will have to manually\ncopy it to the following path:\n\n%s\n", err, path)
4646
return err

0 commit comments

Comments
 (0)