Refactor PTC Login

FeroxRev [2016-07-20 19:41:55]
Refactor PTC Login
Filename
PokemonGo/RocketAPI/Client.cs
PokemonGo/RocketAPI/Console/Program.cs
PokemonGo/RocketAPI/Helpers/HttpClientHelper.cs
PokemonGo/RocketAPI/Login/PtcLogin.cs
PokemonGo/RocketAPI/PokemonGo.RocketAPI.csproj
PokemonGo/RocketAPI/Settings.cs
diff --git a/PokemonGo/RocketAPI/Client.cs b/PokemonGo/RocketAPI/Client.cs
index a406c4b..c24d14f 100644
--- a/PokemonGo/RocketAPI/Client.cs
+++ b/PokemonGo/RocketAPI/Client.cs
@@ -70,51 +70,18 @@ namespace PokemonGo.RocketAPI
                 _accessToken = tokenResponse.id_token;
                 Settings.GoogleRefreshToken = tokenResponse.access_token;
                 Console.WriteLine($"Put RefreshToken in settings for direct login: {Settings.GoogleRefreshToken}");
-                }
+            }
                 else
-                {
+            {
                 var tokenResponse = await GoogleLogin.GetAccessToken(Settings.GoogleRefreshToken);
                 _accessToken = tokenResponse.id_token;
+                _authType  = AuthType.Google;
             }
         }

-        public async Task LoginPtc(string username, string password)
+        public async Task DoPtcLogin(string username, string password)
         {
-            //Get session cookie
-            var sessionResp = await _httpClient.GetAsync(Resources.PtcLoginUrl);
-            var data = await sessionResp.Content.ReadAsStringAsync();
-            var lt = JsonHelper.GetValue(data, "lt");
-            var executionId = JsonHelper.GetValue(data, "execution");
-
-            //Login
-            var loginResp = await _httpClient.PostAsync(Resources.PtcLoginUrl,
-                new FormUrlEncodedContent(
-                    new[]
-                    {
-                        new KeyValuePair<string, string>("lt", lt),
-                        new KeyValuePair<string, string>("execution", executionId),
-                        new KeyValuePair<string, string>("_eventId", "submit"),
-                        new KeyValuePair<string, string>("username", username),
-                        new KeyValuePair<string, string>("password", password),
-                    }));
-
-            var ticketId = HttpUtility.ParseQueryString(loginResp.Headers.Location.Query)["ticket"];
-
-            //Get tokenvar
-            var tokenResp = await _httpClient.PostAsync(Resources.PtcLoginOauth,
-                new FormUrlEncodedContent(
-                    new[]
-                    {
-                        new KeyValuePair<string, string>("client_id", "mobile-app_pokemon-go"),
-                        new KeyValuePair<string, string>("redirect_uri", "https://www.nianticlabs.com/pokemongo/error"),
-                        new KeyValuePair<string, string>("client_secret",
-                            "w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR"),
-                        new KeyValuePair<string, string>("grant_type", "grant_type"),
-                        new KeyValuePair<string, string>("code", ticketId),
-                    }));
-
-            var tokenData = await tokenResp.Content.ReadAsStringAsync();
-            _accessToken = HttpUtility.ParseQueryString(tokenData)["access_token"];
+            _accessToken = await PtcLogin.GetAccessToken(username, password);
             _authType = AuthType.Ptc;
         }

diff --git a/PokemonGo/RocketAPI/Console/Program.cs b/PokemonGo/RocketAPI/Console/Program.cs
index cd0f2d2..bd5b32c 100644
--- a/PokemonGo/RocketAPI/Console/Program.cs
+++ b/PokemonGo/RocketAPI/Console/Program.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Linq;
 using System.Net.Http;
 using System.Text;
@@ -19,19 +20,16 @@ namespace PokemonGo.RocketAPI.Console
             Task.Run(() => Execute());
              System.Console.ReadLine();
         }
