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
79 lines (65 loc) · 2.7 KB
/
AuthFeature.cs
File metadata and controls
79 lines (65 loc) · 2.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface
{
/// <summary>
/// Enable the authentication feature and configure the AuthService.
/// </summary>
public class AuthFeature : IPlugin
{
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 string HtmlRedirect { 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 AuthFeature(Func<IAuthSession> sessionFactory, IAuthProvider[] authProviders, string htmlRedirect = "~/login")
{
this.sessionFactory = sessionFactory;
this.authProviders = authProviders;
ServiceRoutes = new Dictionary<Type, string[]> {
{ typeof(AuthenticateService), new[]{ "/auth", "/auth/{provider}", "/authenticate", "/authenticate/{provider}", } },
{ typeof(AssignRolesService), new[]{ "/assignroles" } },
{ typeof(UnAssignRolesService), new[]{ "/unassignroles" } },
};
RegisterPlugins = new List<IPlugin> {
new SessionFeature()
};
this.HtmlRedirect = htmlRedirect;
}
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));
}
public static TimeSpan? GetDefaultSessionExpiry()
{
var authProvider = AuthenticateService.AuthProviders.FirstOrDefault() as AuthProvider;
return authProvider == null ? null : authProvider.SessionExpiry;
}
}
}