示例#1
0
        private async Task GetTokenAsync3(string code)
        {
            var d = new Dictionary <string, string>();

            d.Add("grant_type", "authorization_code");
            d.Add("client_id", "ownerapi");
            d.Add("code", code);
            d.Add("code_verifier", code_verifier);
            d.Add("redirect_uri", authHost + "/void/callback");

            string json = new JavaScriptSerializer().Serialize(d);
            string tmpAccessToken;

            using (HttpClient client = new TessHttpClient(new TessClientHandler(null, null))) {
                client.DefaultRequestHeaders.Referrer = referrer;
                using (StringContent content = new StringContent(json, Encoding.UTF8, "application/json")) {
                    HttpResponseMessage result = await client.PostAsync(authHost + "/oauth2/v3/token", content);

                    if (result.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception("authorization_code - Error: " + result.StatusCode);
                    }

                    string  resultContent = result.Content.ReadAsStringAsync().Result;
                    dynamic jsonResult    = new JavaScriptSerializer().DeserializeObject(resultContent);
                    //RefreshToken                = jsonResult["refresh_token"];
                    tmpAccessToken = jsonResult["access_token"];
                }
            }

            await GetTokenAsync4Async(tmpAccessToken);
        }
示例#2
0
        private string MFA2(string MFA_Code, string factor_id)
        {
            using (HttpClient client = new TessHttpClient(new TessClientHandler(false, false))) {
                client.DefaultRequestHeaders.Add("Cookie", cookie);

                Dictionary <string, string> d = new Dictionary <string, string>();
                d.Add("factor_id", factor_id);
                d.Add("passcode", MFA_Code);
                d.Add("transaction_id", transaction_id);

                string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(d);

                using (var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")) {
                    HttpResponseMessage result = client.PostAsync(authHost + "/oauth2/v3/authorize/mfa/verify", content).Result;
                    string resultContent       = result.Content.ReadAsStringAsync().Result;
                    try {
                        dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(resultContent);
                        object  o          = jsonResult["data"]["valid"];

                        if ((bool)o)
                        {
                            return(MFA3());
                        }
                    }
                    catch (Exception ex) {
                        Log.Error("Error: MFA2 : " + resultContent, ex);
                    }
                }
            }

            return("NULL");
        }
示例#3
0
        private string MFA3()
        {
            using (HttpClient client = new TessHttpClient(new TessClientHandler(false, false))) {
                client.DefaultRequestHeaders.Add("Cookie", cookie);

                Dictionary <string, string> d = new Dictionary <string, string>();
                d.Add("transaction_id", transaction_id);

                using (FormUrlEncodedContent content = new FormUrlEncodedContent(d)) {
                    UriBuilder b = new UriBuilder(authHost + "/oauth2/v3/authorize");
                    b.Port = -1;
                    var q = HttpUtility.ParseQueryString(b.Query);
                    q.Add("client_id", "ownerapi");
                    q.Add("code_challenge", code_challenge);
                    q.Add("code_challenge_method", "S256");
                    q.Add("redirect_uri", authHost + "/void/callback");
                    q.Add("response_type", "code");
                    q.Add("scope", "openid email offline_access");
                    q.Add("state", state);
                    b.Query = q.ToString();
                    string url  = b.ToString();
                    var    temp = content.ReadAsStringAsync().Result;
                    HttpResponseMessage result = client.PostAsync(url, content).Result;
                    string resultContent       = result.Content.ReadAsStringAsync().Result;
                    Uri    location            = result.Headers.Location;

                    if (result.StatusCode == HttpStatusCode.Redirect && location != null)
                    {
                        string code = HttpUtility.ParseQueryString(location.Query).Get("code");
                        Log.Debug("Code: " + code);
                        return(code);
                    }
                    else
                    {
                        Log.Warning("Error: MFA2 Fail!");
                        return("NULL");
                    }
                }
            }
        }
