VW: update api (#19496)

This commit is contained in:
andig 2025-03-06 18:43:47 +01:00 • committed by GitHub
parent 7227eaeab7
commit 6aac2dcb85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 15 deletions

View file

@ -3,7 +3,6 @@ package vwidentity
import (
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strings"
@ -72,27 +71,26 @@ type CredentialParams struct {
}
func ParseCredentialsPage(r io.ReadCloser) (CredentialParams, error) {
var res CredentialParams
buf := new(strings.Builder)
if _, err := io.Copy(buf, r); err != nil {
return res, err
b, err := io.ReadAll(r)
if err != nil {
return CredentialParams{}, err
}
re := regexp.MustCompile(`(?s)window._IDK\s*=\s*(.*?)[;<]`)
match := re.FindAllStringSubmatch(buf.String(), -1)
return parseCredentials(string(b))
}
func parseCredentials(body string) (CredentialParams, error) {
// find js block
match := regexp.MustCompile(`(?s)window._IDK\s*=\s*(.*?)[;<]`).FindAllStringSubmatch(body, -1)
// clean quotes
tmpl := strings.ReplaceAll(match[0][1], `'`, `"`)
quotes1 := strings.ReplaceAll(match[0][1], `'`, `"`)
quotes2 := regexp.MustCompile(`\s(\w+)(?s)*:`).ReplaceAllString(quotes1, `" $1":`)
// strip , }
re2 := regexp.MustCompile(`(?s),\s+}`)
tmpl = re2.ReplaceAllString(tmpl, "}")
for _, v := range []string{"templateModel", "disabledFeatures", "isFooterEnabled", "currentLocale", "csrf_parameterName", "csrf_token", "userSession", "userId", "countryOfResidence", "baseUrl", "consentBaseUrl", "isRTLEnabled"} {
tmpl = strings.Replace(tmpl, v, fmt.Sprintf(`"%s"`, v), 1)
}
tmpl := regexp.MustCompile(`(?s),\s+}`).ReplaceAllString(quotes2, "}")
var res CredentialParams
err := json.Unmarshal([]byte(tmpl), &res)
return res, err

View file

@ -0,0 +1,31 @@
package vwidentity
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
b := `<script>
window._IDK = {
templateModel: {"clientLegalEntityModel":{"clientId":"foo@apps_vw-dilab_com","clientAppName":"myAudi App","clientAppDisplayName":"myAudi App","legalEntityInfo":{"name":"Audi","shortName":"AUDI","productName":"Audi ID","theme":"audi","defaultLanguage":"de","termAndConditionsType":"DEFAULT","legalProperties":{"revokeDataContact":"","imprintText":"IMPRINT","countryOfJurisdiction":"DE"}},"informalLanguage":false,"legalEntityCode":"audi","imprintTextKey":"imprint.link.text"},"template":"loginAuthenticate","hmac":"16563a265369fbc1ef1ba126411349a430b1d8b5c1592b6284216dc634169ca3","useClientRendering":true,"titleKey":"title.login.password","title":null,"emailPasswordForm":{"@class":"com.volkswagen.identitykit.signin.domain.model.dto.EmailPasswordForm","email":"foo@bar","password":null},"error":null,"relayState":"be82f61e811860f3b4d8a78365253705bbdeaed4","nextButtonDisabled":false,"enableNextButtonAfterSeconds":0,"postAction":"login/authenticate","identifierUrl":"login/identifier"},
disabledFeatures: {
isRTLEnabled: false,
},
currentLocale: 'de-DE',
csrf_parameterName: '_csrf',
csrf_token: 'pwAX6CTKoSQWsej-9sROQ5LPw8Qy0gCT9oSdFxFxo9VkyNeYxjMg2kevkhY70Iyawul6e_D27qYK5za-leCoJXMSxu1R8eep',
userSession: {
userId: '',
countryOfResidence: ''
},
baseUrl: 'https://identity.vwgroup.io',
consentBaseUrl: 'http://consentapi-service.idk-peu.svc.cluster.local:8080',
cancelContractUrl: ''
}
</script>`
_, err := parseCredentials(b)
require.NoError(t, err)
}