Cookies for Import-Export

This commit is contained in:
Michal Horejsek 2020-09-09 09:09:35 +02:00
parent 2e439e17cf
commit de58c7a905
3 changed files with 26 additions and 1 deletions

View File

@ -3,7 +3,13 @@
Changelog [format](http://keepachangelog.com/en/1.0.0/)
## Unreleased
### Added
* GODT-682 Persistent anonymous API cookies for Import-Export.
### Changed
* GODT-511 User agent format changed.
### Removed
* GODT-519 Unused AUTH scope parsing methods

View File

@ -21,9 +21,11 @@ import (
"runtime/pprof"
"github.com/ProtonMail/proton-bridge/internal/cmd"
"github.com/ProtonMail/proton-bridge/internal/cookies"
"github.com/ProtonMail/proton-bridge/internal/events"
"github.com/ProtonMail/proton-bridge/internal/frontend"
"github.com/ProtonMail/proton-bridge/internal/importexport"
"github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/internal/updates"
"github.com/ProtonMail/proton-bridge/internal/users/credentials"
"github.com/ProtonMail/proton-bridge/pkg/config"
@ -36,6 +38,10 @@ import (
)
const (
// cacheVersion is used for cache files such as lock, or preferences.
// Different number will drop old files and create new ones.
cacheVersion = "c11"
appName = "importExport"
appNameDash = "import-export-app"
)
@ -58,7 +64,7 @@ func main() {
// IMPORTANT: ***Read the comments before CHANGING the order ***
func run(context *cli.Context) (contextError error) { // nolint[funlen]
// We need to have config instance to setup a logs, panic handler, etc ...
cfg := config.New(appName, constants.Version, constants.Revision, "")
cfg := config.New(appName, constants.Version, constants.Revision, cacheVersion)
// We want to know about any problem. Our PanicHandler calls sentry which is
// not dependent on anything else. If that fails, it tries to create crash
@ -132,6 +138,16 @@ func run(context *cli.Context) (contextError error) { // nolint[funlen]
// implementation depending on whether build flag pmapi_prod is used or not.
cm.SetRoundTripper(cfg.GetRoundTripper(cm, eventListener))
pref := preferences.New(cfg)
// Cookies must be persisted across restarts.
jar, err := cookies.NewCookieJar(pref)
if err != nil {
logrus.WithError(err).Warn("Could not create cookie jar")
} else {
cm.SetCookieJar(jar)
}
importexportInstance := importexport.New(cfg, panicHandler, eventListener, cm, credentialsStore)
// Decide about frontend mode before initializing rest of import-export.

View File

@ -266,6 +266,9 @@ func (c *client) doBuffered(req *http.Request, bodyBuffer []byte, retryUnauthori
return
}
// Cookies are returned only after request was sent.
c.log.Tracef("REQCOOKIES '%v'", req.Cookies())
resDate := res.Header.Get("Date")
if resDate != "" {
if serverTime, err := http.ParseTime(resDate); err == nil {