2015-08-18 15:55:09 +08:00
|
|
|
package oauthutil
|
|
|
|
|
|
|
|
import (
|
2018-04-07 02:13:27 +08:00
|
|
|
"context"
|
2015-09-03 06:37:42 +08:00
|
|
|
"crypto/rand"
|
2015-08-18 15:55:09 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-09-03 06:37:42 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
2015-08-18 15:55:09 +08:00
|
|
|
"net/http"
|
2016-01-04 23:13:36 +08:00
|
|
|
"strings"
|
2016-05-24 01:03:22 +08:00
|
|
|
"sync"
|
2015-08-18 15:55:09 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-13 00:30:54 +08:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
|
|
|
"github.com/ncw/rclone/fs/fshttp"
|
2016-06-12 22:06:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2015-09-03 06:37:42 +08:00
|
|
|
"github.com/skratchdot/open-golang/open"
|
2015-08-18 15:55:09 +08:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2015-09-12 21:17:39 +08:00
|
|
|
const (
|
|
|
|
// 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
|
|
|
|
// prompting the user to copy the code and paste it in the application.
|
|
|
|
TitleBarRedirectURL = "urn:ietf:wg:oauth:2.0:oob"
|
|
|
|
|
2015-10-05 05:06:53 +08:00
|
|
|
// bindPort is the port that we bind the local webserver to
|
|
|
|
bindPort = "53682"
|
|
|
|
|
|
|
|
// bindAddress is binding for local webserver when active
|
|
|
|
bindAddress = "127.0.0.1:" + bindPort
|
2015-09-12 21:17:39 +08:00
|
|
|
|
|
|
|
// RedirectURL is redirect to local webserver when active
|
|
|
|
RedirectURL = "http://" + bindAddress + "/"
|
2015-10-05 05:06:53 +08:00
|
|
|
|
|
|
|
// RedirectPublicURL is redirect to local webserver when active with public name
|
|
|
|
RedirectPublicURL = "http://localhost.rclone.org:" + bindPort + "/"
|
2015-11-08 23:29:19 +08:00
|
|
|
|
|
|
|
// RedirectLocalhostURL is redirect to local webserver when active with localhost
|
|
|
|
RedirectLocalhostURL = "http://localhost:" + bindPort + "/"
|
2015-09-12 21:17:39 +08:00
|
|
|
)
|
2015-08-18 15:55:09 +08:00
|
|
|
|
|
|
|
// oldToken contains an end-user's tokens.
|
|
|
|
// This is the data you must store to persist authentication.
|
|
|
|
//
|
|
|
|
// From the original code.google.com/p/goauth2/oauth package - used
|
|
|
|
// for backwards compatibility in the rclone config file
|
|
|
|
type oldToken struct {
|
|
|
|
AccessToken string
|
|
|
|
RefreshToken string
|
|
|
|
Expiry time.Time
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:24:20 +08:00
|
|
|
// GetToken returns the token saved in the config file under
|
2015-08-18 15:55:09 +08:00
|
|
|
// section name.
|
2017-09-06 15:24:20 +08:00
|
|
|
func GetToken(name string) (*oauth2.Token, error) {
|
2018-01-13 00:30:54 +08:00
|
|
|
tokenString := config.FileGet(name, config.ConfigToken)
|
2015-08-18 15:55:09 +08:00
|
|
|
if tokenString == "" {
|
2016-06-12 22:06:02 +08:00
|
|
|
return nil, errors.New("empty token found - please run rclone config again")
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
token := new(oauth2.Token)
|
2016-12-21 02:03:09 +08:00
|
|
|
err := json.Unmarshal([]byte(tokenString), token)
|
2015-08-18 15:55:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// if has data then return it
|
2017-05-22 00:00:01 +08:00
|
|
|
if token.AccessToken != "" {
|
2015-08-18 15:55:09 +08:00
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
// otherwise try parsing as oldToken
|
|
|
|
oldtoken := new(oldToken)
|
|
|
|
err = json.Unmarshal([]byte(tokenString), oldtoken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Fill in result into new token
|
|
|
|
token.AccessToken = oldtoken.AccessToken
|
|
|
|
token.RefreshToken = oldtoken.RefreshToken
|
|
|
|
token.Expiry = oldtoken.Expiry
|
|
|
|
// Save new format in config file
|
2017-09-06 15:24:20 +08:00
|
|
|
err = PutToken(name, token, false)
|
2015-08-18 15:55:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:24:20 +08:00
|
|
|
// PutToken stores the token in the config file
|
2015-08-18 15:55:09 +08:00
|
|
|
//
|
|
|
|
// This saves the config file if it changes
|
2017-09-06 15:24:20 +08:00
|
|
|
func PutToken(name string, token *oauth2.Token, newSection bool) error {
|
2015-08-18 15:55:09 +08:00
|
|
|
tokenBytes, err := json.Marshal(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokenString := string(tokenBytes)
|
2018-01-13 00:30:54 +08:00
|
|
|
old := config.FileGet(name, config.ConfigToken)
|
2015-08-18 15:55:09 +08:00
|
|
|
if tokenString != old {
|
2018-01-13 00:30:54 +08:00
|
|
|
err = config.SetValueAndSave(name, config.ConfigToken, tokenString)
|
2017-08-05 18:04:42 +08:00
|
|
|
if newSection && err != nil {
|
|
|
|
fs.Debugf(name, "Added new token to config, still needs to be saved")
|
|
|
|
} else if err != nil {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Errorf(nil, "Failed to save new token in config file: %v", err)
|
2016-12-19 23:04:07 +08:00
|
|
|
} else {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(name, "Saved new token in config file")
|
2016-12-19 23:04:07 +08:00
|
|
|
}
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-24 01:03:22 +08:00
|
|
|
// TokenSource stores updated tokens in the config file
|
|
|
|
type TokenSource struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
name string
|
|
|
|
tokenSource oauth2.TokenSource
|
|
|
|
token *oauth2.Token
|
|
|
|
config *oauth2.Config
|
|
|
|
ctx context.Context
|
2016-08-09 02:02:27 +08:00
|
|
|
expiryTimer *time.Timer // signals whenever the token expires
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Token returns a token or an error.
|
|
|
|
// Token must be safe for concurrent use by multiple goroutines.
|
|
|
|
// The returned Token must not be modified.
|
|
|
|
//
|
|
|
|
// This saves the token in the config file if it has changed
|
2016-05-24 01:03:22 +08:00
|
|
|
func (ts *TokenSource) Token() (*oauth2.Token, error) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
|
|
|
|
// Make a new token source if required
|
|
|
|
if ts.tokenSource == nil {
|
|
|
|
ts.tokenSource = ts.config.TokenSource(ts.ctx, ts.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := ts.tokenSource.Token()
|
2015-08-18 15:55:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-24 01:03:22 +08:00
|
|
|
changed := *token != *ts.token
|
|
|
|
ts.token = token
|
|
|
|
if changed {
|
2016-08-09 02:02:27 +08:00
|
|
|
// Bump on the expiry timer if it is set
|
|
|
|
if ts.expiryTimer != nil {
|
|
|
|
ts.expiryTimer.Reset(ts.timeToExpiry())
|
|
|
|
}
|
2017-09-06 15:24:20 +08:00
|
|
|
err = PutToken(ts.name, token, false)
|
2015-09-22 14:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2016-05-24 01:03:22 +08:00
|
|
|
// Invalidate invalidates the token
|
|
|
|
func (ts *TokenSource) Invalidate() {
|
|
|
|
ts.mu.Lock()
|
|
|
|
ts.token.AccessToken = ""
|
|
|
|
ts.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-08-09 02:02:27 +08:00
|
|
|
// timeToExpiry returns how long until the token expires
|
|
|
|
//
|
|
|
|
// Call with the lock held
|
|
|
|
func (ts *TokenSource) timeToExpiry() time.Duration {
|
|
|
|
t := ts.token
|
|
|
|
if t == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if t.Expiry.IsZero() {
|
|
|
|
return 3E9 * time.Second // ~95 years
|
|
|
|
}
|
|
|
|
return t.Expiry.Sub(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnExpiry returns a channel which has the time written to it when
|
|
|
|
// the token expires. Note that there is only one channel so if
|
|
|
|
// attaching multiple go routines it will only signal to one of them.
|
|
|
|
func (ts *TokenSource) OnExpiry() <-chan time.Time {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
if ts.expiryTimer == nil {
|
|
|
|
ts.expiryTimer = time.NewTimer(ts.timeToExpiry())
|
|
|
|
}
|
|
|
|
return ts.expiryTimer.C
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:55:09 +08:00
|
|
|
// Check interface satisfied
|
2016-05-24 01:03:22 +08:00
|
|
|
var _ oauth2.TokenSource = (*TokenSource)(nil)
|
2015-08-18 15:55:09 +08:00
|
|
|
|
|
|
|
// Context returns a context with our HTTP Client baked in for oauth2
|
2017-11-24 17:07:03 +08:00
|
|
|
func Context(client *http.Client) context.Context {
|
|
|
|
return context.WithValue(context.Background(), oauth2.HTTPClient, client)
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
|
2015-09-17 03:08:40 +08:00
|
|
|
// overrideCredentials sets the ClientID and ClientSecret from the
|
2016-01-04 23:13:36 +08:00
|
|
|
// config file if they are not blank.
|
|
|
|
// If any value is overridden, true is returned.
|
2017-01-16 23:10:08 +08:00
|
|
|
// the origConfig is copied
|
2018-01-13 00:30:54 +08:00
|
|
|
func overrideCredentials(name string, origConfig *oauth2.Config) (newConfig *oauth2.Config, changed bool) {
|
|
|
|
newConfig = new(oauth2.Config)
|
|
|
|
*newConfig = *origConfig
|
2017-01-16 23:10:08 +08:00
|
|
|
changed = false
|
2018-01-13 00:30:54 +08:00
|
|
|
ClientID := config.FileGet(name, config.ConfigClientID)
|
2015-09-17 03:08:40 +08:00
|
|
|
if ClientID != "" {
|
2018-01-13 00:30:54 +08:00
|
|
|
newConfig.ClientID = ClientID
|
2016-01-04 23:13:36 +08:00
|
|
|
changed = true
|
2015-09-17 03:08:40 +08:00
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
ClientSecret := config.FileGet(name, config.ConfigClientSecret)
|
2015-09-17 03:08:40 +08:00
|
|
|
if ClientSecret != "" {
|
2018-01-13 00:30:54 +08:00
|
|
|
newConfig.ClientSecret = ClientSecret
|
2016-01-04 23:13:36 +08:00
|
|
|
changed = true
|
2015-09-17 03:08:40 +08:00
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
AuthURL := config.FileGet(name, config.ConfigAuthURL)
|
2017-06-09 03:35:32 +08:00
|
|
|
if AuthURL != "" {
|
2018-01-13 00:30:54 +08:00
|
|
|
newConfig.Endpoint.AuthURL = AuthURL
|
2017-06-09 03:35:32 +08:00
|
|
|
changed = true
|
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
TokenURL := config.FileGet(name, config.ConfigTokenURL)
|
2017-06-09 03:35:32 +08:00
|
|
|
if TokenURL != "" {
|
2018-01-13 00:30:54 +08:00
|
|
|
newConfig.Endpoint.TokenURL = TokenURL
|
2017-06-09 03:35:32 +08:00
|
|
|
changed = true
|
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
return newConfig, changed
|
2015-09-17 03:08:40 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 17:07:03 +08:00
|
|
|
// NewClientWithBaseClient gets a token from the config file and
|
|
|
|
// configures a Client with it. It returns the client and a
|
|
|
|
// TokenSource which Invalidate may need to be called on. It uses the
|
|
|
|
// httpClient passed in as the base client.
|
|
|
|
func NewClientWithBaseClient(name string, config *oauth2.Config, baseClient *http.Client) (*http.Client, *TokenSource, error) {
|
2017-01-16 23:10:08 +08:00
|
|
|
config, _ = overrideCredentials(name, config)
|
2017-09-06 15:24:20 +08:00
|
|
|
token, err := GetToken(name)
|
2015-08-18 15:55:09 +08:00
|
|
|
if err != nil {
|
2016-05-24 01:03:22 +08:00
|
|
|
return nil, nil, err
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set our own http client in the context
|
2017-11-24 17:07:03 +08:00
|
|
|
ctx := Context(baseClient)
|
2015-08-18 15:55:09 +08:00
|
|
|
|
|
|
|
// Wrap the TokenSource in our TokenSource which saves changed
|
|
|
|
// tokens in the config file
|
2016-05-24 01:03:22 +08:00
|
|
|
ts := &TokenSource{
|
|
|
|
name: name,
|
|
|
|
token: token,
|
|
|
|
config: config,
|
|
|
|
ctx: ctx,
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
2016-05-24 01:03:22 +08:00
|
|
|
return oauth2.NewClient(ctx, ts), ts, nil
|
2015-08-18 15:55:09 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-24 17:07:03 +08:00
|
|
|
// NewClient gets a token from the config file and configures a Client
|
|
|
|
// with it. It returns the client and a TokenSource which Invalidate may need to be called on
|
2018-01-13 00:30:54 +08:00
|
|
|
func NewClient(name string, oauthConfig *oauth2.Config) (*http.Client, *TokenSource, error) {
|
|
|
|
return NewClientWithBaseClient(name, oauthConfig, fshttp.NewClient(fs.Config))
|
2017-11-24 17:07:03 +08:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:55:09 +08:00
|
|
|
// Config does the initial creation of the token
|
2015-09-03 06:37:42 +08:00
|
|
|
//
|
2015-09-12 21:17:39 +08:00
|
|
|
// It may run an internal webserver to receive the results
|
2017-08-04 02:08:31 +08:00
|
|
|
func Config(id, name string, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error {
|
|
|
|
return doConfig(id, name, config, true, opts)
|
2017-06-14 23:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigNoOffline does the same as Config but does not pass the
|
|
|
|
// "access_type=offline" parameter.
|
2017-08-04 02:08:31 +08:00
|
|
|
func ConfigNoOffline(id, name string, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error {
|
|
|
|
return doConfig(id, name, config, false, opts)
|
2017-06-14 23:46:46 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
func doConfig(id, name string, oauthConfig *oauth2.Config, offline bool, opts []oauth2.AuthCodeOption) error {
|
|
|
|
oauthConfig, changed := overrideCredentials(name, oauthConfig)
|
|
|
|
automatic := config.FileGet(name, config.ConfigAutomatic) != ""
|
2016-01-04 23:13:36 +08:00
|
|
|
|
2017-03-03 06:36:00 +08:00
|
|
|
if changed {
|
2018-01-13 00:30:54 +08:00
|
|
|
fmt.Printf("Make sure your Redirect URL is set to %q in your custom config.\n", RedirectURL)
|
2017-03-03 06:36:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:55:09 +08:00
|
|
|
// See if already have a token
|
2018-01-13 00:30:54 +08:00
|
|
|
tokenString := config.FileGet(name, "token")
|
2015-08-18 15:55:09 +08:00
|
|
|
if tokenString != "" {
|
|
|
|
fmt.Printf("Already have a token - refresh?\n")
|
2018-01-13 00:30:54 +08:00
|
|
|
if !config.Confirm() {
|
2015-08-18 15:55:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 21:17:39 +08:00
|
|
|
// Detect whether we should use internal web server
|
|
|
|
useWebServer := false
|
2018-01-18 19:18:35 +08:00
|
|
|
switch oauthConfig.RedirectURL {
|
2015-11-08 23:29:19 +08:00
|
|
|
case RedirectURL, RedirectPublicURL, RedirectLocalhostURL:
|
2015-09-12 21:17:39 +08:00
|
|
|
useWebServer = true
|
2016-01-04 23:13:36 +08:00
|
|
|
if automatic {
|
|
|
|
break
|
|
|
|
}
|
2015-09-12 21:17:39 +08:00
|
|
|
fmt.Printf("Use auto config?\n")
|
|
|
|
fmt.Printf(" * Say Y if not sure\n")
|
2016-01-04 23:13:36 +08:00
|
|
|
fmt.Printf(" * Say N if you are working on a remote or headless machine\n")
|
2018-01-13 00:30:54 +08:00
|
|
|
auto := config.Confirm()
|
2016-01-04 23:13:36 +08:00
|
|
|
if !auto {
|
|
|
|
fmt.Printf("For this to work, you will need rclone available on a machine that has a web browser available.\n")
|
|
|
|
fmt.Printf("Execute the following on your machine:\n")
|
|
|
|
if changed {
|
2018-01-13 00:30:54 +08:00
|
|
|
fmt.Printf("\trclone authorize %q %q %q\n", id, oauthConfig.ClientID, oauthConfig.ClientSecret)
|
2016-01-04 23:13:36 +08:00
|
|
|
} else {
|
|
|
|
fmt.Printf("\trclone authorize %q\n", id)
|
|
|
|
}
|
|
|
|
fmt.Println("Then paste the result below:")
|
|
|
|
code := ""
|
|
|
|
for code == "" {
|
|
|
|
fmt.Printf("result> ")
|
2018-01-13 00:30:54 +08:00
|
|
|
code = strings.TrimSpace(config.ReadLine())
|
2016-01-04 23:13:36 +08:00
|
|
|
}
|
|
|
|
token := &oauth2.Token{}
|
|
|
|
err := json.Unmarshal([]byte(code), token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-06 15:24:20 +08:00
|
|
|
return PutToken(name, token, false)
|
2016-01-04 23:13:36 +08:00
|
|
|
}
|
|
|
|
case TitleBarRedirectURL:
|
|
|
|
useWebServer = automatic
|
|
|
|
if !automatic {
|
|
|
|
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 or Y didn't work\n")
|
2018-01-13 00:30:54 +08:00
|
|
|
useWebServer = config.Confirm()
|
2016-01-04 23:13:36 +08:00
|
|
|
}
|
2015-10-03 04:06:11 +08:00
|
|
|
if useWebServer {
|
|
|
|
// copy the config and set to use the internal webserver
|
2018-01-13 00:30:54 +08:00
|
|
|
configCopy := *oauthConfig
|
|
|
|
oauthConfig = &configCopy
|
|
|
|
oauthConfig.RedirectURL = RedirectURL
|
2015-10-03 04:06:11 +08:00
|
|
|
}
|
2015-09-12 21:17:39 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 06:37:42 +08:00
|
|
|
// Make random state
|
|
|
|
stateBytes := make([]byte, 16)
|
|
|
|
_, err := rand.Read(stateBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state := fmt.Sprintf("%x", stateBytes)
|
2017-06-14 23:46:46 +08:00
|
|
|
if offline {
|
|
|
|
opts = append(opts, oauth2.AccessTypeOffline)
|
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
authURL := oauthConfig.AuthCodeURL(state, opts...)
|
2015-09-03 06:37:42 +08:00
|
|
|
|
|
|
|
// Prepare webserver
|
|
|
|
server := authServer{
|
|
|
|
state: state,
|
|
|
|
bindAddress: bindAddress,
|
2015-09-23 01:47:16 +08:00
|
|
|
authURL: authURL,
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|
2015-09-12 21:17:39 +08:00
|
|
|
if useWebServer {
|
2015-09-11 20:26:51 +08:00
|
|
|
server.code = make(chan string, 1)
|
2015-09-03 06:37:42 +08:00
|
|
|
go server.Start()
|
|
|
|
defer server.Stop()
|
2015-09-23 01:47:16 +08:00
|
|
|
authURL = "http://" + bindAddress + "/auth"
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:55:09 +08:00
|
|
|
// Generate a URL for the user to visit for authorization.
|
2015-09-23 01:47:16 +08:00
|
|
|
_ = open.Start(authURL)
|
|
|
|
fmt.Printf("If your browser doesn't open automatically go to the following link: %s\n", authURL)
|
2015-09-11 20:26:51 +08:00
|
|
|
fmt.Printf("Log in and authorize rclone for access\n")
|
2015-08-18 15:55:09 +08:00
|
|
|
|
2015-09-11 20:26:51 +08:00
|
|
|
var authCode string
|
2015-09-12 21:17:39 +08:00
|
|
|
if useWebServer {
|
2015-09-11 20:26:51 +08:00
|
|
|
// Read the code, and exchange it for a token.
|
|
|
|
fmt.Printf("Waiting for code...\n")
|
|
|
|
authCode = <-server.code
|
|
|
|
if authCode != "" {
|
|
|
|
fmt.Printf("Got code\n")
|
|
|
|
} else {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.New("failed to get code")
|
2015-09-11 20:26:51 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Read the code, and exchange it for a token.
|
|
|
|
fmt.Printf("Enter verification code> ")
|
2018-01-13 00:30:54 +08:00
|
|
|
authCode = config.ReadLine()
|
2015-09-11 20:26:51 +08:00
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
token, err := oauthConfig.Exchange(oauth2.NoContext, authCode)
|
2015-08-18 15:55:09 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to get token")
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
2016-01-04 23:13:36 +08:00
|
|
|
|
|
|
|
// Print code if we do automatic retrieval
|
|
|
|
if automatic {
|
|
|
|
result, err := json.Marshal(token)
|
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to marshal token")
|
2016-01-04 23:13:36 +08:00
|
|
|
}
|
|
|
|
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste", result)
|
|
|
|
}
|
2017-09-06 15:24:20 +08:00
|
|
|
return PutToken(name, token, true)
|
2015-08-18 15:55:09 +08:00
|
|
|
}
|
2015-09-03 06:37:42 +08:00
|
|
|
|
|
|
|
// Local web server for collecting auth
|
|
|
|
type authServer struct {
|
|
|
|
state string
|
|
|
|
listener net.Listener
|
|
|
|
bindAddress string
|
2015-09-11 20:26:51 +08:00
|
|
|
code chan string
|
2015-09-23 01:47:16 +08:00
|
|
|
authURL string
|
2017-08-04 02:08:31 +08:00
|
|
|
server *http.Server
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// startWebServer runs an internal web server to receive config details
|
|
|
|
func (s *authServer) Start() {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(nil, "Starting auth server on %s", s.bindAddress)
|
2015-09-03 06:37:42 +08:00
|
|
|
mux := http.NewServeMux()
|
2017-08-04 02:08:31 +08:00
|
|
|
s.server = &http.Server{
|
2015-09-03 06:37:42 +08:00
|
|
|
Addr: s.bindAddress,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2017-08-04 02:08:31 +08:00
|
|
|
s.server.SetKeepAlivesEnabled(false)
|
2015-09-03 06:37:42 +08:00
|
|
|
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Error(w, "", 404)
|
|
|
|
return
|
|
|
|
})
|
2015-09-11 20:26:51 +08:00
|
|
|
mux.HandleFunc("/auth", func(w http.ResponseWriter, req *http.Request) {
|
2016-03-23 21:00:43 +08:00
|
|
|
http.Redirect(w, req, s.authURL, http.StatusTemporaryRedirect)
|
2015-09-11 20:26:51 +08:00
|
|
|
return
|
|
|
|
})
|
2015-09-03 06:37:42 +08:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
2016-03-23 21:00:43 +08:00
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(nil, "Received request on auth server")
|
2015-09-03 06:37:42 +08:00
|
|
|
code := req.FormValue("code")
|
|
|
|
if code != "" {
|
|
|
|
state := req.FormValue("state")
|
|
|
|
if state != s.state {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(nil, "State did not match: want %q got %q", s.state, state)
|
2015-09-03 06:37:42 +08:00
|
|
|
fmt.Fprintf(w, "<h1>Failure</h1>\n<p>Auth state doesn't match</p>")
|
|
|
|
} else {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(nil, "Successfully got code")
|
2015-09-11 20:26:51 +08:00
|
|
|
if s.code != nil {
|
|
|
|
fmt.Fprintf(w, "<h1>Success</h1>\n<p>Go back to rclone to continue</p>")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "<h1>Success</h1>\n<p>Cut and paste this code into rclone: <code>%s</code></p>", code)
|
|
|
|
}
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|
2017-11-10 05:34:44 +08:00
|
|
|
} else {
|
|
|
|
fs.Debugf(nil, "No code found on request")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "<h1>Failed!</h1>\nNo code found returned by remote server.")
|
|
|
|
}
|
|
|
|
if s.code != nil {
|
|
|
|
s.code <- code
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.listener, err = net.Listen("tcp", s.bindAddress)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start auth webserver: %v", err)
|
|
|
|
}
|
2017-08-04 02:08:31 +08:00
|
|
|
err = s.server.Serve(s.listener)
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(nil, "Closed auth server with error: %v", err)
|
2015-09-03 06:37:42 +08:00
|
|
|
}
|