@@ -18,9 +18,19 @@ public CredentialsAuthValidator()
1818 }
1919 }
2020
21+ private class PrivateAuthValidator : AbstractValidator < Authenticate >
22+ {
23+ public PrivateAuthValidator ( )
24+ {
25+ RuleFor ( x => x . UserName ) . NotEmpty ( ) ;
26+ }
27+ }
28+
2129 public static string Name = AuthenticateService . CredentialsProvider ;
2230 public static string Realm = "/auth/" + AuthenticateService . CredentialsProvider ;
2331
32+ public bool SkipPasswordVerificationForPrivateRequests { get ; set ; }
33+
2434 public CredentialsAuthProvider ( )
2535 {
2636 Provider = Name ;
@@ -44,20 +54,25 @@ public virtual bool TryAuthenticate(IServiceBase authService, string userName, s
4454 if ( IsAccountLocked ( authRepo , userAuth ) )
4555 throw new AuthenticationException ( "This account has been locked" ) ;
4656
47- var holdSessionId = session . Id ;
48- session . PopulateWith ( userAuth ) ; //overwrites session.Id
49- session . Id = holdSessionId ;
50- session . IsAuthenticated = true ;
51- session . UserAuthId = userAuth . Id . ToString ( CultureInfo . InvariantCulture ) ;
52- session . ProviderOAuthAccess = authRepo . GetUserAuthDetails ( session . UserAuthId )
53- . ConvertAll ( x => ( IAuthTokens ) x ) ;
57+ PopulateSession ( authRepo , userAuth , session ) ;
5458
5559 return true ;
5660 }
5761
5862 return false ;
5963 }
6064
65+ private static void PopulateSession ( IUserAuthRepository authRepo , IUserAuth userAuth , IAuthSession session )
66+ {
67+ var holdSessionId = session . Id ;
68+ session . PopulateWith ( userAuth ) ; //overwrites session.Id
69+ session . Id = holdSessionId ;
70+ session . IsAuthenticated = true ;
71+ session . UserAuthId = userAuth . Id . ToString ( CultureInfo . InvariantCulture ) ;
72+ session . ProviderOAuthAccess = authRepo . GetUserAuthDetails ( session . UserAuthId )
73+ . ConvertAll ( x => ( IAuthTokens ) x ) ;
74+ }
75+
6176 public override bool IsAuthorized ( IAuthSession session , IAuthTokens tokens , Authenticate request = null )
6277 {
6378 if ( request != null )
@@ -73,6 +88,12 @@ public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Auth
7388
7489 public override object Authenticate ( IServiceBase authService , IAuthSession session , Authenticate request )
7590 {
91+ if ( SkipPasswordVerificationForPrivateRequests && authService . Request . IsPrivateRequest ( ) )
92+ {
93+ new PrivateAuthValidator ( ) . ValidateAndThrow ( request ) ;
94+ return AuthenticatePrivateRequest ( authService , session , request . UserName , request . Password , request . Continue ) ;
95+ }
96+
7697 new CredentialsAuthValidator ( ) . ValidateAndThrow ( request ) ;
7798 return Authenticate ( authService , session , request . UserName , request . Password , request . Continue ) ;
7899 }
@@ -115,6 +136,38 @@ protected object Authenticate(IServiceBase authService, IAuthSession session, st
115136 throw HttpError . Unauthorized ( ErrorMessages . InvalidUsernameOrPassword ) ;
116137 }
117138
139+ protected object AuthenticatePrivateRequest (
140+ IServiceBase authService , IAuthSession session , string userName , string password , string referrerUrl )
141+ {
142+ var authRepo = authService . TryResolve < IAuthRepository > ( ) . AsUserAuthRepository ( authService . GetResolver ( ) ) ;
143+
144+ var userAuth = authRepo . GetUserAuthByUserName ( userName ) ;
145+ if ( userAuth == null )
146+ throw HttpError . Unauthorized ( ErrorMessages . InvalidUsernameOrPassword ) ;
147+
148+ if ( IsAccountLocked ( authRepo , userAuth ) )
149+ throw new AuthenticationException ( "This account has been locked" ) ;
150+
151+ PopulateSession ( authRepo , userAuth , session ) ;
152+
153+ session . IsAuthenticated = true ;
154+
155+ if ( session . UserAuthName == null )
156+ session . UserAuthName = userName ;
157+
158+ var response = OnAuthenticated ( authService , session , null , null ) ;
159+ if ( response != null )
160+ return response ;
161+
162+ return new AuthenticateResponse
163+ {
164+ UserId = session . UserAuthId ,
165+ UserName = userName ,
166+ SessionId = session . Id ,
167+ ReferrerUrl = referrerUrl
168+ } ;
169+ }
170+
118171 public override IHttpResult OnAuthenticated ( IServiceBase authService , IAuthSession session , IAuthTokens tokens , Dictionary < string , string > authInfo )
119172 {
120173 var userSession = session as AuthUserSession ;
0 commit comments