1- using System . Globalization ;
1+ using System ;
22using System . Collections . Generic ;
3- using ServiceStack . Host ;
3+ using System . Globalization ;
4+ using System . Net ;
45using ServiceStack . Configuration ;
5- using ServiceStack . FluentValidation ;
6+ using ServiceStack . Host ;
67using ServiceStack . Text ;
7- using System ;
8- using System . Net ;
98using ServiceStack . Web ;
109
1110namespace 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