chore: switch to ReuseTokenSource (#26206)

This commit is contained in:
andig 2025-12-27 15:44:31 +01:00 • committed by GitHub
parent 6a11785633
commit 19a30ac9ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 137 additions and 108 deletions

View file

@ -5,7 +5,6 @@ import (
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/oauth"
"github.com/evcc-io/evcc/util/request"
"golang.org/x/oauth2"
)
@ -15,10 +14,10 @@ type tokenSource struct {
}
func TokenSource(log *util.Logger, token *oauth2.Token) oauth2.TokenSource {
return oauth.RefreshTokenSource(token, &tokenSource{log})
return oauth2.ReuseTokenSource(token, &tokenSource{log})
}
func (ts *tokenSource) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (ts *tokenSource) Token() (*oauth2.Token, error) {
// "Content-Type: application/json" \
// --request POST \
// https://console.corrently.io/v2.0/auth/requestToken

View file

@ -79,13 +79,13 @@ func NewEdfTempoFromConfig(other map[string]any) (api.Tariff, error) {
t.Client.Transport = &oauth2.Transport{
Base: t.Client.Transport,
Source: oauth.RefreshTokenSource(new(oauth2.Token), t),
Source: oauth2.ReuseTokenSource(nil, oauth.BootstrapTokenSource(t.refreshToken)),
}
return runOrError(t)
}
func (t *EdfTempo) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (t *EdfTempo) refreshToken() (*oauth2.Token, error) {
tokenURL := "https://digital.iservices.rte-france.com/token/oauth"
req, _ := request.New(http.MethodPost, tokenURL, nil, map[string]string{
"Authorization": t.basic,

View file

@ -9,10 +9,10 @@ import (
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/oauth"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/hasura/go-graphql-client"
"golang.org/x/oauth2"
)
// BaseURI is Octopus Energy Germany's Kraken API root.
@ -28,7 +28,7 @@ type OctopusDeGraphQLClient struct {
// NewClient returns a new, authenticated instance of OctopusDeGraphQLClient.
func NewClient(log *util.Logger, email, password, accountNumber string) (*OctopusDeGraphQLClient, error) {
ts := oauth.RefreshTokenSource(nil, &TokenSource{
ts := oauth2.ReuseTokenSource(nil, &tokenSource{
log: log,
email: email,
password: password,

View file

@ -12,25 +12,27 @@ import (
"golang.org/x/oauth2"
)
type TokenSource struct {
type tokenSource struct {
log *util.Logger
email, password string
}
var _ oauth2.TokenSource = (*tokenSource)(nil)
// RefreshToken implements oauth.TokenRefresher to obtain a new JWT token.
// It parses the JWT to extract the actual expiry time from the token claims.
func (c *TokenSource) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (ts *tokenSource) Token() (*oauth2.Token, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Create a temporary client without authentication for the token request
cli := request.NewClient(c.log)
cli := request.NewClient(ts.log)
tempClient := graphql.NewClient(BaseURI, cli)
var q krakenTokenAuthentication
if err := tempClient.Mutate(ctx, &q, map[string]any{
"email": c.email,
"password": c.password,
"email": ts.email,
"password": ts.password,
}); err != nil {
return nil, fmt.Errorf("authentication failed: %w", err)
}

View file

@ -85,7 +85,7 @@ func NewOstromFromConfig(other map[string]any) (api.Tariff, error) {
t.Client.Transport = &oauth2.Transport{
Base: t.Client.Transport,
Source: oauth.RefreshTokenSource(nil, t),
Source: oauth2.ReuseTokenSource(nil, oauth.BootstrapTokenSource(t.refreshToken)),
}
contracts, err := t.getContracts()
@ -158,7 +158,7 @@ func (t *Ostrom) getFixedPrice() (float64, error) {
return 0, errors.New("tariff not found")
}
func (t *Ostrom) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (t *Ostrom) refreshToken() (*oauth2.Token, error) {
uri := ostrom.URI_AUTH + "/oauth2/token"
data := url.Values{"grant_type": {"client_credentials"}}
req, _ := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{