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
74 lines (63 loc) · 2.56 KB
/
AuthFeature.cs
File metadata and controls
74 lines (63 loc) · 2.56 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
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.FluentValidation.Internal;
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 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)
{
this.sessionFactory = sessionFactory;
this.authProviders = authProviders;
ServiceRoutes = new Dictionary<Type, string[]> {
{ typeof(AuthService), new[]{"/auth", "/auth/{provider}"} },
{ typeof(AssignRolesService), new[]{"/assignroles"} },
{ typeof(UnAssignRolesService), new[]{"/unassignroles"} },
};
RegisterPlugins = new List<IPlugin> {
new SessionFeature()
};
}
public void Register(IAppHost appHost)
{
AuthService.Init(sessionFactory, authProviders);
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 = AuthService.AuthProviders.FirstOrDefault() as AuthProvider;
return authProvider == null ? null : authProvider.SessionExpiry;
}
}
}