forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialsAuthProvider.cs
More file actions
142 lines (119 loc) · 5.29 KB
/
CredentialsAuthProvider.cs
File metadata and controls
142 lines (119 loc) · 5.29 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
using System.Globalization;
using System.Collections.Generic;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.Configuration;
using ServiceStack.FluentValidation;
namespace ServiceStack.ServiceInterface.Auth
{
public class CredentialsAuthProvider : AuthProvider
{
class CredentialsAuthValidator : AbstractValidator<Auth>
{
public CredentialsAuthValidator()
{
RuleFor(x => x.UserName).NotEmpty();
RuleFor(x => x.Password).NotEmpty();
}
}
public static string Name = AuthService.CredentialsProvider;
public static string Realm = "/auth/" + AuthService.CredentialsProvider;
public CredentialsAuthProvider()
{
this.Provider = Name;
this.AuthRealm = Realm;
}
public CredentialsAuthProvider(IResourceManager appSettings, string authRealm, string oAuthProvider)
: base(appSettings, authRealm, oAuthProvider) { }
public CredentialsAuthProvider(IResourceManager appSettings)
: base(appSettings, Realm, Name) { }
public virtual bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var authRepo = authService.TryResolve<IUserAuthRepository>();
if (authRepo == null)
{
Log.WarnFormat("Tried to authenticate without a registered IUserAuthRepository");
return false;
}
var session = authService.GetSession();
UserAuth userAuth = null;
if (authRepo.TryAuthenticate(userName, password, out userAuth))
{
session.PopulateWith(userAuth);
session.IsAuthenticated = true;
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
session.ProviderOAuthAccess = authRepo.GetUserOAuthProviders(session.UserAuthId)
.ConvertAll(x => (IOAuthTokens)x);
return true;
}
return false;
}
public override bool IsAuthorized(IAuthSession session, IOAuthTokens tokens, Auth request=null)
{
if (request != null)
{
if (!LoginMatchesSession(session, request.UserName)) return false;
}
return !session.UserAuthName.IsNullOrEmpty();
}
public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
new CredentialsAuthValidator().ValidateAndThrow(request);
return Authenticate(authService, session, request.UserName, request.Password);
}
protected object Authenticate(IServiceBase authService, IAuthSession session, string userName, string password)
{
if (!LoginMatchesSession(session, userName))
{
authService.RemoveSession();
session = authService.GetSession();
}
if (TryAuthenticate(authService, userName, password))
{
if (session.UserAuthName == null)
session.UserAuthName = userName;
OnAuthenticated(authService, session, null, null);
return new AuthResponse {
UserName = userName,
SessionId = session.Id,
};
}
throw HttpError.Unauthorized("Invalid UserName or Password");
}
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
var userSession = session as AuthUserSession;
if (userSession != null)
{
LoadUserAuthInfo(userSession, tokens, authInfo);
}
var authRepo = authService.TryResolve<IUserAuthRepository>();
if (authRepo != null)
{
if (tokens != null)
{
authInfo.ForEach((x, y) => tokens.Items[x] = y);
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
}
foreach (var oAuthToken in session.ProviderOAuthAccess)
{
var authProvider = AuthService.GetAuthProvider(oAuthToken.Provider);
if (authProvider == null) continue;
var userAuthProvider = authProvider as OAuthProvider;
if (userAuthProvider != null)
{
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
}
}
var httpRes = authService.RequestContext.Get<IHttpResponse>();
if (httpRes != null)
{
httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
}
}
authService.SaveSession(session, SessionExpiry);
session.OnAuthenticated(authService, session, tokens, authInfo);
}
}
}