Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // vim:sts=2:sw=2:expandtab
- public class MyLogin
- {
- public void login(Request request,
- String login,
- String password)
- {
- if( login == null || password == null )
- respond(UNAUTHORIZED, "You should provide login and password");
- else
- doLogin(request, login, password);
- }
- private final void respond(int code, String description) {}
- private static final int UNAUTHORIZED = 1;
- private static final int FORBIDDEN = 2;
- private void doLogin(Request request, String login, String password)
- {
- UserInfo user = AccountsStorage.find(login);
- if( user == null )
- handleUnknownUserLogin();
- else
- (new KnownUserLoginHandler(request, login, password, user)).handle();
- }
- private void handleUnknownUserLogin()
- {
- respond(UNAUTHORIZED, "Unknown user");
- }
- private class KnownUserLoginHandler
- {
- Request request;
- String login;
- String password;
- UserInfo user;
- KnownUserLoginHandler(
- Request request,
- String login,
- String password,
- UserInfo user)
- {
- this.request = request;
- this.login = login;
- this.password = password;
- this.user = user;
- }
- public void handle()
- {
- if( user.inactive )
- handleInactiveUserLogin();
- else
- if( user.authScheme.equals( "PETRIVKA" ) )
- handlePetrivkaAuthSchemeLogin();
- else
- handleUsualAuthSchemeLogin();
- }
- private void handleInactiveUserLogin()
- {
- respond(UNAUTHORIZED, "Account is inactive");
- }
- private void handlePetrivkaAuthSchemeLogin()
- {
- if( user.passwordMatches(password) )
- setupSuccessfulAuthResult();
- else
- respond(UNAUTHORIZED, "Authentication failed");
- }
- private void handleUsualAuthSchemeLogin()
- {
- AuthScheme scheme = findAuthScheme();
- if( scheme == null )
- respond(FORBIDDEN, "Unknown authentication scheme: " + user.authScheme);
- else
- {
- int waitFor = 0;
- if( waitFor == 0 )
- setupSuccessfulAuthResult();
- else
- respond(UNAUTHORIZED, "Authentication within failed");
- }
- }
- private void setupSuccessfulAuthResult()
- {
- request.storeInSession("user", user );
- }
- private AuthScheme findAuthScheme()
- {
- return null;
- }
- }
- }
- class Request
- {
- final void storeInSession(String key, Object what) {}
- }
- class AccountsStorage
- {
- static UserInfo find(String name) { return null; }
- }
- class UserInfo
- {
- boolean inactive;
- String authScheme;
- final boolean passwordMatches(String psw) { return false; }
- }
- class AccessStorage
- {
- static AuthScheme find(String name) { return null; }
- }
- class AuthScheme
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement