From ae97f04d9328b57702dfb53df263b7bbf2f06bb3 Mon Sep 17 00:00:00 2001 From: mariozski Date: Sun, 29 Mar 2015 16:49:39 +0200 Subject: [PATCH 1/2] fixed Battle.net handler constructor --- .../BattleNetAuthenticationHandler.cs | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs index c2993df..77c0e10 100644 --- a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs +++ b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs @@ -16,10 +16,10 @@ 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; @@ -28,42 +28,48 @@ namespace Owin.Security.Providers.BattleNet { _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; - } } + protected override Task InitializeCoreAsync() + { + return Task.Run(() => + { + 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; + } + }); + } + protected override async Task AuthenticateCoreAsync() { AuthenticationProperties properties = null; From 4bf2cbc1c01e70e693199ca763ca320873079073 Mon Sep 17 00:00:00 2001 From: Jerrie Pelser Date: Wed, 8 Apr 2015 15:29:14 +0700 Subject: [PATCH 2/2] Fix naming conventions while I'm at it... --- .../BattleNetAuthenticationHandler.cs | 99 +++++++++---------- .../BattleNetAuthenticationMiddleware.cs | 10 +- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs index 77c0e10..8a8454a 100644 --- a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs +++ b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationHandler.cs @@ -16,58 +16,57 @@ namespace Owin.Security.Providers.BattleNet { private const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string"; - 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 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; + this.httpClient = httpClient; + this.logger = logger; } protected override Task InitializeCoreAsync() { - return Task.Run(() => + switch (Options.Region) { - 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; - } - }); + 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 AuthenticateCoreAsync() @@ -98,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); } @@ -121,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(); @@ -131,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); @@ -172,7 +171,7 @@ namespace Owin.Security.Providers.BattleNet } catch (Exception ex) { - _logger.WriteError(ex.Message); + logger.WriteError(ex.Message); } return new AuthenticationTicket(null, properties); } @@ -218,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) + @@ -250,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; } diff --git a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationMiddleware.cs b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationMiddleware.cs index e54e1fa..acda7ca 100644 --- a/Owin.Security.Providers/BattleNet/BattleNetAuthenticationMiddleware.cs +++ b/Owin.Security.Providers/BattleNet/BattleNetAuthenticationMiddleware.cs @@ -13,8 +13,8 @@ namespace Owin.Security.Providers.BattleNet { public class BattleNetAuthenticationMiddleware : AuthenticationMiddleware { - 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(); + logger = app.CreateLogger(); 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 /// protected override AuthenticationHandler CreateHandler() { - return new BattleNetAuthenticationHandler(_httpClient, _logger); + return new BattleNetAuthenticationHandler(httpClient, logger); } private static HttpMessageHandler ResolveHttpMessageHandler(BattleNetAuthenticationOptions options)