@@ -19,84 +19,86 @@ import (
1919)
2020
2121const (
22- // AuthRequestTimeout is the timeout for authentication-related API requests
22+ // AuthRequestTimeout is the timeout for authentication-related API requests.
2323 AuthRequestTimeout = 15 * time .Second
2424
25- // IV is the initialization vector for AES encryption
25+ // IV is the initialization vector for AES encryption.
2626 IV = "0102030405060708"
2727
28- // SignatureMD5 is used for key derivation
28+ // SignatureMD5 is used for key derivation.
2929 SignatureMD5 = "C383D8C4D279B78130AD52DC71D95CAA"
3030
31- // AppPackageID identifies the mobile app package
31+ // AppPackageID identifies the mobile app package.
3232 AppPackageID = "com.interrait.mymazda"
3333
34- // UserAgentBaseAPI is the User-Agent for base API requests
34+ // UserAgentBaseAPI is the User-Agent for base API requests.
3535 UserAgentBaseAPI = "MyMazda-Android/9.0.5"
3636
37- // UserAgentUsherAPI is the User-Agent for Usher API requests
37+ // UserAgentUsherAPI is the User-Agent for Usher API requests.
3838 UserAgentUsherAPI = "MyMazda/9.0.5 (Google Pixel 3a; Android 11)"
3939
40- // AppOS identifies the operating system
40+ // AppOS identifies the operating system.
4141 AppOS = "Android"
4242
43- // AppVersion is the mobile app version
43+ // AppVersion is the mobile app version.
4444 AppVersion = "9.0.5"
4545
46- // UsherSDKVersion is the Usher SDK version
46+ // UsherSDKVersion is the Usher SDK version.
4747 UsherSDKVersion = "11.3.0700.001"
4848
49- // InternalUserID is a placeholder used in API requests
49+ // InternalUserID is a placeholder used in API requests.
5050 InternalUserID = "__INTERNAL_ID__"
5151)
5252
53- // Authentication endpoint constants
53+ // Authentication endpoint constants.
5454const (
5555 EndpointCheckVersion = "service/checkVersion"
5656 EndpointEncryptionKey = "system/encryptionKey"
5757 EndpointLogin = "user/login"
5858)
5959
60- // Region represents a valid geographic region
60+ // Region represents a valid geographic region.
6161type Region string
6262
6363const (
64- // RegionMNAO represents Mazda North American Operations
64+ // RegionMNAO represents Mazda North American Operations.
6565 RegionMNAO Region = "MNAO"
66- // RegionMME represents Mazda Europe
66+ // RegionMME represents Mazda Europe.
6767 RegionMME Region = "MME"
68- // RegionMJO represents Mazda Japan
68+ // RegionMJO represents Mazda Japan.
6969 RegionMJO Region = "MJO"
7070)
7171
72- // String returns the string representation of the region
72+ // String returns the string representation of the region.
7373func (r Region ) String () string {
7474 return string (r )
7575}
7676
77- // IsValid checks if the region is valid
77+ // IsValid checks if the region is valid.
7878func (r Region ) IsValid () bool {
7979 _ , ok := RegionConfigs [string (r )]
80+
8081 return ok
8182}
8283
83- // ParseRegion parses a string into a Region, returning an error if invalid
84+ // ParseRegion parses a string into a Region, returning an error if invalid.
8485func ParseRegion (s string ) (Region , error ) {
8586 r := Region (s )
8687 if ! r .IsValid () {
8788 return "" , fmt .Errorf ("invalid region: %s (must be one of: MNAO, MME, MJO)" , s )
8889 }
90+
8991 return r , nil
9092}
9193
92- // RegionConfig holds configuration for a specific region
94+ // RegionConfig holds configuration for a specific region.
9395type RegionConfig struct {
9496 AppCode string
9597 BaseURL string
9698 UsherURL string
9799}
98100
99- // RegionConfigs maps region codes to their configurations
101+ // RegionConfigs maps region codes to their configurations.
100102var RegionConfigs = map [string ]RegionConfig {
101103 "MNAO" : {
102104 AppCode : "202007270941270111799" ,
@@ -115,7 +117,7 @@ var RegionConfigs = map[string]RegionConfig{
115117 },
116118}
117119
118- // Client represents an API client
120+ // Client represents an API client.
119121type Client struct {
120122 email string
121123 password string
@@ -138,7 +140,7 @@ type Client struct {
138140 sleepFunc func (context.Context , time.Duration ) error
139141}
140142
141- // NewClient creates a new API client
143+ // NewClient creates a new API client.
142144func NewClient (email , password string , region Region ) (* Client , error ) {
143145 if ! region .IsValid () {
144146 return nil , fmt .Errorf ("invalid region: %s" , region )
@@ -162,25 +164,25 @@ func NewClient(email, password string, region Region) (*Client, error) {
162164 }, nil
163165}
164166
165- // SetDebug enables or disables debug logging
167+ // SetDebug enables or disables debug logging.
166168func (c * Client ) SetDebug (debug bool ) {
167169 c .debug = debug
168170}
169171
170- // SetCachedCredentials sets the client's cached authentication credentials
172+ // SetCachedCredentials sets the client's cached authentication credentials.
171173func (c * Client ) SetCachedCredentials (accessToken string , accessTokenExpirationTs int64 , encKey , signKey string ) {
172174 c .accessToken = accessToken
173175 c .accessTokenExpirationTs = accessTokenExpirationTs
174176 c .Keys .EncKey = encKey
175177 c .Keys .SignKey = signKey
176178}
177179
178- // GetCredentials returns the current authentication credentials for caching
180+ // GetCredentials returns the current authentication credentials for caching.
179181func (c * Client ) GetCredentials () (accessToken string , accessTokenExpirationTs int64 , encKey , signKey string ) {
180182 return c .accessToken , c .accessTokenExpirationTs , c .Keys .EncKey , c .Keys .SignKey
181183}
182184
183- // GetEncryptionKeys retrieves the encryption and signing keys from the API
185+ // GetEncryptionKeys retrieves the encryption and signing keys from the API.
184186func (c * Client ) GetEncryptionKeys (ctx context.Context ) error {
185187 // Ensure we have a timeout for the request
186188 ctx , cancel := context .WithTimeout (ctx , AuthRequestTimeout )
@@ -245,7 +247,7 @@ func (c *Client) GetEncryptionKeys(ctx context.Context) error {
245247 return nil
246248}
247249
248- // GetUsherEncryptionKey retrieves the RSA public key from Usher API
250+ // GetUsherEncryptionKey retrieves the RSA public key from Usher API.
249251func (c * Client ) GetUsherEncryptionKey (ctx context.Context ) (string , string , error ) {
250252 // Ensure we have a timeout for the request
251253 ctx , cancel := context .WithTimeout (ctx , AuthRequestTimeout )
@@ -287,7 +289,7 @@ func (c *Client) GetUsherEncryptionKey(ctx context.Context) (string, string, err
287289 return response .Data .PublicKey , response .Data .VersionPrefix , nil
288290}
289291
290- // Login authenticates with the API and retrieves an access token
292+ // Login authenticates with the API and retrieves an access token.
291293func (c * Client ) Login (ctx context.Context ) error {
292294 // Ensure we have a timeout for the request
293295 ctx , cancel := context .WithTimeout (ctx , AuthRequestTimeout )
@@ -364,7 +366,7 @@ func (c *Client) Login(ctx context.Context) error {
364366 return nil
365367}
366368
367- // IsTokenValid checks if the access token is present and not expired
369+ // IsTokenValid checks if the access token is present and not expired.
368370func (c * Client ) IsTokenValid () bool {
369371 return cache .IsTokenValid (c .accessToken , c .accessTokenExpirationTs )
370372}
@@ -382,22 +384,25 @@ func (c *Client) getSignFromTimestamp(timestamp string) string {
382384
383385 timestampExtended := strings .ToUpper (timestamp + timestamp [6 :] + timestamp [3 :])
384386 temporarySignKey := c .getTemporarySignKeyFromAppCode ()
387+
385388 return SignWithSHA256 (timestampExtended + temporarySignKey )
386389}
387390
388391func (c * Client ) getTemporarySignKeyFromAppCode () string {
389392 val1 := SignWithMD5 (c .appCode + AppPackageID )
390393 val2 := strings .ToLower (SignWithMD5 (val1 + SignatureMD5 ))
394+
391395 return val2 [20 :32 ] + val2 [0 :10 ] + val2 [4 :6 ]
392396}
393397
394398func (c * Client ) getDecryptionKeyFromAppCode () string {
395399 val1 := SignWithMD5 (c .appCode + AppPackageID )
396400 val2 := strings .ToLower (SignWithMD5 (val1 + SignatureMD5 ))
401+
397402 return val2 [4 :20 ]
398403}
399404
400- // decryptCheckVersionPayload decrypts and parses the checkVersion response payload
405+ // decryptCheckVersionPayload decrypts and parses the checkVersion response payload.
401406func (c * Client ) decryptCheckVersionPayload (payload string ) (* CheckVersionResponse , error ) {
402407 key := c .getDecryptionKeyFromAppCode ()
403408 decrypted , err := DecryptAES128CBC (payload , key , IV )
0 commit comments