diff --git a/Owin.Security.Providers/Owin.Security.Providers.csproj b/Owin.Security.Providers/Owin.Security.Providers.csproj index a93b0fb..e311456 100644 --- a/Owin.Security.Providers/Owin.Security.Providers.csproj +++ b/Owin.Security.Providers/Owin.Security.Providers.csproj @@ -260,6 +260,11 @@ + + + + + diff --git a/Owin.Security.Providers/Wargaming/Constant.cs b/Owin.Security.Providers/Wargaming/Constant.cs new file mode 100644 index 0000000..9abca13 --- /dev/null +++ b/Owin.Security.Providers/Wargaming/Constant.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Owin.Security.Providers.Wargaming +{ + internal static class Constants + { + internal const string DefaultAuthenticationType = "Wargaming"; + } +} \ No newline at end of file diff --git a/Owin.Security.Providers/Wargaming/WargamingAccountAuthenticationExtensions.cs b/Owin.Security.Providers/Wargaming/WargamingAccountAuthenticationExtensions.cs new file mode 100644 index 0000000..ea0fd36 --- /dev/null +++ b/Owin.Security.Providers/Wargaming/WargamingAccountAuthenticationExtensions.cs @@ -0,0 +1,49 @@ +using Microsoft.Owin; +using System; + +namespace Owin.Security.Providers.Wargaming +{ + /// + /// Extension methods for using + /// + public static class WargamingAccountAuthenticationExtensions + { + /// + /// Authenticate users using Wargaming + /// + /// The passed to the configuration method + /// Middleware configuration options + /// The updated + public static IAppBuilder UseWargamingAccountAuthentication(this IAppBuilder app, WargamingAuthenticationOptions options) + { + if (app == null) + { + throw new ArgumentNullException("app"); + } + if (options == null) + { + throw new ArgumentNullException("options"); + } + + return app.Use(typeof(WargamingAuthenticationMiddleware), app, options); + } + + /// + /// Authenticate users using Steam + /// + /// The passed to the configuration method + /// The wargaming application key + /// The updated + public static IAppBuilder UseWargamingAccountAuthentication(this IAppBuilder app, string appId) + { + return UseWargamingAccountAuthentication(app, new WargamingAuthenticationOptions + { + ProviderDiscoveryUri = "https://na.wargaming.net/id/openid/", + Caption = "Wargaming", + AuthenticationType = "Wargaming", + CallbackPath = new PathString("/signin-wargaming"), + AppId = appId + }); + } + } +} \ No newline at end of file diff --git a/Owin.Security.Providers/Wargaming/WargamingAuthenticationHandler.cs b/Owin.Security.Providers/Wargaming/WargamingAuthenticationHandler.cs new file mode 100644 index 0000000..8bb5477 --- /dev/null +++ b/Owin.Security.Providers/Wargaming/WargamingAuthenticationHandler.cs @@ -0,0 +1,36 @@ +using Microsoft.Owin.Logging; +using Newtonsoft.Json; +using Owin.Security.Providers.OpenID; +using System.Collections.Generic; +using System.Net.Http; +using System.Security.Claims; +using System.Text.RegularExpressions; + +namespace Owin.Security.Providers.Wargaming +{ + internal sealed class WargamingAuthenticationHandler : OpenIDAuthenticationHandlerBase + { + private readonly Regex AccountIDRegex = new Regex(@"^https://na.wargaming.net/id/([0-9]{10}).*$", RegexOptions.Compiled); + + private const string UserInfoUri = "https://api.worldoftanks.com/wot/account/info/?application_id={0}&account_id={1}&fields=nickname"; + + public WargamingAuthenticationHandler(HttpClient httpClient, ILogger logger) + : base(httpClient, logger) + { + } + + protected override void SetIdentityInformations(ClaimsIdentity identity, string claimedID, IDictionary attributeExchangeProperties) + { + Match accountIDMatch = AccountIDRegex.Match(claimedID); + if (accountIDMatch.Success) + { + string accountID = accountIDMatch.Groups[1].Value; + var getUserInfoTask = _httpClient.GetStringAsync(string.Format(UserInfoUri, Options.AppId, accountID)); + getUserInfoTask.Wait(); + string userInfoRaw = getUserInfoTask.Result; + dynamic userInfo = JsonConvert.DeserializeObject(userInfoRaw); + identity.AddClaim(new Claim(ClaimTypes.Name, (string)userInfo["data"][accountID].nickname, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType)); + } + } + } +} \ No newline at end of file diff --git a/Owin.Security.Providers/Wargaming/WargamingAuthenticationMiddleware.cs b/Owin.Security.Providers/Wargaming/WargamingAuthenticationMiddleware.cs new file mode 100644 index 0000000..3f541e3 --- /dev/null +++ b/Owin.Security.Providers/Wargaming/WargamingAuthenticationMiddleware.cs @@ -0,0 +1,35 @@ +using System; +using System.Globalization; +using System.Net.Http; +using Microsoft.Owin; +using Microsoft.Owin.Logging; +using Microsoft.Owin.Security; +using Microsoft.Owin.Security.DataHandler; +using Microsoft.Owin.Security.DataProtection; +using Microsoft.Owin.Security.Infrastructure; +using Owin.Security.Providers.OpenID; + +namespace Owin.Security.Providers.Wargaming +{ + /// + /// OWIN middleware for authenticating users using an OpenID provider + /// + public class WargamingAuthenticationMiddleware : OpenIDAuthenticationMiddlewareBase + { + + /// + /// Initializes a + /// + /// The next middleware in the OWIN pipeline to invoke + /// The OWIN application + /// Configuration options for the middleware + public WargamingAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, WargamingAuthenticationOptions options) + : base(next, app, options) + { } + + protected override AuthenticationHandler CreateSpecificHandler() + { + return new WargamingAuthenticationHandler(_httpClient, _logger); + } + } +} diff --git a/Owin.Security.Providers/Wargaming/WargamingAuthenticationOptions.cs b/Owin.Security.Providers/Wargaming/WargamingAuthenticationOptions.cs new file mode 100644 index 0000000..040cf0a --- /dev/null +++ b/Owin.Security.Providers/Wargaming/WargamingAuthenticationOptions.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Net.Http; +using Microsoft.Owin; +using Microsoft.Owin.Security; +using Owin.Security.Providers.OpenID; + +namespace Owin.Security.Providers.Wargaming +{ + public class WargamingAuthenticationOptions : OpenIDAuthenticationOptions + { + /// + /// Region to use for to log in + /// + public enum Region + { + Europe, + US, + Russia + } + + /// + /// Gets or sets the Wargaming-assigned appId + /// + public string AppId { get; set; } + } + + +}