Skip to content

Commit

Permalink
Change to OAuth credentials flow
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxRev committed Jul 28, 2016
1 parent a83d05a commit 0655773
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/PokemonGo.RocketAPI/Proto/google/protobuf
*.sln
*.suo
/PokemonGo.RocketAPI/bin/Release
/PokemonGo.RocketAPI/obj/Release
26 changes: 18 additions & 8 deletions PokemonGo.RocketAPI/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ namespace PokemonGo.RocketAPI
{
public class Client
{
public Rpc.Login Login => new Rpc.Login(this);
public Rpc.Player Player => new Rpc.Player(this);
public Rpc.Download Download => new Rpc.Download(this);
public Rpc.Inventory Inventory => new Rpc.Inventory(this);
public Rpc.Map Map => new Rpc.Map(this);
public Rpc.Fort Fort => new Rpc.Fort(this);
public Rpc.Encounter Encounter => new Rpc.Encounter(this);
public Rpc.Misc Misc => new Rpc.Misc(this);
public Rpc.Login Login;
public Rpc.Player Player;
public Rpc.Download Download;
public Rpc.Inventory Inventory;
public Rpc.Map Map;
public Rpc.Fort Fort;
public Rpc.Encounter Encounter;
public Rpc.Misc Misc;

public ISettings Settings { get; }
public string AuthToken { get; set; }
Expand All @@ -45,6 +45,16 @@ public class Client
public Client(ISettings settings)
{
Settings = settings;

Login = new Rpc.Login(this);
Player = new Rpc.Player(this);
Download = new Rpc.Download(this);
Inventory = new Rpc.Inventory(this);
Map = new Rpc.Map(this);
Fort = new Rpc.Fort(this);
Encounter = new Rpc.Encounter(this);
Misc = new Rpc.Misc(this);

Player.SetCoordinates(Settings.DefaultLatitude, Settings.DefaultLongitude, Settings.DefaultAltitude);
}
}
Expand Down
16 changes: 16 additions & 0 deletions PokemonGo.RocketAPI/Exceptions/GoogleException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PokemonGo.RocketAPI.Exceptions
{
public class GoogleException : Exception
{
public GoogleException(string message) : base(message)
{

}
}
}
12 changes: 12 additions & 0 deletions PokemonGo.RocketAPI/Exceptions/GoogleOfflineException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PokemonGo.RocketAPI.Exceptions
{
public class GoogleOfflineException : Exception
{
}
}
14 changes: 8 additions & 6 deletions PokemonGo.RocketAPI/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ namespace PokemonGo.RocketAPI
{
public interface ISettings
{
AuthType AuthType { get; }
double DefaultLatitude { get; }
double DefaultLongitude { get; }
double DefaultAltitude { get; }
AuthType AuthType { get; set; }
double DefaultLatitude { get; set; }
double DefaultLongitude { get; set; }
double DefaultAltitude { get; set; }
string GoogleRefreshToken { get; set; }
string PtcPassword { get; }
string PtcUsername { get; }
string PtcPassword { get; set; }
string PtcUsername { get; set; }
string GoogleUsername { get; set; }
string GooglePassword { get; set; }
}
}
12 changes: 9 additions & 3 deletions PokemonGo.RocketAPI/Login/GoogleLogin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using PokemonGo.RocketAPI.Helpers;

Expand All @@ -8,18 +9,23 @@ public static class GoogleLogin
{
private const string OauthTokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
private const string OauthEndpoint = "https://accounts.google.com/o/oauth2/device/code";
private const string ClientId = "848232511240-73ri3t7plvk96pj4f85uj8otdat2alem.apps.googleusercontent.com";
private const string ClientId = "848232511240-7so421jotr2609rmqakceuu1luuq0ptb.apps.googleusercontent.com";// "848232511240-73ri3t7plvk96pj4f85uj8otdat2alem.apps.googleusercontent.com";

private const string ClientSecret = "NCjF1TLi2CcY6t5mt0ZveuL7";


public static async Task<TokenResponseModel> GetAccessToken(DeviceCodeModel deviceCode)
{
int count = 0;

//Poll until user submitted code..
TokenResponseModel tokenResponse;
do
{
await Task.Delay(2000);
tokenResponse = await PollSubmittedToken(deviceCode.device_code);
} while (tokenResponse.access_token == null || tokenResponse.refresh_token == null);
count++;
} while (tokenResponse.access_token == null || tokenResponse.refresh_token == null && count < 100);

return tokenResponse;
}
Expand All @@ -29,7 +35,7 @@ public static async Task<DeviceCodeModel> GetDeviceCode()
var deviceCode = await HttpClientHelper.PostFormEncodedAsync<DeviceCodeModel>(OauthEndpoint,
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("scope", "openid email https://www.googleapis.com/auth/userinfo.email"));
return deviceCode;
return deviceCode;
}

private static async Task<TokenResponseModel> PollSubmittedToken(string deviceCode)
Expand Down
38 changes: 38 additions & 0 deletions PokemonGo.RocketAPI/Login/GoogleLoginGPSOAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DankMemes.GPSOAuthSharp;
using Newtonsoft.Json;
using PokemonGo.RocketAPI.Exceptions;

