import requests, io, sys, base64, json, uuid, hashlib, time
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
TEN, CID = 'vin3s.au.auth0.com', 'jE5xt50qC7oIh1f32qMzA6hGznIU5mgH'
AUD, SC = 'https://mobile.connected-car.vinfast.vn', 'openid profile email offline_access'
EMAIL, PW = 'minhhai113he@gmail.com', 'Minhgiang007?'
BASE = AUD
UV = 'ccarusermgnt/api/v1/user-vehicle'

r = requests.post(f'https://{TEN}/oauth/token', json={
    'client_id': CID, 'grant_type': 'password', 'username': EMAIL, 'password': PW,
    'scope': SC, 'audience': AUD, 'realm': 'Username-Password-Authentication'}, timeout=20)
tok = r.json()['access_token']
pad = lambda s: s + '=' * (-len(s) % 4)
claims = json.loads(base64.urlsafe_b64decode(pad(tok.split('.')[1])))
sub = claims['sub']             # auth0|6879d2a767da4a6088ecf5a2
subnum = sub.split('|')[-1]
import hashlib
email_md5 = hashlib.md5(EMAIL.encode()).hexdigest()

def hdr(extra, ver='2.26.11'):
    h = {
        'Authorization': f'Bearer {tok}',
        'x-service-name': 'CAPP',
        'x-app-version': ver,
        'x-device-platform': 'android',
        'x-device-identifier': 'vfdashboard-community-edition',
        'x-device-family': 'android',
        'x-device-locale': 'vi',
        'x-device-os-version': '14',
        'x-country': 'vi',
        'x-timezone': 'Asia/Ho_Chi_Minh',
        'x-imei': '00000000-' + uuid.uuid4().hex[:12],
        'Content-Type': 'application/json',
    }
    h.update(extra)
    return h

tests = [
    ('plain', {}, f'{BASE}/{UV}'),
    ('player=full-sub', {'x-player-identifier': sub}, f'{BASE}/{UV}'),
    ('player=id-number', {'x-player-identifier': subnum}, f'{BASE}/{UV}'),
    ('player=email', {'x-player-identifier': EMAIL}, f'{BASE}/{UV}'),
    ('ver=2.17.5', {'x-app-version': '2.17.5'}, f'{BASE}/{UV}'),
    ('acct-profile', {}, f'{BASE}/ccarusermgnt/api/v1/auth0/account/profile'),
    ('acct-client-creds', {}, f'{BASE}/ccarusermgnt/api/v1/auth0/account/client/credentials'),
    ('acct-check', {}, f'{BASE}/ccarusermgnt/api/v1/auth0/account/one-profile/check-customer'),
    ('vehicles-alt', {}, f'{BASE}/ccarusermgnt/api/v1/user vehicles'.replace(' ', '/')),
]
for tag, extra, url in tests:
    try:
        rr = requests.get(url, headers=hdr(extra), timeout=15)
        body = (rr.text or '')[:180].replace(chr(10), ' ')
        print(f'[{tag:18s}] {rr.status_code} {body!r}')
    except Exception as e:
        print(f'[{tag:18s}] EXC {type(e).__name__}: {e}')
