Fix token refresh (#764)
This commit is contained in:
parent
6e875d2f2e
commit
57495de286
5 changed files with 141 additions and 4 deletions
|
|
@ -17,11 +17,11 @@ func (t *Token) UnmarshalJSON(data []byte) error {
|
|||
var s struct {
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
IDToken string
|
||||
}
|
||||
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err == nil {
|
||||
t.TokenType = "bearer"
|
||||
t.AccessToken = s.AccessToken
|
||||
t.RefreshToken = s.RefreshToken
|
||||
t.Expiry = time.Now().Add(time.Hour)
|
||||
|
|
|
|||
31
vehicle/id/token_test.go
Normal file
31
vehicle/id/token_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package id
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
var tok Token
|
||||
str := `{"accesstoken":"access","refreshtoken":"refresh"}`
|
||||
|
||||
if err := json.Unmarshal([]byte(str), &tok); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if tok.AccessToken != "access" {
|
||||
t.Error("AccessToken")
|
||||
}
|
||||
|
||||
if tok.RefreshToken != "refresh" {
|
||||
t.Error("RefreshToken")
|
||||
}
|
||||
|
||||
if tok.TokenType != "bearer" {
|
||||
t.Error("TokenType")
|
||||
}
|
||||
|
||||
if tok.Expiry.IsZero() {
|
||||
t.Error("Expiry")
|
||||
}
|
||||
}
|
||||
31
vehicle/oidc/oidc_test.go
Normal file
31
vehicle/oidc/oidc_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package oidc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
var tok Token
|
||||
str := `{"access_token":"access","refresh_token":"refresh","token_type":"bearer","expires_in":3600}`
|
||||
|
||||
if err := json.Unmarshal([]byte(str), &tok); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if tok.AccessToken != "access" {
|
||||
t.Error("AccessToken")
|
||||
}
|
||||
|
||||
if tok.RefreshToken != "refresh" {
|
||||
t.Error("RefreshToken")
|
||||
}
|
||||
|
||||
if tok.TokenType != "bearer" {
|
||||
t.Error("TokenType")
|
||||
}
|
||||
|
||||
if tok.Expiry.IsZero() {
|
||||
t.Error("Expiry")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package vw
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
|
@ -9,11 +10,23 @@ import (
|
|||
"github.com/andig/evcc/util"
|
||||
"github.com/andig/evcc/util/request"
|
||||
"github.com/andig/evcc/vehicle/oidc"
|
||||
"github.com/imdario/mergo"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// Token is the VW token
|
||||
type Token oidc.Token
|
||||
type Token oauth2.Token
|
||||
|
||||
func (t *Token) UnmarshalJSON(data []byte) error {
|
||||
var o oidc.Token
|
||||
|
||||
err := json.Unmarshal(data, &o)
|
||||
if err == nil {
|
||||
*t = (Token)(o.Token)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Token) TokenSource(log *util.Logger, clientID string) oauth2.TokenSource {
|
||||
return &TokenSource{
|
||||
|
|
@ -35,7 +48,7 @@ func (ts *TokenSource) Token() (*oauth2.Token, error) {
|
|||
err = ts.refreshToken()
|
||||
}
|
||||
|
||||
return &ts.token.Token, err
|
||||
return (*oauth2.Token)(ts.token), err
|
||||
}
|
||||
|
||||
func (ts *TokenSource) refreshToken() error {
|
||||
|
|
@ -53,9 +66,15 @@ func (ts *TokenSource) refreshToken() error {
|
|||
if err == nil {
|
||||
var token Token
|
||||
if err = ts.DoJSON(req, &token); err == nil {
|
||||
ts.token = &token
|
||||
ts.mergeToken(token)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ts *TokenSource) mergeToken(t Token) {
|
||||
if err := mergo.Merge(ts.token, &t, mergo.WithOverride); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
56
vehicle/vw/token_test.go
Normal file
56
vehicle/vw/token_test.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package vw
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
var tok Token
|
||||
str := `{"access_token":"access","refresh_token":"refresh","token_type":"bearer","expires_in":3600}`
|
||||
|
||||
if err := json.Unmarshal([]byte(str), &tok); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if tok.AccessToken != "access" {
|
||||
t.Error("AccessToken")
|
||||
}
|
||||
|
||||
if tok.RefreshToken != "refresh" {
|
||||
t.Error("RefreshToken")
|
||||
}
|
||||
|
||||
if tok.TokenType != "bearer" {
|
||||
t.Error("TokenType")
|
||||
}
|
||||
|
||||
if tok.Expiry.IsZero() {
|
||||
t.Error("Expiry")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
ts := &TokenSource{
|
||||
token: &Token{
|
||||
AccessToken: "access1",
|
||||
RefreshToken: "refresh1",
|
||||
},
|
||||
}
|
||||
|
||||
new := Token{
|
||||
AccessToken: "access2",
|
||||
}
|
||||
|
||||
ts.mergeToken(new)
|
||||
|
||||
tok := ts.token
|
||||
|
||||
if tok.AccessToken != "access2" {
|
||||
t.Error("AccessToken")
|
||||
}
|
||||
|
||||
if tok.RefreshToken != "refresh1" {
|
||||
t.Error("RefreshToken")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue