This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
/
Startup.cs
148 lines (129 loc) · 5.54 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Autofac;
using BrockAllen.MembershipReboot;
using BrockAllen.MembershipReboot.Ef;
using BrockAllen.MembershipReboot.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace ServerApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureMembershipReboot(app);
var oauthServerConfig = new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
Provider = new MyProvider(),
TokenEndpointPath = new PathString("/token")
};
app.UseOAuthAuthorizationServer(oauthServerConfig);
var oauthConfig = new Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationOptions
{
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = "Bearer"
};
app.UseOAuthBearerAuthentication(oauthConfig);
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
private static void ConfigureMembershipReboot(IAppBuilder app)
{
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DefaultMembershipRebootDatabase, BrockAllen.MembershipReboot.Ef.Migrations.Configuration>());
var builder = new ContainerBuilder();
var config = new MembershipRebootConfiguration();
// just for giggles, we'll use the multi-tenancy to keep
// client authentication separate from user authentication
config.MultiTenant = true;
config.RequireAccountVerification = false;
builder.RegisterInstance(config);
builder.RegisterType<DefaultMembershipRebootDatabase>()
.InstancePerLifetimeScope();
builder.RegisterType<DefaultUserAccountRepository>()
.As<IUserAccountRepository>()
.As<IUserAccountQuery>()
.InstancePerLifetimeScope();
builder.RegisterType<UserAccountService>()
.InstancePerLifetimeScope();
var container = builder.Build();
app.Use(async (ctx, next) =>
{
using (var scope = container.BeginLifetimeScope())
{
ctx.Environment.SetUserAccountService(() => scope.Resolve<UserAccountService>());
await next();
}
});
PopulateTestData(container);
}
private static void PopulateTestData(IContainer container)
{
using(var scope = container.BeginLifetimeScope())
{
var svc = scope.Resolve<UserAccountService>();
if (svc.GetByUsername("clients", "client") == null)
{
var client = svc.CreateAccount("clients", "client", "secret", (string)null);
svc.AddClaim(client.ID, "scope", "foo");
svc.AddClaim(client.ID, "scope", "bar");
}
if (svc.GetByUsername("users", "alice") == null)
{
var alice = svc.CreateAccount("users", "alice", "pass", "alice@alice.com");
svc.AddClaim(alice.ID, "role", "people");
}
}
}
}
public class MyProvider : OAuthAuthorizationServerProvider
{
public override System.Threading.Tasks.Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
string cid, csecret;
if (context.TryGetBasicCredentials(out cid, out csecret))
{
var svc = context.OwinContext.Environment.GetUserAccountService<UserAccount>();
if (svc.Authenticate("clients", cid, csecret))
{
context.Validated();
}
}
return Task.FromResult<object>(null);
}
public override Task ValidateTokenRequest(OAuthValidateTokenRequestContext context)
{
if (context.TokenRequest.IsResourceOwnerPasswordCredentialsGrantType)
{
var svc = context.OwinContext.Environment.GetUserAccountService<UserAccount>();
var client = svc.GetByUsername("clients", context.ClientContext.ClientId);
var scopes = context.TokenRequest.ResourceOwnerPasswordCredentialsGrant.Scope;
if (scopes.All(scope=>client.HasClaim("scope", scope)))
{
context.Validated();
}
}
return Task.FromResult<object>(null);
}
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var svc = context.OwinContext.Environment.GetUserAccountService<UserAccount>();
UserAccount user;
if (svc.Authenticate("users", context.UserName, context.Password, out user))
{
var claims = user.GetAllClaims();
var id = new System.Security.Claims.ClaimsIdentity(claims, "MembershipReboot");
context.Validated(id);
}
return base.GrantResourceOwnerCredentials(context);
}
}
}