forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthFeature.cs
More file actions
147 lines (121 loc) · 5.06 KB
/
Copy pathAuthFeature.cs
File metadata and controls
147 lines (121 loc) · 5.06 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Auth;
namespace ServiceStack
{
/// <summary>
/// Enable the authentication feature and configure the AuthService.
/// </summary>
public class AuthFeature : IPlugin, IPostInitPlugin
{
public static bool AddUserIdHttpHeader = true;
private readonly Func<IAuthSession> sessionFactory;
private readonly IAuthProvider[] authProviders;
public Dictionary<Type, string[]> ServiceRoutes { get; set; }
public List<IPlugin> RegisterPlugins { get; set; }
public List<IAuthEvents> AuthEvents { get; set; }
public string HtmlRedirect { get; set; }
public bool IncludeAuthMetadataProvider { get; set; }
public bool ValidateUniqueEmails { get; set; }
public bool ValidateUniqueUserNames { get; set; }
public bool IncludeAssignRoleServices
{
set
{
if (!value)
{
(from registerService in ServiceRoutes
where registerService.Key == typeof(AssignRolesService)
|| registerService.Key == typeof(UnAssignRolesService)
select registerService.Key).ToList()
.ForEach(x => ServiceRoutes.Remove(x));
}
}
}
public bool IncludeRegistrationService
{
set
{
if (value)
{
if (!RegisterPlugins.Any(x => x is RegistrationFeature))
{
RegisterPlugins.Add(new RegistrationFeature());
}
}
}
}
public AuthFeature(Func<IAuthSession> sessionFactory, IAuthProvider[] authProviders, string htmlRedirect = null)
{
this.sessionFactory = sessionFactory;
this.authProviders = authProviders;
Func<string,string> localize = s => HostContext.AppHost.ResolveLocalizedString(s, null);
ServiceRoutes = new Dictionary<Type, string[]> {
{ typeof(AuthenticateService), new[]
{
"/" + localize(LocalizedStrings.Auth),
"/" + localize(LocalizedStrings.Auth) + "/{provider}",
"/" + localize(LocalizedStrings.Authenticate),
"/" + localize(LocalizedStrings.Authenticate) + "/{provider}",
} },
{ typeof(AssignRolesService), new[]{ "/" + localize(LocalizedStrings.AssignRoles) } },
{ typeof(UnAssignRolesService), new[]{ "/" + localize(LocalizedStrings.UnassignRoles) } },
};
RegisterPlugins = new List<IPlugin> {
new SessionFeature()
};
AuthEvents = new List<IAuthEvents>();
this.HtmlRedirect = htmlRedirect ?? "~/" + localize(LocalizedStrings.Login);
this.IncludeAuthMetadataProvider = true;
this.ValidateUniqueEmails = true;
}
public void Register(IAppHost appHost)
{
AuthenticateService.Init(sessionFactory, authProviders);
AuthenticateService.HtmlRedirect = HtmlRedirect;
var unitTest = appHost == null;
if (unitTest) return;
foreach (var registerService in ServiceRoutes)
{
appHost.RegisterService(registerService.Key, registerService.Value);
}
RegisterPlugins.ForEach(x => appHost.LoadPlugin(x));
if (IncludeAuthMetadataProvider && appHost.TryResolve<IAuthMetadataProvider>() == null)
appHost.Register<IAuthMetadataProvider>(new AuthMetadataProvider());
}
public TimeSpan GetDefaultSessionExpiry()
{
var authProvider = authProviders.FirstOrDefault() as AuthProvider;
return authProvider != null
? authProvider.SessionExpiry
: SessionFeature.DefaultSessionExpiry;
}
public void AfterPluginsLoaded(IAppHost appHost)
{
var authEvents = appHost.TryResolve<IAuthEvents>();
if (authEvents == null)
{
authEvents = AuthEvents.Count == 0
? new AuthEvents() :
AuthEvents.Count == 1
? AuthEvents.First()
: new MultiAuthEvents(AuthEvents);
appHost.GetContainer().Register<IAuthEvents>(authEvents);
}
else if (AuthEvents.Count > 0)
{
throw new Exception("Registering IAuthEvents via both AuthFeature.AuthEvents and IOC is not allowed");
}
}
}
public static class AuthFeatureExtensions
{
public static string GetHtmlRedirect(this AuthFeature feature)
{
if (feature != null)
return feature.HtmlRedirect;
return "~/" + HostContext.ResolveLocalizedString(LocalizedStrings.Login);
}
}
}