2015-08-18 07:55:09 +00:00
|
|
|
package oauthutil
|
|
|
|
|
|
|
|
import (
|
2018-04-06 18:13:27 +00:00
|
|
|
"context"
|
2015-08-18 07:55:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-06-03 21:49:11 +00:00
|
|
|
"html/template"
|
2015-09-02 22:37:42 +00:00
|
|
|
"net"
|
2015-08-18 07:55:09 +00:00
|
|
|
"net/http"
|
2019-08-30 10:52:03 +00:00
|
|
|
"net/url"
|
2016-05-23 17:03:22 +00:00
|
|
|
"sync"
|
2015-08-18 07:55:09 +00:00
|
|
|
"time"
|
|
|
|
|
2016-06-12 14:06:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
2019-08-30 10:52:03 +00:00
|
|
|
"github.com/rclone/rclone/lib/random"
|
2015-09-02 22:37:42 +00:00
|
|
|
"github.com/skratchdot/open-golang/open"
|
2015-08-18 07:55:09 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2015-09-12 13:17:39 +00: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-04 21:06:53 +00: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 13:17:39 +00:00
|
|
|
|
|
|
|
// RedirectURL is redirect to local webserver when active
|
|
|
|
RedirectURL = "http://" + bindAddress + "/"
|
2015-10-04 21:06:53 +00:00
|
|
|
|
|
|
|
// RedirectPublicURL is redirect to local webserver when active with public name
|
|
|
|
RedirectPublicURL = "http://localhost.rclone.org:" + bindPort + "/"
|
2015-11-08 15:29:19 +00:00
|
|
|
|
|
|
|
// RedirectLocalhostURL is redirect to local webserver when active with localhost
|
|
|
|
RedirectLocalhostURL = "http://localhost:" + bindPort + "/"
|
2018-06-03 21:49:11 +00:00
|
|
|
|
2019-08-27 21:48:43 +00:00
|
|
|
// RedirectPublicSecureURL is a public https URL which
|
|
|
|
// redirects to the local webserver
|
|
|
|
RedirectPublicSecureURL = "https://oauth.rclone.org/"
|
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// AuthResponseTemplate is a template to handle the redirect URL for oauth requests
|
|
|
|
AuthResponseTemplate = `<!DOCTYPE html>
|
2018-06-03 21:49:11 +00:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>{{ if .OK }}Success!{{ else }}Failure!{{ end }}</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>{{ if .OK }}Success!{{ else }}Failure!{{ end }}</h1>
|
|
|
|
<hr>
|
|
|
|
<pre style="width: 750px; white-space: pre-wrap;">
|
|
|
|
{{ if eq .OK false }}
|
2019-08-30 10:52:03 +00:00
|
|
|
Error: {{ .Name }}<br>
|
|
|
|
{{ if .Description }}Description: {{ .Description }}<br>{{ end }}
|
|
|
|
{{ if .Code }}Code: {{ .Code }}<br>{{ end }}
|
|
|
|
{{ if .HelpURL }}Look here for help: <a href="{{ .HelpURL }}">{{ .HelpURL }}</a><br>{{ end }}
|
2018-06-03 21:49:11 +00:00
|
|
|
{{ else }}
|
|
|
|
All done. Please go back to rclone.
|
|
|
|
{{ end }}
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
2015-09-12 13:17:39 +00:00
|
|
|
)
|
2015-08-18 07:55:09 +00: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 07:24:20 +00:00
|
|
|
// GetToken returns the token saved in the config file under
|
2015-08-18 07:55:09 +00:00
|
|
|
// section name.
|
2018-05-14 17:06:57 +00:00
|
|
|
func GetToken(name string, m configmap.Mapper) (*oauth2.Token, error) {
|
|
|
|
tokenString, ok := m.Get(config.ConfigToken)
|
|
|
|
if !ok || tokenString == "" {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("empty token found - please run rclone config again")
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
token := new(oauth2.Token)
|
2016-12-20 18:03:09 +00:00
|
|
|
err := json.Unmarshal([]byte(tokenString), token)
|
2015-08-18 07:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// if has data then return it
|
2017-05-21 16:00:01 +00:00
|
|
|
if token.AccessToken != "" {
|
2015-08-18 07:55:09 +00: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
|
2018-05-14 17:06:57 +00:00
|
|
|
err = PutToken(name, m, token, false)
|
2015-08-18 07:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 07:24:20 +00:00
|
|
|
// PutToken stores the token in the config file
|
2015-08-18 07:55:09 +00:00
|
|
|
//
|
|
|
|
// This saves the config file if it changes
|
2018-05-14 17:06:57 +00:00
|
|
|
func PutToken(name string, m configmap.Mapper, token *oauth2.Token, newSection bool) error {
|
2015-08-18 07:55:09 +00:00
|
|
|
tokenBytes, err := json.Marshal(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokenString := string(tokenBytes)
|
2018-05-14 17:06:57 +00:00
|
|
|
old, ok := m.Get(config.ConfigToken)
|
|
|
|
if !ok || tokenString != old {
|
2018-01-12 16:30:54 +00:00
|
|
|
err = config.SetValueAndSave(name, config.ConfigToken, tokenString)
|
2017-08-05 10:04:42 +00: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 11:01:20 +00:00
|
|
|
fs.Errorf(nil, "Failed to save new token in config file: %v", err)
|
2016-12-19 15:04:07 +00:00
|
|
|
} else {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(name, "Saved new token in config file")
|
2016-12-19 15:04:07 +00:00
|
|
|
}
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:03:22 +00:00
|
|
|
// TokenSource stores updated tokens in the config file
|
|
|
|
type TokenSource struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
name string
|
2018-05-14 17:06:57 +00:00
|
|
|
m configmap.Mapper
|
2016-05-23 17:03:22 +00:00
|
|
|
tokenSource oauth2.TokenSource
|
|
|
|
token *oauth2.Token
|
|
|
|
config *oauth2.Config
|
|
|
|
ctx context.Context
|
2016-08-08 18:02:27 +00:00
|
|
|
expiryTimer *time.Timer // signals whenever the token expires
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 18:06:46 +00:00
|
|
|
// If token has expired then first try re-reading it from the config
|
2019-04-30 12:06:24 +00:00
|
|
|
// file in case a concurrently running rclone has updated it already
|
2019-01-04 18:06:46 +00:00
|
|
|
func (ts *TokenSource) reReadToken() bool {
|
|
|
|
tokenString, err := config.FileGetFresh(ts.name, config.ConfigToken)
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(ts.name, "Failed to read token out of config file: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newToken := new(oauth2.Token)
|
|
|
|
err = json.Unmarshal([]byte(tokenString), newToken)
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(ts.name, "Failed to parse token out of config file: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !newToken.Valid() {
|
|
|
|
fs.Debugf(ts.name, "Loaded invalid token from config file - ignoring")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fs.Debugf(ts.name, "Loaded fresh token from config file")
|
|
|
|
ts.token = newToken
|
|
|
|
ts.tokenSource = nil // invalidate since we changed the token
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:55:09 +00: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-23 17:03:22 +00:00
|
|
|
func (ts *TokenSource) Token() (*oauth2.Token, error) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
2019-01-04 18:06:46 +00:00
|
|
|
var (
|
|
|
|
token *oauth2.Token
|
|
|
|
err error
|
|
|
|
changed = false
|
|
|
|
)
|
|
|
|
const maxTries = 5
|
|
|
|
|
|
|
|
// Try getting the token a few times
|
|
|
|
for i := 1; i <= maxTries; i++ {
|
|
|
|
// Try reading the token from the config file in case it has
|
|
|
|
// been updated by a concurrent rclone process
|
|
|
|
if !ts.token.Valid() {
|
|
|
|
if ts.reReadToken() {
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 17:03:22 +00:00
|
|
|
|
2019-01-04 18:06:46 +00:00
|
|
|
// Make a new token source if required
|
|
|
|
if ts.tokenSource == nil {
|
|
|
|
ts.tokenSource = ts.config.TokenSource(ts.ctx, ts.token)
|
|
|
|
}
|
2016-05-23 17:03:22 +00:00
|
|
|
|
2019-01-04 18:06:46 +00:00
|
|
|
token, err = ts.tokenSource.Token()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fs.Debugf(ts.name, "Token refresh failed try %d/%d: %v", i, maxTries, err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2015-08-18 07:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-04 18:06:46 +00:00
|
|
|
changed = changed || (*token != *ts.token)
|
2016-05-23 17:03:22 +00:00
|
|
|
ts.token = token
|
|
|
|
if changed {
|
2016-08-08 18:02:27 +00:00
|
|
|
// Bump on the expiry timer if it is set
|
|
|
|
if ts.expiryTimer != nil {
|
|
|
|
ts.expiryTimer.Reset(ts.timeToExpiry())
|
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
err = PutToken(ts.name, ts.m, token, false)
|
2015-09-22 06:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:03:22 +00:00
|
|
|
// Invalidate invalidates the token
|
|
|
|
func (ts *TokenSource) Invalidate() {
|
|
|
|
ts.mu.Lock()
|
|
|
|
ts.token.AccessToken = ""
|
|
|
|
ts.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:02:27 +00: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() {
|
2019-09-05 12:59:06 +00:00
|
|
|
return 3e9 * time.Second // ~95 years
|
2016-08-08 18:02:27 +00:00
|
|
|
}
|
|
|
|
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 07:55:09 +00:00
|
|
|
// Check interface satisfied
|
2016-05-23 17:03:22 +00:00
|
|
|
var _ oauth2.TokenSource = (*TokenSource)(nil)
|
2015-08-18 07:55:09 +00:00
|
|
|
|
|
|
|
// Context returns a context with our HTTP Client baked in for oauth2
|
2017-11-24 09:07:03 +00:00
|
|
|
func Context(client *http.Client) context.Context {
|
|
|
|
return context.WithValue(context.Background(), oauth2.HTTPClient, client)
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 19:08:40 +00:00
|
|
|
// overrideCredentials sets the ClientID and ClientSecret from the
|
2016-01-04 15:13:36 +00:00
|
|
|
// config file if they are not blank.
|
|
|
|
// If any value is overridden, true is returned.
|
2017-01-16 15:10:08 +00:00
|
|
|
// the origConfig is copied
|
2018-05-14 17:06:57 +00:00
|
|
|
func overrideCredentials(name string, m configmap.Mapper, origConfig *oauth2.Config) (newConfig *oauth2.Config, changed bool) {
|
2018-01-12 16:30:54 +00:00
|
|
|
newConfig = new(oauth2.Config)
|
|
|
|
*newConfig = *origConfig
|
2017-01-16 15:10:08 +00:00
|
|
|
changed = false
|
2018-05-14 17:06:57 +00:00
|
|
|
ClientID, ok := m.Get(config.ConfigClientID)
|
|
|
|
if ok && ClientID != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
newConfig.ClientID = ClientID
|
2016-01-04 15:13:36 +00:00
|
|
|
changed = true
|
2015-09-16 19:08:40 +00:00
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
ClientSecret, ok := m.Get(config.ConfigClientSecret)
|
|
|
|
if ok && ClientSecret != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
newConfig.ClientSecret = ClientSecret
|
2016-01-04 15:13:36 +00:00
|
|
|
changed = true
|
2015-09-16 19:08:40 +00:00
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
AuthURL, ok := m.Get(config.ConfigAuthURL)
|
|
|
|
if ok && AuthURL != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
newConfig.Endpoint.AuthURL = AuthURL
|
2017-06-08 19:35:32 +00:00
|
|
|
changed = true
|
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
TokenURL, ok := m.Get(config.ConfigTokenURL)
|
|
|
|
if ok && TokenURL != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
newConfig.Endpoint.TokenURL = TokenURL
|
2017-06-08 19:35:32 +00:00
|
|
|
changed = true
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
return newConfig, changed
|
2015-09-16 19:08:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 09:07:03 +00: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.
|
2018-05-14 17:06:57 +00:00
|
|
|
func NewClientWithBaseClient(name string, m configmap.Mapper, config *oauth2.Config, baseClient *http.Client) (*http.Client, *TokenSource, error) {
|
|
|
|
config, _ = overrideCredentials(name, m, config)
|
|
|
|
token, err := GetToken(name, m)
|
2015-08-18 07:55:09 +00:00
|
|
|
if err != nil {
|
2016-05-23 17:03:22 +00:00
|
|
|
return nil, nil, err
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set our own http client in the context
|
2017-11-24 09:07:03 +00:00
|
|
|
ctx := Context(baseClient)
|
2015-08-18 07:55:09 +00:00
|
|
|
|
|
|
|
// Wrap the TokenSource in our TokenSource which saves changed
|
|
|
|
// tokens in the config file
|
2016-05-23 17:03:22 +00:00
|
|
|
ts := &TokenSource{
|
|
|
|
name: name,
|
2018-05-14 17:06:57 +00:00
|
|
|
m: m,
|
2016-05-23 17:03:22 +00:00
|
|
|
token: token,
|
|
|
|
config: config,
|
|
|
|
ctx: ctx,
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
2016-05-23 17:03:22 +00:00
|
|
|
return oauth2.NewClient(ctx, ts), ts, nil
|
2015-08-18 07:55:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-24 09:07:03 +00: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-05-14 17:06:57 +00:00
|
|
|
func NewClient(name string, m configmap.Mapper, oauthConfig *oauth2.Config) (*http.Client, *TokenSource, error) {
|
|
|
|
return NewClientWithBaseClient(name, m, oauthConfig, fshttp.NewClient(fs.Config))
|
2017-11-24 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// AuthResult is returned from the web server after authorization
|
|
|
|
// success or failure
|
|
|
|
type AuthResult struct {
|
|
|
|
OK bool // Failure or Success?
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Code string
|
|
|
|
HelpURL string
|
|
|
|
Form url.Values // the complete contents of the form
|
|
|
|
Err error // any underlying error to report
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error satisfies the error interface so AuthResult can be used as an error
|
|
|
|
func (ar *AuthResult) Error() string {
|
|
|
|
status := "Error"
|
|
|
|
if ar.OK {
|
|
|
|
status = "OK"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s: %s\nCode: %q\nDescription: %s\nHelp: %s",
|
|
|
|
status, ar.Name, ar.Code, ar.Description, ar.HelpURL)
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:55:09 +00:00
|
|
|
// Config does the initial creation of the token
|
2015-09-02 22:37:42 +00:00
|
|
|
//
|
2015-09-12 13:17:39 +00:00
|
|
|
// It may run an internal webserver to receive the results
|
2018-05-14 17:06:57 +00:00
|
|
|
func Config(id, name string, m configmap.Mapper, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error {
|
2019-08-30 10:52:03 +00:00
|
|
|
return doConfig(id, name, m, config, true, nil, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckAuthFn is called when a good Auth has been received
|
|
|
|
type CheckAuthFn func(*oauth2.Config, *AuthResult) error
|
|
|
|
|
|
|
|
// ConfigWithCallback does the initial creation of the token
|
|
|
|
//
|
|
|
|
// It may run an internal webserver to receive the results
|
|
|
|
//
|
|
|
|
// When the AuthResult is known the checkAuth function is called if set
|
|
|
|
func ConfigWithCallback(id, name string, m configmap.Mapper, config *oauth2.Config, checkAuth CheckAuthFn, opts ...oauth2.AuthCodeOption) error {
|
|
|
|
return doConfig(id, name, m, config, true, checkAuth, opts)
|
2017-06-14 15:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigNoOffline does the same as Config but does not pass the
|
|
|
|
// "access_type=offline" parameter.
|
2018-05-14 17:06:57 +00:00
|
|
|
func ConfigNoOffline(id, name string, m configmap.Mapper, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error {
|
2019-08-30 10:52:03 +00:00
|
|
|
return doConfig(id, name, m, config, false, nil, opts)
|
2018-06-01 10:34:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
func doConfig(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, offline bool, checkAuth CheckAuthFn, opts []oauth2.AuthCodeOption) error {
|
2018-05-14 17:06:57 +00:00
|
|
|
oauthConfig, changed := overrideCredentials(name, m, oauthConfig)
|
2019-01-17 15:01:13 +00:00
|
|
|
authorizeOnlyValue, ok := m.Get(config.ConfigAuthorize)
|
|
|
|
authorizeOnly := ok && authorizeOnlyValue != "" // set if being run by "rclone authorize"
|
2019-10-26 19:19:22 +00:00
|
|
|
authorizeNoAutoBrowserValue, ok := m.Get(config.ConfigAuthNoBrowser)
|
|
|
|
authorizeNoAutoBrowser := ok && authorizeNoAutoBrowserValue != ""
|
2016-01-04 15:13:36 +00:00
|
|
|
|
2015-08-18 07:55:09 +00:00
|
|
|
// See if already have a token
|
2018-05-14 17:06:57 +00:00
|
|
|
tokenString, ok := m.Get("token")
|
|
|
|
if ok && tokenString != "" {
|
2015-08-18 07:55:09 +00:00
|
|
|
fmt.Printf("Already have a token - refresh?\n")
|
2019-01-17 15:01:13 +00:00
|
|
|
if !config.ConfirmWithConfig(m, "config_refresh_token", true) {
|
2015-08-18 07:55:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:01:13 +00:00
|
|
|
// Ask the user whether they are using a local machine
|
|
|
|
isLocal := func() bool {
|
|
|
|
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")
|
|
|
|
return config.ConfirmWithConfig(m, "config_is_local", true)
|
|
|
|
}
|
|
|
|
|
2015-09-12 13:17:39 +00:00
|
|
|
// Detect whether we should use internal web server
|
|
|
|
useWebServer := false
|
2018-01-18 11:18:35 +00:00
|
|
|
switch oauthConfig.RedirectURL {
|
2019-08-30 10:52:03 +00:00
|
|
|
case TitleBarRedirectURL:
|
|
|
|
useWebServer = authorizeOnly
|
|
|
|
if !authorizeOnly {
|
|
|
|
useWebServer = isLocal()
|
|
|
|
}
|
|
|
|
if useWebServer {
|
|
|
|
// copy the config and set to use the internal webserver
|
|
|
|
configCopy := *oauthConfig
|
|
|
|
oauthConfig = &configCopy
|
|
|
|
oauthConfig.RedirectURL = RedirectURL
|
|
|
|
}
|
|
|
|
default:
|
2018-05-13 16:28:09 +00:00
|
|
|
if changed {
|
|
|
|
fmt.Printf("Make sure your Redirect URL is set to %q in your custom config.\n", oauthConfig.RedirectURL)
|
|
|
|
}
|
2015-09-12 13:17:39 +00:00
|
|
|
useWebServer = true
|
2019-01-17 15:01:13 +00:00
|
|
|
if authorizeOnly {
|
2016-01-04 15:13:36 +00:00
|
|
|
break
|
|
|
|
}
|
2019-01-17 15:01:13 +00:00
|
|
|
if !isLocal() {
|
2016-01-04 15:13:36 +00:00
|
|
|
fmt.Printf("For this to work, you will need rclone available on a machine that has a web browser available.\n")
|
2019-08-10 16:31:08 +00:00
|
|
|
fmt.Printf("Execute the following on your machine (same rclone version recommended) :\n")
|
2016-01-04 15:13:36 +00:00
|
|
|
if changed {
|
2020-02-13 10:18:23 +00:00
|
|
|
fmt.Printf("\trclone authorize %q -- %q %q\n", id, oauthConfig.ClientID, oauthConfig.ClientSecret)
|
2016-01-04 15:13:36 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf("\trclone authorize %q\n", id)
|
|
|
|
}
|
|
|
|
fmt.Println("Then paste the result below:")
|
2019-08-30 10:52:03 +00:00
|
|
|
code := config.ReadNonEmptyLine("result> ")
|
2016-01-04 15:13:36 +00:00
|
|
|
token := &oauth2.Token{}
|
|
|
|
err := json.Unmarshal([]byte(code), token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-28 09:53:53 +00:00
|
|
|
return PutToken(name, m, token, true)
|
2016-01-04 15:13:36 +00:00
|
|
|
}
|
2015-09-12 13:17:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 22:37:42 +00:00
|
|
|
// Make random state
|
2019-08-30 10:52:03 +00:00
|
|
|
state, err := random.Password(128)
|
2015-09-02 22:37:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-30 10:52:03 +00:00
|
|
|
|
|
|
|
// Generate oauth URL
|
2017-06-14 15:46:46 +00:00
|
|
|
if offline {
|
|
|
|
opts = append(opts, oauth2.AccessTypeOffline)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
authURL := oauthConfig.AuthCodeURL(state, opts...)
|
2015-09-02 22:37:42 +00:00
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// Prepare webserver if needed
|
|
|
|
var server *authServer
|
2015-09-12 13:17:39 +00:00
|
|
|
if useWebServer {
|
2019-08-30 10:52:03 +00:00
|
|
|
server = newAuthServer(bindAddress, state, authURL)
|
2019-09-27 13:59:56 +00:00
|
|
|
err := server.Init()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to start auth webserver")
|
|
|
|
}
|
|
|
|
go server.Serve()
|
2015-09-02 22:37:42 +00:00
|
|
|
defer server.Stop()
|
2019-09-27 13:59:56 +00:00
|
|
|
authURL = "http://" + bindAddress + "/auth?state=" + state
|
2015-09-02 22:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 19:19:22 +00:00
|
|
|
if !authorizeNoAutoBrowser && oauthConfig.RedirectURL != TitleBarRedirectURL {
|
|
|
|
// Open the URL for the user to visit
|
|
|
|
_ = open.Start(authURL)
|
|
|
|
fmt.Printf("If your browser doesn't open automatically go to the following link: %s\n", authURL)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Please go to the following link: %s\n", authURL)
|
|
|
|
}
|
2015-09-11 12:26:51 +00:00
|
|
|
fmt.Printf("Log in and authorize rclone for access\n")
|
2015-08-18 07:55:09 +00:00
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// Read the code via the webserver or manually
|
|
|
|
var auth *AuthResult
|
2015-09-12 13:17:39 +00:00
|
|
|
if useWebServer {
|
2015-09-11 12:26:51 +00:00
|
|
|
fmt.Printf("Waiting for code...\n")
|
2019-08-30 10:52:03 +00:00
|
|
|
auth = <-server.result
|
|
|
|
if !auth.OK || auth.Code == "" {
|
|
|
|
return auth
|
|
|
|
}
|
|
|
|
fmt.Printf("Got code\n")
|
|
|
|
if checkAuth != nil {
|
|
|
|
err = checkAuth(oauthConfig, auth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-06-01 10:34:35 +00:00
|
|
|
}
|
2015-09-11 12:26:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-30 10:52:03 +00:00
|
|
|
auth = &AuthResult{
|
|
|
|
Code: config.ReadNonEmptyLine("Enter verification code> "),
|
|
|
|
}
|
2015-09-11 12:26:51 +00:00
|
|
|
}
|
2019-08-30 10:52:03 +00:00
|
|
|
|
|
|
|
// Exchange the code for a token
|
2020-03-22 12:28:19 +00:00
|
|
|
ctx := Context(fshttp.NewClient(fs.Config))
|
|
|
|
token, err := oauthConfig.Exchange(ctx, auth.Code)
|
2015-08-18 07:55:09 +00:00
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.Wrap(err, "failed to get token")
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
2016-01-04 15:13:36 +00:00
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// Print code if we are doing a manual auth
|
2019-01-17 15:01:13 +00:00
|
|
|
if authorizeOnly {
|
2016-01-04 15:13:36 +00:00
|
|
|
result, err := json.Marshal(token)
|
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.Wrap(err, "failed to marshal token")
|
2016-01-04 15:13:36 +00:00
|
|
|
}
|
2019-01-17 15:01:13 +00:00
|
|
|
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", result)
|
2016-01-04 15:13:36 +00:00
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
return PutToken(name, m, token, true)
|
2015-08-18 07:55:09 +00:00
|
|
|
}
|
2015-09-02 22:37:42 +00:00
|
|
|
|
|
|
|
// Local web server for collecting auth
|
|
|
|
type authServer struct {
|
2019-08-30 10:52:03 +00:00
|
|
|
state string
|
|
|
|
listener net.Listener
|
|
|
|
bindAddress string
|
|
|
|
authURL string
|
|
|
|
server *http.Server
|
|
|
|
result chan *AuthResult
|
2018-06-01 10:34:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// newAuthServer makes the webserver for collecting auth
|
|
|
|
func newAuthServer(bindAddress, state, authURL string) *authServer {
|
|
|
|
return &authServer{
|
|
|
|
state: state,
|
|
|
|
bindAddress: bindAddress,
|
|
|
|
authURL: authURL, // http://host/auth redirects to here
|
|
|
|
result: make(chan *AuthResult, 1),
|
|
|
|
}
|
2015-09-02 22:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 10:52:03 +00:00
|
|
|
// Receive the auth request
|
|
|
|
func (s *authServer) handleAuth(w http.ResponseWriter, req *http.Request) {
|
|
|
|
fs.Debugf(nil, "Received %s request on auth server to %q", req.Method, req.URL.Path)
|
|
|
|
|
|
|
|
// Reply with the response to the user and to the channel
|
|
|
|
reply := func(status int, res *AuthResult) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
var t = template.Must(template.New("authResponse").Parse(AuthResponseTemplate))
|
|
|
|
if err := t.Execute(w, res); err != nil {
|
|
|
|
fs.Debugf(nil, "Could not execute template for web response.")
|
|
|
|
}
|
|
|
|
s.result <- res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the form parameters and save them
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
reply(http.StatusBadRequest, &AuthResult{
|
|
|
|
Name: "Parse form error",
|
|
|
|
Description: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get code, error if empty
|
|
|
|
code := req.Form.Get("code")
|
|
|
|
if code == "" {
|
|
|
|
reply(http.StatusBadRequest, &AuthResult{
|
|
|
|
Name: "Auth Error",
|
|
|
|
Description: "No code returned by remote server",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check state
|
|
|
|
state := req.Form.Get("state")
|
|
|
|
if state != s.state {
|
|
|
|
reply(http.StatusBadRequest, &AuthResult{
|
|
|
|
Name: "Auth state doesn't match",
|
|
|
|
Description: fmt.Sprintf("Expecting %q got %q", s.state, state),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// code OK
|
|
|
|
reply(http.StatusOK, &AuthResult{
|
|
|
|
OK: true,
|
|
|
|
Code: code,
|
|
|
|
Form: req.Form,
|
|
|
|
})
|
2018-06-03 21:49:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 13:59:56 +00:00
|
|
|
// Init gets the internal web server ready to receive config details
|
|
|
|
func (s *authServer) Init() error {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(nil, "Starting auth server on %s", s.bindAddress)
|
2015-09-02 22:37:42 +00:00
|
|
|
mux := http.NewServeMux()
|
2017-08-03 18:08:31 +00:00
|
|
|
s.server = &http.Server{
|
2015-09-02 22:37:42 +00:00
|
|
|
Addr: s.bindAddress,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2017-08-03 18:08:31 +00:00
|
|
|
s.server.SetKeepAlivesEnabled(false)
|
2019-08-30 10:52:03 +00:00
|
|
|
|
2015-09-02 22:37:42 +00:00
|
|
|
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
|
2019-08-30 10:52:03 +00:00
|
|
|
http.Error(w, "", http.StatusNotFound)
|
2015-09-02 22:37:42 +00:00
|
|
|
return
|
|
|
|
})
|
2015-09-11 12:26:51 +00:00
|
|
|
mux.HandleFunc("/auth", func(w http.ResponseWriter, req *http.Request) {
|
2019-09-27 13:59:56 +00:00
|
|
|
state := req.FormValue("state")
|
|
|
|
if state != s.state {
|
|
|
|
fs.Debugf(nil, "State did not match: want %q got %q", s.state, state)
|
2019-08-30 10:52:03 +00:00
|
|
|
http.Error(w, "State did not match - please try again", http.StatusForbidden)
|
2019-09-27 13:59:56 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-23 13:00:43 +00:00
|
|
|
http.Redirect(w, req, s.authURL, http.StatusTemporaryRedirect)
|
2015-09-11 12:26:51 +00:00
|
|
|
return
|
|
|
|
})
|
2019-08-30 10:52:03 +00:00
|
|
|
mux.HandleFunc("/", s.handleAuth)
|
2015-09-02 22:37:42 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
s.listener, err = net.Listen("tcp", s.bindAddress)
|
|
|
|
if err != nil {
|
2019-09-27 13:59:56 +00:00
|
|
|
return err
|
2015-09-02 22:37:42 +00:00
|
|
|
}
|
2019-09-27 13:59:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the auth server, doesn't return
|
|
|
|
func (s *authServer) Serve() {
|
|
|
|
err := s.server.Serve(s.listener)
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(nil, "Closed auth server with error: %v", err)
|
2015-09-02 22:37:42 +00:00
|
|
|
}
|
2019-04-15 19:03:33 +00:00
|
|
|
|
2019-09-27 13:59:56 +00:00
|
|
|
// Stop the auth server by closing its socket
|
2019-04-15 19:03:33 +00:00
|
|
|
func (s *authServer) Stop() {
|
|
|
|
fs.Debugf(nil, "Closing auth server")
|
2019-08-30 10:52:03 +00:00
|
|
|
close(s.result)
|
2019-04-15 19:03:33 +00:00
|
|
|
_ = s.listener.Close()
|
|
|
|
|
|
|
|
// close the server
|
|
|
|
_ = s.server.Close()
|
|
|
|
}
|