Merge branch 'master' of https://github.com/Lorac/OwinOAuthProviders into Lorac-master

This commit is contained in:
Jerrie Pelser
2015-04-09 09:25:45 +07:00
6 changed files with 167 additions and 0 deletions

View File

@@ -260,6 +260,11 @@
<Compile Include="Twitch\Provider\TwitchAuthenticationProvider.cs" />
<Compile Include="Twitch\Provider\TwitchReturnEndpointContext.cs" />
<Compile Include="Twitch\Provider\ITwitchAuthenticationProvider.cs" />
<Compile Include="Wargaming\Constant.cs" />
<Compile Include="Wargaming\WargamingAccountAuthenticationExtensions.cs" />
<Compile Include="Wargaming\WargamingAuthenticationHandler.cs" />
<Compile Include="Wargaming\WargamingAuthenticationOptions.cs" />
<Compile Include="Wargaming\WargamingAuthenticationMiddleware.cs" />
<Compile Include="WordPress\WordPressAuthenticationExtensions.cs" />
<Compile Include="WordPress\WordPressAuthenticationHandler.cs" />
<Compile Include="WordPress\WordPressAuthenticationMiddleware.cs" />

View File

@@ -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";
}
}

View File

@@ -0,0 +1,49 @@
using Microsoft.Owin;
using System;
namespace Owin.Security.Providers.Wargaming
{
/// <summary>
/// Extension methods for using <see cref="WargamingAuthenticationMiddleware"/>
/// </summary>
public static class WargamingAccountAuthenticationExtensions
{
/// <summary>
/// Authenticate users using Wargaming
/// </summary>
/// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
/// <param name="options">Middleware configuration options</param>
/// <returns>The updated <see cref="IAppBuilder"/></returns>
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);
}
/// <summary>
/// Authenticate users using Steam
/// </summary>
/// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
/// <param name="applicationKey">The wargaming application key</param>
/// <returns>The updated <see cref="IAppBuilder"/></returns>
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
});
}
}
}

View File

@@ -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<WargamingAuthenticationOptions>
{
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<string, string> 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));
}
}
}
}

View File

@@ -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
{
/// <summary>
/// OWIN middleware for authenticating users using an OpenID provider
/// </summary>
public class WargamingAuthenticationMiddleware : OpenIDAuthenticationMiddlewareBase<WargamingAuthenticationOptions>
{
/// <summary>
/// Initializes a <see cref="WargamingAuthenticationMiddleware"/>
/// </summary>
/// <param name="next">The next middleware in the OWIN pipeline to invoke</param>
/// <param name="app">The OWIN application</param>
/// <param name="options">Configuration options for the middleware</param>
public WargamingAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, WargamingAuthenticationOptions options)
: base(next, app, options)
{ }
protected override AuthenticationHandler<WargamingAuthenticationOptions> CreateSpecificHandler()
{
return new WargamingAuthenticationHandler(_httpClient, _logger);
}
}
}

View File

@@ -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
{
/// <summary>
/// Region to use for to log in
/// </summary>
public enum Region
{
Europe,
US,
Russia
}
/// <summary>
/// Gets or sets the Wargaming-assigned appId
/// </summary>
public string AppId { get; set; }
}
}