drive, googlecloudstorage: optionally use auto config for the oauth token

This commit is contained in:
Nick Craig-Wood 2015-09-12 14:17:39 +01:00
parent 3ecbf2af25
commit f1226f19b2
4 changed files with 73 additions and 29 deletions

View file

@ -31,8 +31,6 @@ import (
const ( const (
rcloneClientID = "amzn1.application-oa2-client.6bf18d2d1f5b485c94c8988bb03ad0e7" rcloneClientID = "amzn1.application-oa2-client.6bf18d2d1f5b485c94c8988bb03ad0e7"
rcloneClientSecret = "k8/NyszKm5vEkZXAwsbGkd6C3NrbjIqMg4qEhIeF14Szub2wur+/teS3ubXgsLe9//+tr/qoqK+lq6mg8vWkoA==" rcloneClientSecret = "k8/NyszKm5vEkZXAwsbGkd6C3NrbjIqMg4qEhIeF14Szub2wur+/teS3ubXgsLe9//+tr/qoqK+lq6mg8vWkoA=="
bindAddress = "127.0.0.1:53682"
redirectURL = "http://" + bindAddress + "/"
folderKind = "FOLDER" folderKind = "FOLDER"
fileKind = "FILE" fileKind = "FILE"
assetKind = "ASSET" assetKind = "ASSET"
@ -54,7 +52,7 @@ var (
}, },
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: fs.Reveal(rcloneClientSecret), ClientSecret: fs.Reveal(rcloneClientSecret),
RedirectURL: redirectURL, RedirectURL: oauthutil.RedirectURL,
} }
) )
@ -64,7 +62,7 @@ func init() {
Name: "amazon cloud drive", Name: "amazon cloud drive",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string) { Config: func(name string) {
err := oauthutil.ConfigWithWebserver(name, acdConfig, bindAddress) err := oauthutil.Config(name, acdConfig)
if err != nil { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -1,7 +1,7 @@
--- ---
title: "Google drive" title: "Google drive"
description: "Rclone docs for Google drive" description: "Rclone docs for Google drive"
date: "2015-05-10" date: "2015-09-12"
--- ---
<i class="fa fa-google"></i> Google Drive <i class="fa fa-google"></i> Google Drive
@ -39,10 +39,16 @@ client_id>
Google Application Client Secret - leave blank to use rclone's. Google Application Client Secret - leave blank to use rclone's.
client_secret> client_secret>
Remote config Remote config
Go to the following link in your browser Use auto config?
https://accounts.google.com/o/oauth2/auth?access_type=&approval_prompt=&client_id=XXXXXXXXXXXX.apps.googleusercontent.com&redirect_uri=urn%3XXXXX%3Awg%3Aoauth%3XX.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state * Say Y if not sure
Log in, then type paste the token that is returned in the browser here * Say N if you are working on a remote or headless machine
Enter verification code> X/XXXXXXXXXXXXXXXXXX-XXXXXXXXX.XXXXXXXXX-XXXXX_XXXXXXX_XXXXXXX y) Yes
n) No
y/n> y
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
-------------------- --------------------
[remote] [remote]
client_id = client_id =
@ -55,6 +61,13 @@ d) Delete this remote
y/e/d> y y/e/d> y
``` ```
Note that rclone runs a webserver on your local machine to collect the
token as returned from Google if you use auto config mode. This only
runs from the moment it opens your browser to the moment you get back
the verification code. This is on `http://127.0.0.1:53682/` and this
it may require you to unblock it temporarily if you are running a host
firewall, or use manual mode.
You can then use it like this, You can then use it like this,
List directories in top level of your drive List directories in top level of your drive

View file

@ -1,7 +1,7 @@
--- ---
title: "Google Cloud Storage" title: "Google Cloud Storage"
description: "Rclone docs for Google Cloud Storage" description: "Rclone docs for Google Cloud Storage"
date: "2014-07-17" date: "2015-09-12"
--- ---
<i class="fa fa-google"></i> Google Cloud Storage <i class="fa fa-google"></i> Google Cloud Storage
@ -70,10 +70,17 @@ Choose a number from below, or type in your own value
5) publicReadWrite 5) publicReadWrite
bucket_acl> 2 bucket_acl> 2
Remote config Remote config
Go to the following link in your browser Remote config
https://accounts.google.com/o/oauth2/auth?access_type=&approval_prompt=&client_id=XXXXXXXXXXXX.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&state=state Use auto config?
Log in, then type paste the token that is returned in the browser here * Say Y if not sure
Enter verification code> x/xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx_xxxxxxxx * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> y
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
-------------------- --------------------
[remote] [remote]
type = google cloud storage type = google cloud storage
@ -90,6 +97,13 @@ d) Delete this remote
y/e/d> y y/e/d> y
``` ```
Note that rclone runs a webserver on your local machine to collect the
token as returned from Google if you use auto config mode. This only
runs from the moment it opens your browser to the moment you get back
the verification code. This is on `http://127.0.0.1:53682/` and this
it may require you to unblock it temporarily if you are running a host
firewall, or use manual mode.
This remote is called `remote` and can now be used like this This remote is called `remote` and can now be used like this
See all the buckets in your project See all the buckets in your project

View file

@ -15,13 +15,21 @@ import (
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
// configKey is the key used to store the token under const (
const configKey = "token" // configKey is the key used to store the token under
configKey = "token"
// TitleBarRedirectURL is the OAuth2 redirect URL to use when the authorization // TitleBarRedirectURL is the OAuth2 redirect URL to use when the authorization
// code should be returned in the title bar of the browser, with the page text // code should be returned in the title bar of the browser, with the page text
// prompting the user to copy the code and paste it in the application. // prompting the user to copy the code and paste it in the application.
const TitleBarRedirectURL = "urn:ietf:wg:oauth:2.0:oob" TitleBarRedirectURL = "urn:ietf:wg:oauth:2.0:oob"
// BindAddress is binding for local webserver when active
bindAddress = "127.0.0.1:53682"
// RedirectURL is redirect to local webserver when active
RedirectURL = "http://" + bindAddress + "/"
)
// oldToken contains an end-user's tokens. // oldToken contains an end-user's tokens.
// This is the data you must store to persist authentication. // This is the data you must store to persist authentication.
@ -144,8 +152,8 @@ func NewClient(name string, config *oauth2.Config) (*http.Client, error) {
// Config does the initial creation of the token // Config does the initial creation of the token
// //
// It runs an internal webserver to receive the results // It may run an internal webserver to receive the results
func ConfigWithWebserver(name string, config *oauth2.Config, bindAddress string) error { func Config(name string, config *oauth2.Config) error {
// See if already have a token // See if already have a token
tokenString := fs.ConfigFile.MustValue(name, "token") tokenString := fs.ConfigFile.MustValue(name, "token")
if tokenString != "" { if tokenString != "" {
@ -155,6 +163,22 @@ func ConfigWithWebserver(name string, config *oauth2.Config, bindAddress string)
} }
} }
// Detect whether we should use internal web server
useWebServer := false
switch config.RedirectURL {
case RedirectURL:
useWebServer = true
case TitleBarRedirectURL:
fmt.Printf("Use auto config?\n")
fmt.Printf(" * Say Y if not sure\n")
fmt.Printf(" * Say N if you are working on a remote or headless machine\n")
useWebServer = fs.Confirm()
// copy the config and set to use the internal webserver
configCopy := *config
config = &configCopy
config.RedirectURL = RedirectURL
}
// Make random state // Make random state
stateBytes := make([]byte, 16) stateBytes := make([]byte, 16)
_, err := rand.Read(stateBytes) _, err := rand.Read(stateBytes)
@ -170,7 +194,7 @@ func ConfigWithWebserver(name string, config *oauth2.Config, bindAddress string)
bindAddress: bindAddress, bindAddress: bindAddress,
authUrl: authUrl, authUrl: authUrl,
} }
if bindAddress != "" { if useWebServer {
server.code = make(chan string, 1) server.code = make(chan string, 1)
go server.Start() go server.Start()
defer server.Stop() defer server.Stop()
@ -183,7 +207,7 @@ func ConfigWithWebserver(name string, config *oauth2.Config, bindAddress string)
fmt.Printf("Log in and authorize rclone for access\n") fmt.Printf("Log in and authorize rclone for access\n")
var authCode string var authCode string
if bindAddress != "" { if useWebServer {
// Read the code, and exchange it for a token. // Read the code, and exchange it for a token.
fmt.Printf("Waiting for code...\n") fmt.Printf("Waiting for code...\n")
authCode = <-server.code authCode = <-server.code
@ -204,11 +228,6 @@ func ConfigWithWebserver(name string, config *oauth2.Config, bindAddress string)
return putToken(name, token) return putToken(name, token)
} }
// Config does the initial creation of the token
func Config(name string, config *oauth2.Config) error {
return ConfigWithWebserver(name, config, "")
}
// Local web server for collecting auth // Local web server for collecting auth
type authServer struct { type authServer struct {
state string state string