Nissan: use OAuth2 for code exchange and token refresh (#3302)
This commit is contained in:
parent
c3e841f000
commit
ce5364346d
1 changed files with 17 additions and 36 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package nissan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -8,7 +9,6 @@ import (
|
|||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/oauth"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
|
@ -115,45 +115,26 @@ func (v *Identity) Login(user, password string) error {
|
|||
}
|
||||
}
|
||||
|
||||
var token oauth.Token
|
||||
if err == nil {
|
||||
data := url.Values{
|
||||
"code": {code},
|
||||
"client_id": {ClientID},
|
||||
"client_secret": {ClientSecret},
|
||||
"redirect_uri": {RedirectURI},
|
||||
"grant_type": {"authorization_code"},
|
||||
}
|
||||
|
||||
uri = fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthURL, realm, data.Encode())
|
||||
req, err = request.New(http.MethodPost, uri, nil, request.URLEncoding)
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, &token)
|
||||
}
|
||||
uri = fmt.Sprintf("%s/oauth2/%s/access_token", AuthURL, realm)
|
||||
config := oauth2.Config{
|
||||
ClientID: ClientID,
|
||||
ClientSecret: ClientSecret,
|
||||
RedirectURL: RedirectURI,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: uri,
|
||||
TokenURL: uri,
|
||||
AuthStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, v.Client)
|
||||
|
||||
var token *oauth2.Token
|
||||
if err == nil {
|
||||
v.TokenSource = oauth.RefreshTokenSource((*oauth2.Token)(&token), v)
|
||||
token, err = config.Exchange(ctx, code)
|
||||
}
|
||||
|
||||
v.TokenSource = config.TokenSource(ctx, token)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) {
|
||||
data := url.Values{
|
||||
"client_id": {ClientID},
|
||||
"client_secret": {ClientSecret},
|
||||
"grant_type": {"refresh_token"},
|
||||
"refresh_token": {token.RefreshToken},
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthURL, Realm, data.Encode())
|
||||
req, err := request.New(http.MethodPost, uri, nil, request.URLEncoding)
|
||||
|
||||
var res oauth.Token
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, &res)
|
||||
}
|
||||
|
||||
return (*oauth2.Token)(&res), err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue