Skip to content

Commit 2c3e022

Browse files
committed
Added initial support for custom UserAuth
1 parent 40d3c80 commit 2c3e022

24 files changed

Lines changed: 494 additions & 467 deletions

src/ServiceStack.Mvc/ExecuteServiceStackFiltersAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
2929
if (roleAttrs.Count + anyRoleAttrs.Count + permAttrs.Count + anyPermAttrs.Count == 0) return;
3030

3131
var httpReq = HttpContext.Current.Request.ToRequest();
32-
var userAuthRepo = httpReq.TryResolve<IUserAuthRepository>();
32+
var userAuthRepo = httpReq.TryResolve<IAuthRepository>();
3333

3434
var hasRoles = roleAttrs.All(x => x.HasAllRoles(httpReq, ssController.AuthSession, userAuthRepo));
3535
if (!hasRoles)

src/ServiceStack.Server/Auth/OrmLiteAuthRepository.cs

Lines changed: 147 additions & 111 deletions
Large diffs are not rendered by default.

src/ServiceStack/Auth/AssignRolesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public AssignRolesResponse()
3939
[DefaultRequest(typeof(AssignRoles))]
4040
public class AssignRolesService : Service
4141
{
42-
public IUserAuthRepository UserAuthRepo { get; set; }
42+
public IAuthRepository UserAuthRepo { get; set; }
4343

4444
public object Post(AssignRoles request)
4545
{

src/ServiceStack/Auth/AuthProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public virtual object Logout(IServiceBase service, Authenticate request)
8080
/// Saves the Auth Tokens for this request. Called in OnAuthenticated().
8181
/// Overrideable, the default behaviour is to call IUserAuthRepository.CreateOrMergeAuthSession().
8282
/// </summary>
83-
protected virtual void SaveUserAuth(IServiceBase authService, IAuthSession session, IUserAuthRepository authRepo, IAuthTokens tokens)
83+
protected virtual void SaveUserAuth(IServiceBase authService, IAuthSession session, IAuthRepository authRepo, IAuthTokens tokens)
8484
{
8585
if (authRepo == null) return;
8686
if (tokens != null)
@@ -121,7 +121,7 @@ public virtual void OnAuthenticated(IServiceBase authService, IAuthSession sessi
121121
LoadUserAuthInfo(userSession, tokens, authInfo);
122122
}
123123

124-
var authRepo = authService.TryResolve<IUserAuthRepository>();
124+
var authRepo = authService.TryResolve<IAuthRepository>();
125125
if (authRepo != null)
126126
{
127127
if (tokens != null)
Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Globalization;
21
using System.Collections.Generic;
2+
using System.Globalization;
33
using ServiceStack.Configuration;
44
using ServiceStack.FluentValidation;
55
using ServiceStack.Text;
@@ -9,7 +9,7 @@ namespace ServiceStack.Auth
99
{
1010
public class CredentialsAuthProvider : AuthProvider
1111
{
12-
class CredentialsAuthValidator : AbstractValidator<Authenticate>
12+
private class CredentialsAuthValidator : AbstractValidator<Authenticate>
1313
{
1414
public CredentialsAuthValidator()
1515
{
@@ -23,34 +23,32 @@ public CredentialsAuthValidator()
2323

2424
public CredentialsAuthProvider()
2525
{
26-
this.Provider = Name;
27-
this.AuthRealm = Realm;
26+
Provider = Name;
27+
AuthRealm = Realm;
2828
}
2929

3030
public CredentialsAuthProvider(IAppSettings appSettings, string authRealm, string oAuthProvider)
31-
: base(appSettings, authRealm, oAuthProvider) { }
31+
: base(appSettings, authRealm, oAuthProvider) {}
3232

3333
public CredentialsAuthProvider(IAppSettings appSettings)
34-
: base(appSettings, Realm, Name) { }
34+
: base(appSettings, Realm, Name) {}
3535

3636
public virtual bool TryAuthenticate(IServiceBase authService, string userName, string password)
3737
{
38-
var authRepo = authService.TryResolve<IUserAuthRepository>();
39-
if (authRepo == null)
40-
{
38+
var authRepo = authService.TryResolve<IAuthRepository>();
39+
if (authRepo == null) {
4140
Log.WarnFormat("Tried to authenticate without a registered IUserAuthRepository");
4241
return false;
4342
}
4443

4544
var session = authService.GetSession();
46-
UserAuth userAuth = null;
47-
if (authRepo.TryAuthenticate(userName, password, out userAuth))
48-
{
45+
IUserAuth userAuth;
46+
if (authRepo.TryAuthenticate(userName, password, out userAuth)) {
4947
session.PopulateWith(userAuth);
5048
session.IsAuthenticated = true;
51-
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
49+
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
5250
session.ProviderOAuthAccess = authRepo.GetUserOAuthProviders(session.UserAuthId)
53-
.ConvertAll(x => (IAuthTokens)x);
51+
.ConvertAll(x => (IAuthTokens) x);
5452

5553
return true;
5654
}
@@ -59,9 +57,10 @@ public virtual bool TryAuthenticate(IServiceBase authService, string userName, s
5957

6058
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
6159
{
62-
if (request != null)
63-
{
64-
if (!LoginMatchesSession(session, request.UserName)) return false;
60+
if (request != null) {
61+
if (!LoginMatchesSession(session, request.UserName)) {
62+
return false;
63+
}
6564
}
6665

6766
return !session.UserAuthName.IsNullOrEmpty();
@@ -80,17 +79,16 @@ protected object Authenticate(IServiceBase authService, IAuthSession session, st
8079

8180
protected object Authenticate(IServiceBase authService, IAuthSession session, string userName, string password, string referrerUrl)
8281
{
83-
if (!LoginMatchesSession(session, userName))
84-
{
82+
if (!LoginMatchesSession(session, userName)) {
8583
authService.RemoveSession();
8684
session = authService.GetSession();
8785
}
8886

89-
if (TryAuthenticate(authService, userName, password))
90-
{
91-
if (session.UserAuthName == null)
87+
if (TryAuthenticate(authService, userName, password)) {
88+
if (session.UserAuthName == null) {
9289
session.UserAuthName = userName;
93-
90+
}
91+
9492
OnAuthenticated(authService, session, null, null);
9593

9694
return new AuthenticateResponse {
@@ -102,46 +100,40 @@ protected object Authenticate(IServiceBase authService, IAuthSession session, st
102100

103101
throw HttpError.Unauthorized("Invalid UserName or Password");
104102
}
105-
103+
106104
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
107105
{
108106
var userSession = session as AuthUserSession;
109-
if (userSession != null)
110-
{
107+
if (userSession != null) {
111108
LoadUserAuthInfo(userSession, tokens, authInfo);
112109
}
113110

114-
var authRepo = authService.TryResolve<IUserAuthRepository>();
115-
if (authRepo != null)
116-
{
117-
if (tokens != null)
118-
{
111+
var authRepo = authService.TryResolve<IAuthRepository>();
112+
if (authRepo != null) {
113+
if (tokens != null) {
119114
authInfo.ForEach((x, y) => tokens.Items[x] = y);
120115
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
121116
}
122-
123-
foreach (var oAuthToken in session.ProviderOAuthAccess)
124-
{
117+
118+
foreach (var oAuthToken in session.ProviderOAuthAccess) {
125119
var authProvider = AuthenticateService.GetAuthProvider(oAuthToken.Provider);
126-
if (authProvider == null) continue;
120+
if (authProvider == null) {
121+
continue;
122+
}
127123
var userAuthProvider = authProvider as OAuthProvider;
128-
if (userAuthProvider != null)
129-
{
124+
if (userAuthProvider != null) {
130125
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
131126
}
132127
}
133-
128+
134129
var httpRes = authService.RequestContext.Get<IHttpResponse>();
135-
if (httpRes != null)
136-
{
130+
if (httpRes != null) {
137131
httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
138132
}
139-
140133
}
141134

142135
authService.SaveSession(session, SessionExpiry);
143136
session.OnAuthenticated(authService, session, tokens, authInfo);
144137
}
145-
146138
}
147-
}
139+
}
Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
using System.Globalization;
1+
using System;
22
using System.Collections.Generic;
3-
using ServiceStack.Host;
3+
using System.Globalization;
4+
using System.Net;
45
using ServiceStack.Configuration;
5-
using ServiceStack.FluentValidation;
6+
using ServiceStack.Host;
67
using ServiceStack.Text;
7-
using System;
8-
using System.Net;
98
using ServiceStack.Web;
109

1110
namespace ServiceStack.Auth
1211
{
1312
public class DigestAuthProvider : AuthProvider
1413
{
15-
class DigestAuthValidator : AbstractValidator<Authenticate>
16-
{
17-
public DigestAuthValidator()
18-
{
19-
RuleFor(x => x.UserName).NotEmpty();
20-
RuleFor(x => x.Password).NotEmpty();
21-
}
22-
}
14+
//private class DigestAuthValidator : AbstractValidator<Authenticate>
15+
//{
16+
// public DigestAuthValidator()
17+
// {
18+
// RuleFor(x => x.UserName).NotEmpty();
19+
// RuleFor(x => x.Password).NotEmpty();
20+
// }
21+
//}
2322

2423
public static string Name = AuthenticateService.DigestProvider;
2524
public static string Realm = "/auth/" + AuthenticateService.DigestProvider;
2625
public static int NonceTimeOut = 600;
2726
public string PrivateKey;
2827
public IAppSettings AppSettings { get; set; }
28+
2929
public DigestAuthProvider()
3030
{
31-
this.Provider = Name;
31+
Provider = Name;
3232
PrivateKey = Guid.NewGuid().ToString();
33-
this.AuthRealm = Realm;
33+
AuthRealm = Realm;
3434
}
35+
3536
public DigestAuthProvider(IAppSettings appSettings, string authRealm, string oAuthProvider)
3637
: base(appSettings, authRealm, oAuthProvider) { }
3738

@@ -40,35 +41,34 @@ public DigestAuthProvider(IAppSettings appSettings)
4041

4142
public virtual bool TryAuthenticate(IServiceBase authService, string userName, string password)
4243
{
43-
var authRepo = authService.TryResolve<IUserAuthRepository>();
44-
if (authRepo == null)
45-
{
44+
var authRepo = authService.TryResolve<IAuthRepository>();
45+
if (authRepo == null) {
4646
Log.WarnFormat("Tried to authenticate without a registered IUserAuthRepository");
4747
return false;
4848
}
4949

5050
var session = authService.GetSession();
5151
var digestInfo = authService.RequestContext.Get<IHttpRequest>().GetDigestAuth();
52-
UserAuth userAuth = null;
53-
if (authRepo.TryAuthenticate(digestInfo,PrivateKey,NonceTimeOut, session.Sequence, out userAuth))
54-
{
52+
IUserAuth userAuth;
53+
if (authRepo.TryAuthenticate(digestInfo, PrivateKey, NonceTimeOut, session.Sequence, out userAuth)) {
5554
session.PopulateWith(userAuth);
5655
session.IsAuthenticated = true;
5756
session.Sequence = digestInfo["nc"];
5857
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
5958
session.ProviderOAuthAccess = authRepo.GetUserOAuthProviders(session.UserAuthId)
60-
.ConvertAll(x => (IAuthTokens)x);
61-
59+
.ConvertAll(x => (IAuthTokens) x);
60+
6261
return true;
6362
}
6463
return false;
6564
}
6665

6766
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
6867
{
69-
if (request != null)
70-
{
71-
if (!LoginMatchesSession(session, request.UserName)) return false;
68+
if (request != null) {
69+
if (!LoginMatchesSession(session, request.UserName)) {
70+
return false;
71+
}
7272
}
7373

7474
return !session.UserAuthName.IsNullOrEmpty();
@@ -82,21 +82,19 @@ public override object Authenticate(IServiceBase authService, IAuthSession sessi
8282

8383
protected object Authenticate(IServiceBase authService, IAuthSession session, string userName, string password)
8484
{
85-
if (!LoginMatchesSession(session, userName))
86-
{
85+
if (!LoginMatchesSession(session, userName)) {
8786
authService.RemoveSession();
8887
session = authService.GetSession();
8988
}
9089

91-
if (TryAuthenticate(authService, userName, password))
92-
{
93-
if (session.UserAuthName == null)
90+
if (TryAuthenticate(authService, userName, password)) {
91+
if (session.UserAuthName == null) {
9492
session.UserAuthName = userName;
93+
}
9594

9695
OnAuthenticated(authService, session, null, null);
9796

98-
return new AuthenticateResponse
99-
{
97+
return new AuthenticateResponse {
10098
UserName = userName,
10199
SessionId = session.Id,
102100
};
@@ -108,27 +106,24 @@ protected object Authenticate(IServiceBase authService, IAuthSession session, st
108106
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
109107
{
110108
var userSession = session as AuthUserSession;
111-
if (userSession != null)
112-
{
109+
if (userSession != null) {
113110
LoadUserAuthInfo(userSession, tokens, authInfo);
114111
}
115112

116-
var authRepo = authService.TryResolve<IUserAuthRepository>();
117-
if (authRepo != null)
118-
{
119-
if (tokens != null)
120-
{
113+
var authRepo = authService.TryResolve<IAuthRepository>();
114+
if (authRepo != null) {
115+
if (tokens != null) {
121116
authInfo.ForEach((x, y) => tokens.Items[x] = y);
122117
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
123118
}
124119

125-
foreach (var oAuthToken in session.ProviderOAuthAccess)
126-
{
120+
foreach (var oAuthToken in session.ProviderOAuthAccess) {
127121
var authProvider = AuthenticateService.GetAuthProvider(oAuthToken.Provider);
128-
if (authProvider == null) continue;
122+
if (authProvider == null) {
123+
continue;
124+
}
129125
var userAuthProvider = authProvider as OAuthProvider;
130-
if (userAuthProvider != null)
131-
{
126+
if (userAuthProvider != null) {
132127
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
133128
}
134129
}
@@ -138,18 +133,20 @@ public override void OnAuthenticated(IServiceBase authService, IAuthSession sess
138133
//{
139134
// httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
140135
//}
141-
142136
}
143137

144138
authService.SaveSession(session, SessionExpiry);
145139
session.OnAuthenticated(authService, session, tokens, authInfo);
146140
}
141+
147142
public override void OnFailedAuthentication(IAuthSession session, IHttpRequest httpReq, IHttpResponse httpRes)
148143
{
149144
var digestHelper = new DigestAuthFunctions();
150-
httpRes.StatusCode = (int)HttpStatusCode.Unauthorized;
151-
httpRes.AddHeader(HttpHeaders.WwwAuthenticate, "{0} realm=\"{1}\", nonce=\"{2}\", qop=\"auth\"".Fmt(Provider, AuthRealm,digestHelper.GetNonce(httpReq.UserHostAddress,PrivateKey)));
145+
httpRes.StatusCode = (int) HttpStatusCode.Unauthorized;
146+
httpRes.AddHeader(
147+
HttpHeaders.WwwAuthenticate,
148+
"{0} realm=\"{1}\", nonce=\"{2}\", qop=\"auth\"".Fmt(Provider, AuthRealm, digestHelper.GetNonce(httpReq.UserHostAddress, PrivateKey)));
152149
httpRes.EndRequest();
153150
}
154151
}
155-
}
152+
}

0 commit comments

Comments
 (0)