BMW: improve regions abstraction

This commit is contained in:
andig 2023-09-17 15:35:04 +02:00
parent 0827c1dd1c
commit 5ec07885a6
2 changed files with 7 additions and 9 deletions

View file

@ -23,14 +23,14 @@ const (
type Identity struct {
*request.Helper
region string
region Region
}
// NewIdentity creates BMW identity
func NewIdentity(log *util.Logger, region string) *Identity {
v := &Identity{
Helper: request.NewHelper(log),
region: strings.ToUpper(region),
region: regions[strings.ToUpper(region)],
}
return v
@ -53,10 +53,10 @@ func (v *Identity) Login(user, password string) (oauth2.TokenSource, error) {
}
data := url.Values{
"client_id": {regions[v.region].ClientID},
"client_id": {v.region.ClientID},
"response_type": {"code"},
"redirect_uri": {RedirectURI},
"state": {regions[v.region].State},
"state": {v.region.State},
"scope": {"openid profile email offline_access smacc vehicle_data perseus dlm svds cesim vsapi remote_services fupo authenticate_user"},
"nonce": {"login_nonce"},
"code_challenge_method": {"S256"},
@ -66,7 +66,7 @@ func (v *Identity) Login(user, password string) (oauth2.TokenSource, error) {
"grant_type": {"authorization_code"},
}
uri := fmt.Sprintf("%s/oauth/authenticate", regions[v.region].AuthURI)
uri := fmt.Sprintf("%s/oauth/authenticate", v.region.AuthURI)
req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), request.URLEncoding)
if err != nil {
return nil, err
@ -134,10 +134,10 @@ func (v *Identity) Login(user, password string) (oauth2.TokenSource, error) {
}
func (v *Identity) retrieveToken(data url.Values) (*oauth2.Token, error) {
uri := fmt.Sprintf("%s/oauth/token", regions[v.region].AuthURI)
uri := fmt.Sprintf("%s/oauth/token", v.region.AuthURI)
req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), map[string]string{
"Content-Type": request.FormContent,
"Authorization": regions[v.region].Authorization,
"Authorization": v.region.Token.Authorization,
})
var tok oauth.Token

View file

@ -20,7 +20,6 @@ var regions = map[string]Region{
"https://cocoapi.bmwgroup.us",
Token{
Authorization: "Basic NTQzOTRhNGItYjZjMS00NWZlLWI3YjItOGZkM2FhOTI1M2FhOmQ5MmYzMWMwLWY1NzktNDRmNS1hNzdkLTk2NmY4ZjAwZTM1MQ==",
// Verifier: "KDarcVUpgymBDCgHDH0PwwMfzycDxu1joeklioOhwXA",
},
Authenticate{
ClientID: "54394a4b-b6c1-45fe-b7b2-8fd3aa9253aa",
@ -32,7 +31,6 @@ var regions = map[string]Region{
"https://cocoapi.bmwgroup.com",
Token{
Authorization: "Basic MzFjMzU3YTAtN2ExZC00NTkwLWFhOTktMzNiOTcyNDRkMDQ4OmMwZTMzOTNkLTcwYTItNGY2Zi05ZDNjLTg1MzBhZjY0ZDU1Mg==",
// Verifier: "7PsmfPS5MpaNt0jEcPpi-B7M7u0gs1Nzw6ex0Y9pa-0",
},
Authenticate{
ClientID: "31c357a0-7a1d-4590-aa99-33b97244d048",