Skip to content

Commit

Permalink
Porsche: add token refresh (#8695)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jun 30, 2023
1 parent 612cc8a commit 672479b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions vehicle/porsche/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/evcc-io/evcc/util"
Expand Down Expand Up @@ -46,13 +47,15 @@ var (

// Identity is the Porsche Identity client
type Identity struct {
log *util.Logger
*request.Helper
oauth2.TokenSource
}

// NewIdentity creates Porsche identity
func NewIdentity(log *util.Logger) *Identity {
v := &Identity{
log: log,
Helper: request.NewHelper(log),
}

Expand All @@ -76,8 +79,7 @@ func (v *Identity) Login(oc *oauth2.Config, user, password string) error {
v.Client.Jar, _ = cookiejar.New(nil)
v.Client.CheckRedirect = request.DontFollow
defer func() {
// keep cookies around
// v.Client.Jar = nil
v.Client.Jar = nil
v.Client.CheckRedirect = nil
}()

Expand Down Expand Up @@ -165,5 +167,15 @@ func (v *Identity) Login(oc *oauth2.Config, user, password string) error {

v.TokenSource = oc.TokenSource(cctx, token)

go v.refresh()

return nil
}

func (v *Identity) refresh() {
for range time.Tick(6 * time.Hour) {
if _, err := v.Token(); err != nil {
v.log.ERROR.Printf("token refresh: %v", err)
}
}
}

0 comments on commit 672479b

Please sign in to comment.