mirror of
https://github.com/rclone/rclone.git
synced 2025-01-11 00:40:03 +01:00
jottacloud: add support for 2-factor authentification fixes #2722
This commit is contained in:
parent
f799be1d6a
commit
291f270904
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@ -80,14 +81,11 @@ func init() {
|
|||||||
|
|
||||||
username, ok := m.Get(configUsername)
|
username, ok := m.Get(configUsername)
|
||||||
if !ok {
|
if !ok {
|
||||||
fs.Errorf(nil, "No username defined")
|
log.Fatalf("No username defined")
|
||||||
}
|
}
|
||||||
// Password
|
|
||||||
password := config.GetPassword("Your Jottacloud password is only required during config and will not be stored.")
|
password := config.GetPassword("Your Jottacloud password is only required during config and will not be stored.")
|
||||||
if password == "" {
|
|
||||||
fs.Errorf(nil, "Password must not be empty!")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// prepare out token request with username and password
|
||||||
srv := rest.NewClient(fshttp.NewClient(fs.Config))
|
srv := rest.NewClient(fshttp.NewClient(fs.Config))
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
values.Set("grant_type", "PASSWORD")
|
values.Set("grant_type", "PASSWORD")
|
||||||
@ -105,12 +103,21 @@ func init() {
|
|||||||
var jsonToken api.TokenJSON
|
var jsonToken api.TokenJSON
|
||||||
resp, err := srv.CallJSON(&opts, nil, &jsonToken)
|
resp, err := srv.CallJSON(&opts, nil, &jsonToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fs.Errorf(nil, "Failed to get resource token: %v", err)
|
// if 2fa is enabled the first request is expected to fail. we'lls do another request with the 2fa code as an additional http header
|
||||||
return
|
if resp != nil {
|
||||||
}
|
if resp.Header.Get("X-JottaCloud-OTP") == "required; SMS" {
|
||||||
if resp.StatusCode != 200 {
|
fmt.Printf("This account has 2 factor authentication enabled you will receive a verification code via SMS.\n")
|
||||||
fs.Errorf(nil, "Failed to get resource token: Got HTTP error code %d", resp.StatusCode)
|
fmt.Printf("Enter verification code> ")
|
||||||
return
|
authCode := config.ReadLine()
|
||||||
|
authCode = strings.Replace(authCode, "-", "", -1) // the sms received contains a pair of 3 digit numbers seperated by '-' but wants a single 6 digit number
|
||||||
|
opts.ExtraHeaders = make(map[string]string)
|
||||||
|
opts.ExtraHeaders["X-Jottacloud-Otp"] = authCode
|
||||||
|
resp, err = srv.CallJSON(&opts, nil, &jsonToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to get resource token: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var token oauth2.Token
|
var token oauth2.Token
|
||||||
@ -122,7 +129,7 @@ func init() {
|
|||||||
// finally save them in the config
|
// finally save them in the config
|
||||||
err = oauthutil.PutToken(name, m, &token, true)
|
err = oauthutil.PutToken(name, m, &token, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fs.Errorf(nil, "Error while setting token: %s", err)
|
log.Fatalf("Error while setting token: %s", err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Options: []fs.Option{{
|
Options: []fs.Option{{
|
||||||
@ -390,11 +397,10 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
f := &Fs{
|
f := &Fs{
|
||||||
name: name,
|
name: name,
|
||||||
root: root,
|
root: root,
|
||||||
user: opt.User,
|
user: opt.User,
|
||||||
opt: *opt,
|
opt: *opt,
|
||||||
//endpointURL: rest.URLPathEscape(path.Join(user, defaultDevice, opt.Mountpoint)),
|
|
||||||
srv: rest.NewClient(oAuthClient).SetRoot(rootURL),
|
srv: rest.NewClient(oAuthClient).SetRoot(rootURL),
|
||||||
apiSrv: rest.NewClient(oAuthClient).SetRoot(apiURL),
|
apiSrv: rest.NewClient(oAuthClient).SetRoot(apiURL),
|
||||||
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
||||||
|
Loading…
Reference in New Issue
Block a user