Merge branch 'mariozski-battlenet-fix'

This commit is contained in:
Jerrie Pelser
2015-04-08 15:29:56 +07:00
2 changed files with 59 additions and 54 deletions

View File

@@ -16,54 +16,59 @@ namespace Owin.Security.Providers.BattleNet
{
private const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
private readonly string _tokenEndpoint = "https://eu.battle.net/oauth/token";
private readonly string _accountUserIdEndpoint = "https://eu.api.battle.net/account/user/id";
private readonly string _accountUserBattleTagEndpoint = "https://eu.api.battle.net/account/user/battletag";
private readonly string _oauthAuthEndpoint = "https://eu.battle.net/oauth/authorize";
private string tokenEndpoint = "https://eu.battle.net/oauth/token";
private string accountUserIdEndpoint = "https://eu.api.battle.net/account/user/id";
private string accountUserBattleTagEndpoint = "https://eu.api.battle.net/account/user/battletag";
private string oauthAuthEndpoint = "https://eu.battle.net/oauth/authorize";
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
private readonly ILogger logger;
private readonly HttpClient httpClient;
public BattleNetAuthenticationHandler(HttpClient httpClient, ILogger logger)
{
_httpClient = httpClient;
_logger = logger;
switch (Options.Region)
{
case Region.China:
_tokenEndpoint = "https://cn.battle.net/oauth/token";
_accountUserIdEndpoint = "https://cn.api.battle.net/account/user/id";
_accountUserBattleTagEndpoint = "https://cn.api.battle.net/account/user/battletag";
_oauthAuthEndpoint = "https://cn.battle.net/oauth/authorize";
break;
case Region.Korea:
_tokenEndpoint = "https://kr.battle.net/oauth/token";
_accountUserIdEndpoint = "https://kr.api.battle.net/account/user/id";
_accountUserBattleTagEndpoint = "https://kr.api.battle.net/account/user/battletag";
_oauthAuthEndpoint = "https://kr.battle.net/oauth/authorize";
break;
case Region.Taiwan:
_tokenEndpoint = "https://tw.battle.net/oauth/token";
_accountUserIdEndpoint = "https://tw.api.battle.net/account/user/id";
_accountUserBattleTagEndpoint = "https://tw.api.battle.net/account/user/battletag";
_oauthAuthEndpoint = "https://tw.battle.net/oauth/authorize";
break;
case Region.Europe:
_tokenEndpoint = "https://eu.battle.net/oauth/token";
_accountUserIdEndpoint = "https://eu.api.battle.net/account/user/id";
_accountUserBattleTagEndpoint = "https://eu.api.battle.net/account/user/battletag";
_oauthAuthEndpoint = "https://eu.battle.net/oauth/authorize";
break;
default:
_tokenEndpoint = "https://us.battle.net/oauth/token";
_accountUserIdEndpoint = "https://us.api.battle.net/account/user/id";
_accountUserBattleTagEndpoint = "https://us.api.battle.net/account/user/battletag";
_oauthAuthEndpoint = "https://us.battle.net/oauth/authorize";
break;
}
this.httpClient = httpClient;
this.logger = logger;
}
protected override Task InitializeCoreAsync()
{
switch (Options.Region)
{
case Region.China:
tokenEndpoint = "https://cn.battle.net/oauth/token";
accountUserIdEndpoint = "https://cn.api.battle.net/account/user/id";
accountUserBattleTagEndpoint = "https://cn.api.battle.net/account/user/battletag";
oauthAuthEndpoint = "https://cn.battle.net/oauth/authorize";
break;
case Region.Korea:
tokenEndpoint = "https://kr.battle.net/oauth/token";
accountUserIdEndpoint = "https://kr.api.battle.net/account/user/id";
accountUserBattleTagEndpoint = "https://kr.api.battle.net/account/user/battletag";
oauthAuthEndpoint = "https://kr.battle.net/oauth/authorize";
break;
case Region.Taiwan:
tokenEndpoint = "https://tw.battle.net/oauth/token";
accountUserIdEndpoint = "https://tw.api.battle.net/account/user/id";
accountUserBattleTagEndpoint = "https://tw.api.battle.net/account/user/battletag";
oauthAuthEndpoint = "https://tw.battle.net/oauth/authorize";
break;
case Region.Europe:
tokenEndpoint = "https://eu.battle.net/oauth/token";
accountUserIdEndpoint = "https://eu.api.battle.net/account/user/id";
accountUserBattleTagEndpoint = "https://eu.api.battle.net/account/user/battletag";
oauthAuthEndpoint = "https://eu.battle.net/oauth/authorize";
break;
default:
tokenEndpoint = "https://us.battle.net/oauth/token";
accountUserIdEndpoint = "https://us.api.battle.net/account/user/id";
accountUserBattleTagEndpoint = "https://us.api.battle.net/account/user/battletag";
oauthAuthEndpoint = "https://us.battle.net/oauth/authorize";
break;
}
return Task.FromResult(true);
}
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
{
AuthenticationProperties properties = null;
@@ -92,7 +97,7 @@ namespace Owin.Security.Providers.BattleNet
}
// OAuth2 10.12 CSRF
if (!ValidateCorrelationId(properties, _logger))
if (!ValidateCorrelationId(properties, logger))
{
return new AuthenticationTicket(null, properties);
}
@@ -115,7 +120,7 @@ namespace Owin.Security.Providers.BattleNet
};
// Request the token
var tokenResponse = await _httpClient.PostAsync(_tokenEndpoint, new FormUrlEncodedContent(body));
var tokenResponse = await httpClient.PostAsync(tokenEndpoint, new FormUrlEncodedContent(body));
tokenResponse.EnsureSuccessStatusCode();
var text = await tokenResponse.Content.ReadAsStringAsync();
@@ -125,13 +130,13 @@ namespace Owin.Security.Providers.BattleNet
var expires = (string)response.expires_in;
// Get WoW User Id
var graphResponse = await _httpClient.GetAsync(_accountUserIdEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
var graphResponse = await httpClient.GetAsync(accountUserIdEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
graphResponse.EnsureSuccessStatusCode();
text = await graphResponse.Content.ReadAsStringAsync();
var userId = JObject.Parse(text);
// Get WoW BattleTag
graphResponse = await _httpClient.GetAsync(_accountUserBattleTagEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
graphResponse = await httpClient.GetAsync(accountUserBattleTagEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled);
graphResponse.EnsureSuccessStatusCode();
text = await graphResponse.Content.ReadAsStringAsync();
var battleTag = JObject.Parse(text);
@@ -166,7 +171,7 @@ namespace Owin.Security.Providers.BattleNet
}
catch (Exception ex)
{
_logger.WriteError(ex.Message);
logger.WriteError(ex.Message);
}
return new AuthenticationTicket(null, properties);
}
@@ -212,7 +217,7 @@ namespace Owin.Security.Providers.BattleNet
var state = Options.StateDataFormat.Protect(properties);
var authorizationEndpoint =
_oauthAuthEndpoint +
oauthAuthEndpoint +
"?response_type=code" +
"&client_id=" + Uri.EscapeDataString(Options.ClientId) +
"&redirect_uri=" + Uri.EscapeDataString(redirectUri) +
@@ -244,7 +249,7 @@ namespace Owin.Security.Providers.BattleNet
var ticket = await AuthenticateAsync();
if (ticket == null)
{
_logger.WriteWarning("Invalid return state, unable to redirect.");
logger.WriteWarning("Invalid return state, unable to redirect.");
Response.StatusCode = 500;
return true;
}

View File

@@ -13,8 +13,8 @@ namespace Owin.Security.Providers.BattleNet
{
public class BattleNetAuthenticationMiddleware : AuthenticationMiddleware<BattleNetAuthenticationOptions>
{
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
private readonly HttpClient httpClient;
private readonly ILogger logger;
public BattleNetAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, BattleNetAuthenticationOptions options)
: base(next, options)
@@ -26,7 +26,7 @@ namespace Owin.Security.Providers.BattleNet
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
Resources.Exception_OptionMustBeProvided, "ClientSecret"));
_logger = app.CreateLogger<BattleNetAuthenticationMiddleware>();
logger = app.CreateLogger<BattleNetAuthenticationMiddleware>();
if (Options.Provider == null)
Options.Provider = new BattleNetAuthenticationProvider();
@@ -42,7 +42,7 @@ namespace Owin.Security.Providers.BattleNet
if (String.IsNullOrEmpty(Options.SignInAsAuthenticationType))
Options.SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType();
_httpClient = new HttpClient(ResolveHttpMessageHandler(Options))
httpClient = new HttpClient(ResolveHttpMessageHandler(Options))
{
Timeout = Options.BackchannelTimeout,
MaxResponseContentBufferSize = 1024 * 1024 * 10
@@ -59,7 +59,7 @@ namespace Owin.Security.Providers.BattleNet
/// </returns>
protected override AuthenticationHandler<BattleNetAuthenticationOptions> CreateHandler()
{
return new BattleNetAuthenticationHandler(_httpClient, _logger);
return new BattleNetAuthenticationHandler(httpClient, logger);
}
private static HttpMessageHandler ResolveHttpMessageHandler(BattleNetAuthenticationOptions options)