Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.99 KB | None | 0 0
  1. // vim:sts=2:sw=2:expandtab
  2. public class MyLogin
  3. {
  4.   public void login(Request request,
  5.     String login,
  6.     String password)
  7.   {
  8.     if( login == null || password == null )
  9.       respond(UNAUTHORIZED, "You should provide login and password");
  10.     else
  11.       doLogin(request, login, password);
  12.   }
  13.  
  14.   private final void respond(int code, String description) {}
  15.  
  16.   private static final int UNAUTHORIZED = 1;
  17.   private static final int FORBIDDEN = 2;
  18.  
  19.   private void doLogin(Request request, String login, String password)
  20.   {
  21.      UserInfo user = AccountsStorage.find(login);
  22.      if( user == null )
  23.         handleUnknownUserLogin();
  24.      else
  25.         (new KnownUserLoginHandler(request, login, password, user)).handle();
  26.   }
  27.  
  28.   private void handleUnknownUserLogin()
  29.   {
  30.     respond(UNAUTHORIZED, "Unknown user");
  31.   }
  32.  
  33.   private class KnownUserLoginHandler
  34.   {
  35.      Request request;
  36.      String login;
  37.      String password;
  38.      UserInfo user;
  39.  
  40.      KnownUserLoginHandler(
  41.            Request request,
  42.            String login,
  43.            String password,
  44.            UserInfo user)
  45.         {
  46.            this.request = request;
  47.            this.login = login;
  48.            this.password = password;
  49.            this.user = user;
  50.         }
  51.  
  52.      public void handle()
  53.      {
  54.         if( user.inactive )
  55.            handleInactiveUserLogin();
  56.         else
  57.            if( user.authScheme.equals( "PETRIVKA" ) )
  58.               handlePetrivkaAuthSchemeLogin();
  59.            else
  60.               handleUsualAuthSchemeLogin();
  61.      }
  62.  
  63.      private void handleInactiveUserLogin()
  64.      {
  65.         respond(UNAUTHORIZED, "Account is inactive");
  66.      }
  67.  
  68.      private void handlePetrivkaAuthSchemeLogin()
  69.      {
  70.         if( user.passwordMatches(password) )
  71.            setupSuccessfulAuthResult();
  72.         else
  73.            respond(UNAUTHORIZED, "Authentication failed");
  74.      }
  75.  
  76.      private void handleUsualAuthSchemeLogin()
  77.      {
  78.         AuthScheme scheme = findAuthScheme();
  79.         if( scheme == null )
  80.            respond(FORBIDDEN, "Unknown authentication scheme: " + user.authScheme);
  81.         else
  82.         {
  83.           int waitFor = 0;
  84.           if( waitFor == 0 )
  85.              setupSuccessfulAuthResult();
  86.           else
  87.              respond(UNAUTHORIZED, "Authentication within failed");
  88.         }
  89.      }
  90.  
  91.      private void setupSuccessfulAuthResult()
  92.      {
  93.         request.storeInSession("user", user );
  94.      }
  95.      
  96.      private AuthScheme findAuthScheme()
  97.      {
  98.         return null;
  99.      }
  100.   }
  101. }
  102.  
  103. class Request
  104. {
  105.   final void storeInSession(String key, Object what) {}
  106. }
  107.  
  108. class AccountsStorage
  109. {
  110.    static UserInfo find(String name) { return null; }
  111. }
  112.  
  113. class UserInfo
  114. {
  115.   boolean inactive;
  116.   String authScheme;
  117.  
  118.   final boolean passwordMatches(String psw) { return false; }
  119. }
  120.  
  121. class AccessStorage
  122. {
  123.   static AuthScheme find(String name) { return null; }
  124. }
  125.  
  126. class AuthScheme
  127. {
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement