AUTH-2763 don't redirect from curl command

This commit is contained in:
Dalton 2020-06-11 12:02:34 -05:00
parent c716dd273c
commit ae8d784e36
5 changed files with 23 additions and 9 deletions

View File

@ -131,7 +131,7 @@ func BuildAccessRequest(options *StartOptions, logger logger.Service) (*http.Req
return nil, err return nil, err
} }
token, err := token.FetchToken(req.URL, logger) token, err := token.FetchTokenWithRedirect(req.URL, logger)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -351,7 +351,7 @@ func sshGen(c *cli.Context) error {
// this fetchToken function mutates the appURL param. We should refactor that // this fetchToken function mutates the appURL param. We should refactor that
fetchTokenURL := &url.URL{} fetchTokenURL := &url.URL{}
*fetchTokenURL = *originURL *fetchTokenURL = *originURL
cfdToken, err := token.FetchToken(fetchTokenURL, logger) cfdToken, err := token.FetchTokenWithRedirect(fetchTokenURL, logger)
if err != nil { if err != nil {
return err return err
} }

View File

@ -127,8 +127,20 @@ func isTokenLocked(lockFilePath string) bool {
return exists && err == nil return exists && err == nil
} }
// FetchTokenWithRedirect will either load a stored token or generate a new one
// it appends a redirect URL to the access cli request if opening the browser
func FetchTokenWithRedirect(appURL *url.URL, logger logger.Service) (string, error) {
return getToken(appURL, true, logger)
}
// FetchToken will either load a stored token or generate a new one // FetchToken will either load a stored token or generate a new one
// it doesn't append a redirect URL to the access cli request if opening the browser
func FetchToken(appURL *url.URL, logger logger.Service) (string, error) { func FetchToken(appURL *url.URL, logger logger.Service) (string, error) {
return getToken(appURL, false, logger)
}
// getToken will either load a stored token or generate a new one
func getToken(appURL *url.URL, shouldRedirect bool, logger logger.Service) (string, error) {
if token, err := GetTokenIfExists(appURL); token != "" && err == nil { if token, err := GetTokenIfExists(appURL); token != "" && err == nil {
return token, nil return token, nil
} }
@ -154,7 +166,7 @@ func FetchToken(appURL *url.URL, logger logger.Service) (string, error) {
// this weird parameter is the resource name (token) and the key/value // this weird parameter is the resource name (token) and the key/value
// we want to send to the transfer service. the key is token and the value // we want to send to the transfer service. the key is token and the value
// is blank (basically just the id generated in the transfer service) // is blank (basically just the id generated in the transfer service)
token, err := transfer.Run(appURL, keyName, keyName, "", path, true, logger) token, err := transfer.Run(appURL, keyName, keyName, "", path, true, shouldRedirect, logger)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -28,12 +28,12 @@ const (
// The "dance" we refer to is building a HTTP request, opening that in a browser waiting for // The "dance" we refer to is building a HTTP request, opening that in a browser waiting for
// the user to complete an action, while it long polls in the background waiting for an // the user to complete an action, while it long polls in the background waiting for an
// action to be completed to download the resource. // action to be completed to download the resource.
func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncrypt bool, logger logger.Service) ([]byte, error) { func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncrypt bool, shouldRedirect bool, logger logger.Service) ([]byte, error) {
encrypterClient, err := encrypter.New("cloudflared_priv.pem", "cloudflared_pub.pem") encrypterClient, err := encrypter.New("cloudflared_priv.pem", "cloudflared_pub.pem")
if err != nil { if err != nil {
return nil, err return nil, err
} }
requestURL, err := buildRequestURL(transferURL, key, value+encrypterClient.PublicKey(), shouldEncrypt) requestURL, err := buildRequestURL(transferURL, key, value+encrypterClient.PublicKey(), shouldEncrypt, shouldRedirect)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -82,7 +82,7 @@ func Run(transferURL *url.URL, resourceName, key, value, path string, shouldEncr
// BuildRequestURL creates a request suitable for a resource transfer. // BuildRequestURL creates a request suitable for a resource transfer.
// it will return a constructed url based off the base url and query key/value provided. // it will return a constructed url based off the base url and query key/value provided.
// cli will build a url for cli transfer request. // cli will build a url for cli transfer request.
func buildRequestURL(baseURL *url.URL, key, value string, cli bool) (string, error) { func buildRequestURL(baseURL *url.URL, key, value string, cli, shouldRedirect bool) (string, error) {
q := baseURL.Query() q := baseURL.Query()
q.Set(key, value) q.Set(key, value)
baseURL.RawQuery = q.Encode() baseURL.RawQuery = q.Encode()
@ -90,8 +90,10 @@ func buildRequestURL(baseURL *url.URL, key, value string, cli bool) (string, err
return baseURL.String(), nil return baseURL.String(), nil
} }
q.Set("redirect_url", baseURL.String()) // we add the token as a query param on both the redirect_url if shouldRedirect {
baseURL.RawQuery = q.Encode() // and this actual baseURL. q.Set("redirect_url", baseURL.String()) // we add the token as a query param on both the redirect_url and the main url
}
baseURL.RawQuery = q.Encode() // and this actual baseURL.
baseURL.Path = "cdn-cgi/access/cli" baseURL.Path = "cdn-cgi/access/cli"
return baseURL.String(), nil return baseURL.String(), nil
} }

View File

@ -40,7 +40,7 @@ func login(c *cli.Context) error {
return err return err
} }
_, err = transfer.Run(loginURL, "cert", "callback", callbackStoreURL, path, false, logger) _, err = transfer.Run(loginURL, "cert", "callback", callbackStoreURL, path, false, true, logger)
if err != nil { if err != nil {
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) 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)
return err return err