-
+
         static async void Execute()
         {
             var client = new Client(Settings.DefaultLatitude, Settings.DefaultLongitude);

-            if (Settings.UsePTC)
-            {
-                await client.LoginPtc(Settings.PtcUsername, Settings.PtcPassword);
-            }
-            else
-            {
+            if (Settings.AuthType == AuthType.Ptc)
+                await client.DoPtcLogin(Settings.PtcUsername, Settings.PtcPassword);
+            else if (Settings.AuthType == AuthType.Google)
                 await client.DoGoogleLogin();
-            }
+
             var serverResponse = await client.GetServer();
             var profile = await client.GetProfile();
             var settings = await client.GetSettings();
diff --git a/PokemonGo/RocketAPI/Helpers/HttpClientHelper.cs b/PokemonGo/RocketAPI/Helpers/HttpClientHelper.cs
index f3d8df4..42eec54 100644
--- a/PokemonGo/RocketAPI/Helpers/HttpClientHelper.cs
+++ b/PokemonGo/RocketAPI/Helpers/HttpClientHelper.cs
@@ -9,7 +9,7 @@ using Newtonsoft.Json.Linq;

 namespace PokemonGo.RocketAPI.Helpers
 {
-    public class HttpClientHelper
+    public static class HttpClientHelper
     {
         public static async Task<TResponse> PostFormEncodedAsync<TResponse>(string url, params KeyValuePair<string, string>[] keyValuePairs)
         {
diff --git a/PokemonGo/RocketAPI/Login/PtcLogin.cs b/PokemonGo/RocketAPI/Login/PtcLogin.cs
new file mode 100644
index 0000000..b7e3a73
--- /dev/null
+++ b/PokemonGo/RocketAPI/Login/PtcLogin.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using System.Web;
+using PokemonGo.RocketAPI.Helpers;
+
+namespace PokemonGo.RocketAPI.Login
+{
+    internal static class PtcLogin
+    {
+        public static async Task<string> GetAccessToken(string username, string password)
+        {
+            var handler = new HttpClientHandler()
+            {
+                AutomaticDecompression = DecompressionMethods.GZip,
+                AllowAutoRedirect = false
+            };
+
+            using (var tempHttpClient = new HttpClient(handler))
+            {
+                //Get session cookie
+                var sessionResp = await tempHttpClient.GetAsync(Resources.PtcLoginUrl);
+                var data = await sessionResp.Content.ReadAsStringAsync();
+                var lt = JsonHelper.GetValue(data, "lt");
+                var executionId = JsonHelper.GetValue(data, "execution");
+
+                //Login
+                var loginResp = await tempHttpClient.PostAsync(Resources.PtcLoginUrl,
+                    new FormUrlEncodedContent(
+                        new[]
+                        {
+                            new KeyValuePair<string, string>("lt", lt),
+                            new KeyValuePair<string, string>("execution", executionId),
+                            new KeyValuePair<string, string>("_eventId", "submit"),
+                            new KeyValuePair<string, string>("username", username),
+                            new KeyValuePair<string, string>("password", password),
+                        }));
+
+                var ticketId = HttpUtility.ParseQueryString(loginResp.Headers.Location.Query)["ticket"];
+
+                //Get tokenvar
+                var tokenResp = await tempHttpClient.PostAsync(Resources.PtcLoginOauth,
+                    new FormUrlEncodedContent(
+                        new[]
+                        {
+                            new KeyValuePair<string, string>("client_id", "mobile-app_pokemon-go"),
+                            new KeyValuePair<string, string>("redirect_uri",
+                                "https://www.nianticlabs.com/pokemongo/error"),
+                            new KeyValuePair<string, string>("client_secret",
+                                "w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR"),
+                            new KeyValuePair<string, string>("grant_type", "grant_type"),
+                            new KeyValuePair<string, string>("code", ticketId),
+                        }));
+
+                var tokenData = await tokenResp.Content.ReadAsStringAsync();
+                return HttpUtility.ParseQueryString(tokenData)["access_token"];
+            }
+        }
+    }
+}
diff --git a/PokemonGo/RocketAPI/PokemonGo.RocketAPI.csproj b/PokemonGo/RocketAPI/PokemonGo.RocketAPI.csproj
index 02cf8f8..f92f5ad 100644
--- a/PokemonGo/RocketAPI/PokemonGo.RocketAPI.csproj
+++ b/PokemonGo/RocketAPI/PokemonGo.RocketAPI.csproj
@@ -95,6 +95,7 @@
     <Compile Include="Helpers\S2Helper.cs" />
     <Compile Include="Helpers\Utils.cs" />
     <Compile Include="Login\GoogleLogin.cs" />
+    <Compile Include="Login\PtcLogin.cs" />
     <Compile Include="Settings.cs" />
     <None Include="app.config" />
     <Compile Include="Client.cs" />
diff --git a/PokemonGo/RocketAPI/Settings.cs b/PokemonGo/RocketAPI/Settings.cs
index 1cfa46c..2658099 100644
--- a/PokemonGo/RocketAPI/Settings.cs
+++ b/PokemonGo/RocketAPI/Settings.cs
@@ -13,7 +13,7 @@ namespace PokemonGo.RocketAPI
     public static class Settings
     {
         //Fetch these settings from intercepting the /auth call in headers and body (only needed for google auth)
-        public const bool UsePTC = false;
+        public const AuthType AuthType = Enums.AuthType.Google;
         public const string PtcUsername = "User";
         public const string PtcPassword = "alligator2";
         public static string GoogleRefreshToken = string.Empty;
You may download the files in Public Git.