]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java
unencrypted ... better clear-text ...
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / login / AddressbookUserLoginSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.login;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
21 import javax.ejb.Stateless;
22 import org.mxchange.jcoreee.database.BaseDatabaseBean;
23 import org.mxchange.jusercore.container.login.LoginContainer;
24 import org.mxchange.jusercore.exceptions.UserNotFoundException;
25 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
26 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
27 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
28 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
29 import org.mxchange.jusercore.model.user.User;
30 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
31 import org.mxchange.jusercore.model.user.UserUtils;
32 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
33
34 /**
35  * A session EJB for user logins
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "login", description = "A bean handling the user login for Addressbook project")
40 public class AddressbookUserLoginSessionBean extends BaseDatabaseBean implements UserLoginSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 21_785_978_127_581_965L;
46
47         /**
48          * Registration EJB
49          */
50         @EJB
51         private UserRegistrationSessionBeanRemote registerBean;
52
53         /**
54          * User EJB
55          */
56         @EJB
57         private UserSessionBeanRemote userBean;
58
59         @Override
60         public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N
63
64                 // Check some beans
65                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
66                 assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N
67
68                 // user should not be null
69                 if (null == container) {
70                         // Abort here
71                         throw new NullPointerException("container is null"); //NOI18N
72                 } else if (container.getUser() == null) {
73                         // NPE again
74                         throw new NullPointerException("container.user is null"); //NOI18N
75                 } else if (container.getUserPassword() == null) {
76                         // And yet again NPE
77                         throw new NullPointerException("container.userPassword is null"); //NOI18N
78                 } else if (container.getUserPassword().isEmpty()) {
79                         // Empty password is not allowed, hardcoded.
80                         throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
81                 }
82
83                 // Is the account there?
84                 if (!this.registerBean.isUserNameRegistered(container.getUser())) {
85                         // Not registered
86                         throw new UserNotFoundException(container.getUser());
87                 }
88
89                 // Get user instance from persistance
90                 User updatedUser = this.userBean.fillUserData(container.getUser());
91
92                 // Debug message
93                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N
94
95                 // Is the user account unconfirmed?
96                 if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) {
97                         // Is unconfirmed
98                         throw new UserStatusUnconfirmedException(container.getUser());
99                 } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) {
100                         // Is locked
101                         throw new UserStatusLockedException(container.getUser());
102                 } else if (!this.isPasswordMatching(container, updatedUser)) {
103                         // Not matcing passwords
104                         throw new UserPasswordMismatchException(container.getUser());
105                 }
106
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
109
110                 // Return it
111                 return updatedUser;
112         }
113
114         /**
115          * Checks if password matches of both instances. Both user instances must
116          * not match, the first one is the one from the calling bean/controller, the
117          * second is the from database.
118          * <p>
119          * @param container Container instance holding the user instance and
120          * clear-text password
121          * @param updatedUser Updated user instance found for given user name
122          * <p>
123          * @return Whether the password matches
124          */
125         private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) {
126                 // First math both instances
127                 if (null == container) {
128                         // Throw NPE
129                         throw new NullPointerException("container is null"); //NOI18N
130                 } else if (null == updatedUser) {
131                         // Throw NPE
132                         throw new NullPointerException("updatedUser is null"); //NOI18N
133                 } else if (container.getUser().equals(updatedUser)) {
134                         // Both same instance!
135                         throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N
136                 }
137
138                 // Is it the same same password?
139                 return UserUtils.ifPasswordMatches(container, updatedUser);
140         }
141
142 }