示例#4
0
        private async Task GetTokenAsync4Async(string tmpAccessToken)
        {
            var d = new Dictionary <string, string>();

            d.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            d.Add("client_id", TESLA_CLIENT_ID);
            d.Add("client_secret", TESLA_CLIENT_SECRET);
            string json = new JavaScriptSerializer().Serialize(d);

            using (HttpClient client = new TessHttpClient(new TessClientHandler(null, null))) {
                client.Timeout = TimeSpan.FromSeconds(5);

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tmpAccessToken);

                using (var content = new StringContent(json, Encoding.UTF8, "application/json")) {
                    HttpResponseMessage result = await client.PostAsync("https://owner-api.teslamotors.com/oauth/token", content);

                    string resultContent = result.Content.ReadAsStringAsync().Result;
                    Log.Debug("HttpStatus: " + result.StatusCode.ToString());

                    LoginResponse = SerializeTool.DeSerializeJson <LoginResponse>(resultContent);
                }
            }
        }
示例#5
0
        private async Task <bool> GetTokenAsync2(MatchCollection mc, string user, string pw)
        {
            int length = 0;

            Dictionary <string, string> d = new Dictionary <string, string>();

            foreach (Match m in mc)
            {
                string key   = m.Groups[1].Value;
                string value = m.Groups[2].Value;
                if (d.ContainsKey(key))
                {
                    continue;
                }
                if (key.Contains("cancel"))
                {
                    continue;
                }
                d.Add(key, value);
                if (key == "transaction_id")
                {
                    transaction_id = value;
                }
                length += m.Groups[1].Value.Length;
                length += m.Groups[2].Value.Length;
                length += 4;
            }

            d.Add("identity", user);
            d.Add("credential", pw);

            string code = "";

            using (HttpClient client = new TessHttpClient(new TessClientHandler(false, false))) {
                client.DefaultRequestHeaders.Add("Cookie", cookie);

                using (FormUrlEncodedContent content = new FormUrlEncodedContent(d)) {
                    UriBuilder b = new UriBuilder(authHost + "/oauth2/v3/authorize");
                    b.Port = -1;
                    var q = HttpUtility.ParseQueryString(b.Query);
                    q["client_id"]             = "ownerapi";
                    q["code_challenge"]        = code_challenge;
                    q["code_challenge_method"] = "S256";
                    q["redirect_uri"]          = authHost + "/void/callback";
                    q["response_type"]         = "code";
                    q["scope"] = "openid email offline_access";
                    q["state"] = state;
                    b.Query    = q.ToString();
                    string url = b.ToString();

                    referrer = b.Uri;

                    var temp = content.ReadAsStringAsync().Result;

                    HttpResponseMessage result = await client.PostAsync(url, content);

                    string resultContent = result.Content.ReadAsStringAsync().Result;
                    Uri    location      = result.Headers.Location;

                    if (result.StatusCode != HttpStatusCode.Redirect)
                    {
                        if (result.StatusCode == HttpStatusCode.OK && resultContent.Contains("passcode"))
                        {
                            return(false); // Signalisieren das wir mit MFA weiter machen müssen
                        }
                        else
                        {
                            Log.Error("GetTokenAsync2 HttpStatus: " + result.StatusCode.ToString() + " / Expecting: Redirect !!!");
                            throw new Exception("GetToken: " + result.StatusCode);
                        }
                    }

                    // Ohne MFA
                    if (location == null)
                    {
                        throw new Exception("GetTokenAsync2 Redirect Location = null!!! Wrong credentials?");
                    }
                    if (result.StatusCode == HttpStatusCode.Redirect && (location != null))
                    {
                        code = HttpUtility.ParseQueryString(location.Query).Get("code");
                        await GetTokenAsync3(code);

                        return(true);
                    }
                    else
                    {
                        throw new Exception("GetTokenAsync2 - result.StatusCode: " + result.StatusCode);
                    }
                }
            }
        }