From 01cb86dd6b8500e7600d4fc493ee911a570027e1 Mon Sep 17 00:00:00 2001 From: Dav Evans Date: Sat, 4 Oct 2014 14:42:44 +1000 Subject: [PATCH] Add OAuth middleware --- src/Church/Church.ConsoleApp/App.config | 6 ++ .../Church.ConsoleApp.csproj | 79 +++++++++++++++++++ src/Church/Church.ConsoleApp/Program.cs | 32 ++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++ src/Church/Church.ConsoleApp/packages.config | 8 ++ .../ChurchOAuthAuthorizationServerProvider.cs | 31 ++++++++ .../Church.Host.Owin.Core.csproj | 17 +++- .../Controllers/ChurchController.cs | 1 + src/Church/Church.Host.Owin.Core/Startup.cs | 20 ++++- src/Church/Church.Host.Owin.Core/Web.config | 6 +- .../Church.Host.Owin.Core/packages.config | 6 +- src/Church/Church.IntegrationTests/app.config | 4 + src/Church/Church.sln | 13 +++ src/Church/packages/repositories.config | 1 + 14 files changed, 251 insertions(+), 9 deletions(-) create mode 100644 src/Church/Church.ConsoleApp/App.config create mode 100644 src/Church/Church.ConsoleApp/Church.ConsoleApp.csproj create mode 100644 src/Church/Church.ConsoleApp/Program.cs create mode 100644 src/Church/Church.ConsoleApp/Properties/AssemblyInfo.cs create mode 100644 src/Church/Church.ConsoleApp/packages.config create mode 100644 src/Church/Church.Host.Owin.Core/Authentication/ChurchOAuthAuthorizationServerProvider.cs diff --git a/src/Church/Church.ConsoleApp/App.config b/src/Church/Church.ConsoleApp/App.config new file mode 100644 index 0000000..9c05822 --- /dev/null +++ b/src/Church/Church.ConsoleApp/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Church/Church.ConsoleApp/Church.ConsoleApp.csproj b/src/Church/Church.ConsoleApp/Church.ConsoleApp.csproj new file mode 100644 index 0000000..3b45bc9 --- /dev/null +++ b/src/Church/Church.ConsoleApp/Church.ConsoleApp.csproj @@ -0,0 +1,79 @@ + + + + + Debug + AnyCPU + {678CD616-5FCD-4484-B8D1-60C187085B9C} + Exe + Properties + Church.ConsoleApp + Church.ConsoleApp + v4.5.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.6.0.5\lib\net45\Newtonsoft.Json.dll + + + + + + ..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll + + + ..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Primitives.dll + + + + + + + + + ..\packages\Thinktecture.IdentityModel.Client.2.0.0\lib\portable-net45+wp80+win8+wpa81\Thinktecture.IdentityModel.Client.dll + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Church/Church.ConsoleApp/Program.cs b/src/Church/Church.ConsoleApp/Program.cs new file mode 100644 index 0000000..7a82a94 --- /dev/null +++ b/src/Church/Church.ConsoleApp/Program.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Thinktecture.IdentityModel.Client; + +namespace Church.ConsoleApp +{ + class Program + { + static void Main(string[] args) + { + const string tokenUrl = @"http://localhost:12345/token"; + const string resourceUrl = @"http://localhost:12345/api/church/1"; + + var client = new OAuth2Client(new Uri(tokenUrl)); + var tokenResponse = client.RequestResourceOwnerPasswordAsync("dav", "dav").Result; + + Console.WriteLine("got token {0}.", tokenResponse.AccessToken); + + var httpClient = new HttpClient(); + httpClient.SetBearerToken(tokenResponse.AccessToken); + var json = httpClient.GetStringAsync(resourceUrl).Result; + + Console.WriteLine("Result {0}.", json); + + Console.ReadLine(); + } + } +} diff --git a/src/Church/Church.ConsoleApp/Properties/AssemblyInfo.cs b/src/Church/Church.ConsoleApp/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9b3f546 --- /dev/null +++ b/src/Church/Church.ConsoleApp/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Church.ConsoleApp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Church.ConsoleApp")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("069ebbd5-c7bb-4cc3-a05a-3b0d87bcbe1c")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/Church/Church.ConsoleApp/packages.config b/src/Church/Church.ConsoleApp/packages.config new file mode 100644 index 0000000..dc6f935 --- /dev/null +++ b/src/Church/Church.ConsoleApp/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Church/Church.Host.Owin.Core/Authentication/ChurchOAuthAuthorizationServerProvider.cs b/src/Church/Church.Host.Owin.Core/Authentication/ChurchOAuthAuthorizationServerProvider.cs new file mode 100644 index 0000000..a514af4 --- /dev/null +++ b/src/Church/Church.Host.Owin.Core/Authentication/ChurchOAuthAuthorizationServerProvider.cs @@ -0,0 +1,31 @@ +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.Owin.Security.OAuth; + +namespace Church.Host.Owin.Core.Authentication +{ + public class ChurchOAuthAuthorizationServerProvider : OAuthAuthorizationServerProvider + { + public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) + { + context.Validated(); + return Task.FromResult(0); + } + + public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) + { + + if (context.UserName != context.Password) + { + context.Rejected(); + return; + } + + var id = new ClaimsIdentity(context.Options.AuthenticationType); + id.AddClaim(new Claim("sub", context.UserName)); + id.AddClaim(new Claim("role", "user")); + + context.Validated(id); + } + } +} \ No newline at end of file diff --git a/src/Church/Church.Host.Owin.Core/Church.Host.Owin.Core.csproj b/src/Church/Church.Host.Owin.Core/Church.Host.Owin.Core.csproj index 3e79417..a76123e 100644 --- a/src/Church/Church.Host.Owin.Core/Church.Host.Owin.Core.csproj +++ b/src/Church/Church.Host.Owin.Core/Church.Host.Owin.Core.csproj @@ -50,14 +50,22 @@ ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll - - ..\packages\Microsoft.Owin.2.1.0\lib\net45\Microsoft.Owin.dll + + False + ..\packages\Microsoft.Owin.3.0.0\lib\net45\Microsoft.Owin.dll ..\packages\Microsoft.Owin.Host.SystemWeb.2.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll - - ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll + + ..\packages\Microsoft.Owin.Security.3.0.0\lib\net45\Microsoft.Owin.Security.dll + + + ..\packages\Microsoft.Owin.Security.OAuth.3.0.0\lib\net45\Microsoft.Owin.Security.OAuth.dll + + + False + ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll ..\packages\Owin.1.0\lib\net40\Owin.dll @@ -109,6 +117,7 @@ + diff --git a/src/Church/Church.Host.Owin.Core/Controllers/ChurchController.cs b/src/Church/Church.Host.Owin.Core/Controllers/ChurchController.cs index 53d85fd..14c859d 100644 --- a/src/Church/Church.Host.Owin.Core/Controllers/ChurchController.cs +++ b/src/Church/Church.Host.Owin.Core/Controllers/ChurchController.cs @@ -15,6 +15,7 @@ namespace Church.Host.Owin.Core.Controllers { + [Authorize] public class ChurchController : ApiController { private readonly IChurchService _churchService; diff --git a/src/Church/Church.Host.Owin.Core/Startup.cs b/src/Church/Church.Host.Owin.Core/Startup.cs index bd45e6e..f7905f1 100644 --- a/src/Church/Church.Host.Owin.Core/Startup.cs +++ b/src/Church/Church.Host.Owin.Core/Startup.cs @@ -1,11 +1,14 @@ -using System.Web.Http; +using System; +using System.Web.Http; using Church.Common.Extensions; using Church.Common.Logging; using Church.Common.Service; using Church.Common.Settings; using Church.Components.Core; using Church.Components.Core.Repository; +using Church.Host.Owin.Core.Authentication; using Microsoft.Owin; +using Microsoft.Owin.Security.OAuth; using Owin; using SimpleInjector; @@ -23,13 +26,26 @@ public void Configuration(IAppBuilder appBuilder) var container = new Container(); HttpConfiguration = new HttpConfiguration(); - appBuilder.UseWebApi(HttpConfiguration); + MappingConfiguration.Configure(); HttpConfiguration.MapHttpAttributeRoutes(); SetContainer(container); + + //Auth + appBuilder.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions + { + AllowInsecureHttp = true, + TokenEndpointPath = new PathString("/token"), + AccessTokenExpireTimeSpan = TimeSpan.FromHours(1), + Provider = new ChurchOAuthAuthorizationServerProvider() + }); + + appBuilder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); + appBuilder.UseWebApi(HttpConfiguration); + //start IServices var services = _container.GetAllInstances(); services.ForEach(s => s.Start()); diff --git a/src/Church/Church.Host.Owin.Core/Web.config b/src/Church/Church.Host.Owin.Core/Web.config index 38cdaef..f80c540 100644 --- a/src/Church/Church.Host.Owin.Core/Web.config +++ b/src/Church/Church.Host.Owin.Core/Web.config @@ -37,12 +37,16 @@ - + + + + + \ No newline at end of file diff --git a/src/Church/Church.Host.Owin.Core/packages.config b/src/Church/Church.Host.Owin.Core/packages.config index 364186d..cb706b9 100644 --- a/src/Church/Church.Host.Owin.Core/packages.config +++ b/src/Church/Church.Host.Owin.Core/packages.config @@ -5,9 +5,11 @@ - + - + + + diff --git a/src/Church/Church.IntegrationTests/app.config b/src/Church/Church.IntegrationTests/app.config index 59746f0..799ed21 100644 --- a/src/Church/Church.IntegrationTests/app.config +++ b/src/Church/Church.IntegrationTests/app.config @@ -17,6 +17,10 @@ + + + + \ No newline at end of file diff --git a/src/Church/Church.sln b/src/Church/Church.sln index dfb0c1c..600e171 100644 --- a/src/Church/Church.sln +++ b/src/Church/Church.sln @@ -17,6 +17,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Church.Types", "Church.Type EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Church.UnitTests", "Church.UnitTests\Church.UnitTests.csproj", "{CFBC092D-108F-494A-9F52-3A9BD3AC14E4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{A376A92E-A66B-483F-9719-63C6E6A70593}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Church.ConsoleApp", "Church.ConsoleApp\Church.ConsoleApp.csproj", "{678CD616-5FCD-4484-B8D1-60C187085B9C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,10 +55,19 @@ Global {CFBC092D-108F-494A-9F52-3A9BD3AC14E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {CFBC092D-108F-494A-9F52-3A9BD3AC14E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {CFBC092D-108F-494A-9F52-3A9BD3AC14E4}.Release|Any CPU.Build.0 = Release|Any CPU + {678CD616-5FCD-4484-B8D1-60C187085B9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {678CD616-5FCD-4484-B8D1-60C187085B9C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {678CD616-5FCD-4484-B8D1-60C187085B9C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {678CD616-5FCD-4484-B8D1-60C187085B9C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {3BB84EC6-7033-41DF-A9B5-70EFC5532E2C} = {A376A92E-A66B-483F-9719-63C6E6A70593} + {CFBC092D-108F-494A-9F52-3A9BD3AC14E4} = {A376A92E-A66B-483F-9719-63C6E6A70593} + {678CD616-5FCD-4484-B8D1-60C187085B9C} = {A376A92E-A66B-483F-9719-63C6E6A70593} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EnterpriseLibraryConfigurationToolBinariesPathV6 = packages\EnterpriseLibrary.Common.6.0.1304.0\lib\NET45;packages\EnterpriseLibrary.Data.6.0.1304.0\lib\NET45 EndGlobalSection diff --git a/src/Church/packages/repositories.config b/src/Church/packages/repositories.config index 5b0f8fc..1ba81a2 100644 --- a/src/Church/packages/repositories.config +++ b/src/Church/packages/repositories.config @@ -2,6 +2,7 @@ +