namespace PokemonGo.RocketAPI.Login
{
public static class GoogleLoginGPSOAuth
{
public static string DoLogin(string username, string password)
{
GPSOAuthClient client = new GPSOAuthClient(username, password);
Dictionary<string, string> response = client.PerformMasterLogin();

if(response.ContainsKey("Error"))
throw new GoogleException(response["Error"]);

//Todo: captcha/2fa implementation

if(!response.ContainsKey("Auth"))
throw new GoogleOfflineException();

Dictionary<string, string> oauthResponse = client.PerformOAuth( response["Token"],
"audience:server:client_id:848232511240-7so421jotr2609rmqakceuu1luuq0ptb.apps.googleusercontent.com",
"com.nianticlabs.pokemongo",
"321187995bc7cdc2b5fc91b11a96e2baa8602c62");

if(!oauthResponse.ContainsKey("Auth"))
throw new GoogleOfflineException();

return oauthResponse["Auth"];
}
}
}
11 changes: 9 additions & 2 deletions PokemonGo.RocketAPI/PokemonGo.RocketAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PokemonGo.RocketAPI</RootNamespace>
<AssemblyName>Pokemon Go Rocket API</AssemblyName>
<AssemblyName>Pokemon.Go.Rocket.API</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Expand Down Expand Up @@ -38,6 +38,10 @@
<HintPath>$(SolutionDir)packages\Google.Protobuf.3.0.0-beta4\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="GPSOAuthSharp, Version=0.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\GPSOAuthSharp.0.0.5\lib\GPSOAuthSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\EnterpriseLibrary.TransientFaultHandling.6.0.1304.0\lib\portable-net45+win+wp8\Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.dll</HintPath>
<Private>True</Private>
Expand All @@ -51,7 +55,7 @@
<Private>True</Private>
</Reference>
<Reference Include="POGOProtos, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\POGOProtos.1.3.0\lib\net45\POGOProtos.dll</HintPath>
<HintPath>$(SolutionDir)\packages\POGOProtos.1.3.0\lib\net45\POGOProtos.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="S2Geometry, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -79,6 +83,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\GoogleException.cs" />
<Compile Include="Exceptions\GoogleOfflineException.cs" />
<Compile Include="HttpClient\PokemonClient.cs" />
<Compile Include="Enums\AuthType.cs" />
<Compile Include="Exceptions\AccessTokenExpiredException.cs" />
Expand All @@ -93,6 +99,7 @@
<Compile Include="Helpers\Utils.cs" />
<Compile Include="ISettings.cs" />
<Compile Include="Login\GoogleLogin.cs" />
<Compile Include="Login\GoogleLoginGPSOAuth.cs" />
<Compile Include="Login\PtcLogin.cs" />
<Compile Include="Rpc\BaseRpc.cs" />
<Compile Include="Rpc\Download.cs" />
Expand Down
20 changes: 19 additions & 1 deletion PokemonGo.RocketAPI/Rpc/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@

namespace PokemonGo.RocketAPI.Rpc
{
public delegate void GoogleDeviceCodeDelegate(string code, string uri);
public class Login : BaseRpc
{
//public event GoogleDeviceCodeDelegate GoogleDeviceCodeEvent;

public Login(Client client) : base(client)
{
}

public async Task DoGoogleLogin()
public async Task DoGoogleLogin(string username,string password)
{
_client.AuthType = AuthType.Google;

_client.AuthToken = GoogleLoginGPSOAuth.DoLogin(username, password);
await SetServer();

/*
* This is our old authentication method
* Keeping this around in case we might need it later on
*
GoogleLogin.TokenResponseModel tokenResponse = null;
if (_client.Settings.GoogleRefreshToken != string.Empty)
{
Expand All @@ -34,12 +44,17 @@ public async Task DoGoogleLogin()
if (_client.AuthToken == null)
{
var deviceCode = await GoogleLogin.GetDeviceCode();
if(deviceCode?.user_code == null || deviceCode?.verification_url == null)
throw new GoogleOfflineException();
GoogleDeviceCodeEvent?.Invoke(deviceCode.user_code, deviceCode.verification_url);
tokenResponse = await GoogleLogin.GetAccessToken(deviceCode);
_client.Settings.GoogleRefreshToken = tokenResponse?.refresh_token;
_client.AuthToken = tokenResponse?.id_token;
}
await SetServer();
*/
}

public async Task DoPtcLogin(string username, string password)
Expand Down Expand Up @@ -95,7 +110,10 @@ private async Task SetServer()
var serverResponse = await PostProto<Request>(Resources.RpcUrl, serverRequest);

if (serverResponse.AuthTicket == null)
{
_client.AuthToken = null;
throw new AccessTokenExpiredException();
}

_client.AuthTicket = serverResponse.AuthTicket;
_client.ApiUrl = serverResponse.ApiUrl;
Expand Down
1 change: 1 addition & 0 deletions PokemonGo.RocketAPI/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<package id="EnterpriseLibrary.TransientFaultHandling.Data" version="6.0.1304.1" targetFramework="net45" />
<package id="Google.Protobuf" version="3.0.0-beta4" targetFramework="net45" />
<package id="Google.Protobuf.Tools" version="3.0.0-beta3" targetFramework="net45" />
<package id="GPSOAuthSharp" version="0.0.5" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="POGOProtos" version="1.3.0" targetFramework="net45" />
Expand Down

0 comments on commit 0655773

Please sign in to comment.