From 26276ed7193688f7ca2b27a298f13be9e1fa6fa5 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 8 Dec 2023 16:45:13 +0100 Subject: [PATCH] chore: guard against initial token being nil --- util/oauth/tokensource.go | 5 +++++ vehicle/tronity.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/util/oauth/tokensource.go b/util/oauth/tokensource.go index 5282852d94..f5d91fbf2e 100644 --- a/util/oauth/tokensource.go +++ b/util/oauth/tokensource.go @@ -19,6 +19,11 @@ type TokenSource struct { } func RefreshTokenSource(token *oauth2.Token, refresher TokenRefresher) oauth2.TokenSource { + if token == nil { + // allocate an (expired) token or mergeToken will fail + token = new(oauth2.Token) + } + ts := &TokenSource{ token: token, refresher: refresher, diff --git a/vehicle/tronity.go b/vehicle/tronity.go index 72ed1cfedd..638ee29932 100644 --- a/vehicle/tronity.go +++ b/vehicle/tronity.go @@ -94,7 +94,7 @@ func NewTronityFromConfig(other map[string]interface{}) (api.Vehicle, error) { // https://app.platform.tronity.io/docs#tag/Authentication if err := cc.Tokens.Error(); err != nil { // use app flow if we don't have tokens - ts = oauth.RefreshTokenSource(&oauth2.Token{}, v) + ts = oauth.RefreshTokenSource(nil, v) } else { // use provided tokens generated by code flow ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))