1515import org .zstack .header .core .ReturnValueCompletion ;
1616import org .zstack .header .errorcode .ErrorCode ;
1717import org .zstack .header .errorcode .ErrorableValue ;
18+ import org .zstack .header .errorcode .OperationFailureException ;
1819import org .zstack .header .identity .*;
1920import org .zstack .header .identity .login .*;
2021import org .zstack .header .message .APIMessage ;
@@ -407,22 +408,16 @@ public LoginType getLoginType() {
407408 return loginType ;
408409 }
409410
410- @ Override
411- public void login (LoginContext loginContext , ReturnValueCompletion <LoginSessionInfo > completion ) {
411+ public ErrorableValue <AccountThirdPartyAccountSourceRefVO > findAccountThirdPartyAccountSourceRefByName (String ldapLoginName , String ldapLoginPassword ) {
412412 final ErrorableValue <LdapServerVO > currentLdapServer = findCurrentLdapServer ();
413413 if (!currentLdapServer .isSuccess ()) {
414- logger .debug ("failed to login by LDAP: failed to find current LdapServer: " + currentLdapServer .error .getDetails ());
415- completion .fail (err (IdentityErrors .AUTHENTICATION_ERROR ,
416- "Login validation failed in LDAP" ));
417- return ;
414+ return ErrorableValue .ofErrorCode (currentLdapServer .error );
418415 }
419416 final LdapServerVO ldap = currentLdapServer .result ;
420417
421- String ldapLoginName = loginContext .getUsername ();
422- if (!isValid (ldapLoginName , loginContext .getPassword ())) {
423- completion .fail (err (IdentityErrors .AUTHENTICATION_ERROR ,
418+ if (!isValid (ldapLoginName , ldapLoginPassword )) {
419+ return ErrorableValue .ofErrorCode (err (IdentityErrors .AUTHENTICATION_ERROR ,
424420 "Login validation failed in LDAP" ));
425- return ;
426421 }
427422
428423 String dn = createDriver ().getFullUserDn (ldap , ldap .getUsernameProperty (), ldapLoginName );
@@ -432,19 +427,29 @@ public void login(LoginContext loginContext, ReturnValueCompletion<LoginSessionI
432427 .find ();
433428
434429 if (vo == null ) {
435- completion . fail (err (IdentityErrors .AUTHENTICATION_ERROR ,
430+ return ErrorableValue . ofErrorCode (err (IdentityErrors .AUTHENTICATION_ERROR ,
436431 "The ldapUid does not have a binding account." ));
437- return ;
438432 }
433+ return ErrorableValue .of (vo );
434+ }
439435
436+ @ Override
437+ public void login (LoginContext loginContext , ReturnValueCompletion <LoginSessionInfo > completion ) {
438+ final ErrorableValue <AccountThirdPartyAccountSourceRefVO > accountThirdPartyAccountSourceRef = findAccountThirdPartyAccountSourceRefByName (loginContext .getUsername (), loginContext .getPassword ());
439+
440+ if (!accountThirdPartyAccountSourceRef .isSuccess ()) {
441+ completion .fail (accountThirdPartyAccountSourceRef .error );
442+ return ;
443+ }
444+ String accountUuid = accountThirdPartyAccountSourceRef .result .getAccountUuid ();
440445 final AccountState state = Q .New (AccountVO .class )
441- .eq (AccountVO_ .uuid , vo . getAccountUuid () )
446+ .eq (AccountVO_ .uuid , accountUuid )
442447 .select (AccountVO_ .state )
443448 .findValue ();
444449
445450 if (state == null || state == AccountState .Staled ) {
446451 completion .fail (operr (
447- "Account[uuid:%s] Not Found!!!" , vo . getAccountUuid () ));
452+ "Account[uuid:%s] Not Found!!!" , accountUuid ));
448453 return ;
449454 }
450455 if (state == AccountState .Disabled ) {
@@ -453,7 +458,7 @@ public void login(LoginContext loginContext, ReturnValueCompletion<LoginSessionI
453458 }
454459
455460 LoginSessionInfo info = new LoginSessionInfo ();
456- info .setAccountUuid (vo . getAccountUuid () );
461+ info .setAccountUuid (accountUuid );
457462 completion .success (info );
458463 }
459464
@@ -478,11 +483,18 @@ public String getAccountIdByName(String username) {
478483
479484 @ Override
480485 public void collectUserInfoIntoContext (LoginContext loginContext ) {
481- loginContext .setAccountUuid (getAccountIdByName (loginContext .getUsername ()));
486+ ErrorableValue <AccountThirdPartyAccountSourceRefVO > accountThirdPartyAccountSourceRef = findAccountThirdPartyAccountSourceRefByName (loginContext .getUsername (), loginContext .getPassword ());
487+ if (!accountThirdPartyAccountSourceRef .isSuccess ()) {
488+ throw new OperationFailureException (accountThirdPartyAccountSourceRef .error );
489+ }
490+ if (accountThirdPartyAccountSourceRef .result == null ) {
491+ return ;
492+ }
493+ loginContext .setAccountUuid (accountThirdPartyAccountSourceRef .result .getAccountUuid ());
482494 }
483495
484496 @ Override
485497 public List <AdditionalAuthFeature > getRequiredAdditionalAuthFeature () {
486- return Collections . singletonList (LoginAuthConstant .basicLoginControl );
498+ return Arrays . asList (LoginAuthConstant .basicLoginControl , LoginAuthConstant . twoFactor );
487499 }
488500}
0 commit comments