Skip to content

Commit

Permalink
authorize: refactor to use new config interfaces rclone#5178
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Apr 8, 2021
1 parent 1a41c93 commit b9fd020
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
44 changes: 30 additions & 14 deletions fs/config/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package config

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
)

// Authorize is for remote authorization of headless machines.
Expand All @@ -20,29 +22,43 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
default:
return errors.Errorf("invalid number of arguments: %d", len(args))
}
newType := args[0]
f := fs.MustFind(newType)
if f.Config == nil {
return errors.Errorf("can't authorize fs %q", newType)
Type := args[0] // FIXME could read this from input
ri, err := fs.Find(Type)
if err != nil {
return err
}
if ri.Config == nil {
return errors.Errorf("can't authorize fs %q", Type)
}
// Name used for temporary fs
name := "**temp-fs**"

// Make sure we delete it
defer DeleteRemote(name)
// Config map for remote
inM := configmap.Simple{}

// Indicate that we are running rclone authorize
Data.SetValue(name, ConfigAuthorize, "true")
inM[ConfigAuthorize] = "true"
if noAutoBrowser {
Data.SetValue(name, ConfigAuthNoBrowser, "true")
inM[ConfigAuthNoBrowser] = "true"
}

if len(args) == 3 {
Data.SetValue(name, ConfigClientID, args[1])
Data.SetValue(name, ConfigClientSecret, args[2])
inM[ConfigClientID] = args[1]
inM[ConfigClientSecret] = args[2]
}

m := fs.ConfigMap(f, name, nil)
f.Config(ctx, name, m)
// Name used for temporary remote
name := "**temp-fs**"

m := fs.ConfigMap(ri, name, inM)
outM := configmap.Simple{}
m.ClearSetters()
m.AddSetter(outM)

ri.Config(ctx, name, m)

// Print code if we are doing a manual auth
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", outM["token"])

fs.Debugf(nil, "Set parameters %q", outM)

return nil
}
8 changes: 0 additions & 8 deletions lib/oauthutil/oauthutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,6 @@ version recommended):
return errors.Wrap(err, "failed to get token")
}

// Print code if we are doing a manual auth
if authorizeOnly {
result, err := json.Marshal(token)
if err != nil {
return errors.Wrap(err, "failed to marshal token")
}
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", result)
}
return PutToken(name, m, token, true)
}

Expand Down

0 comments on commit b9fd020

Please sign in to comment.