BrightSide Workbench Full Report + Source Code
Authentication.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.auth;
19 
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.servlet.http.HttpSession;
26 import org.amic.util.date.CheckDate;
27 import org.turro.string.Strings;
28 import org.apache.commons.mail.EmailException;
29 import org.turro.action.Actions;
30 import org.turro.action.Contacts;
31 import static org.turro.action.Contacts.BEHAVEAS_ICONTACT;
32 import org.turro.action.MailSenders;
33 import org.turro.action.Secrets;
34 import org.turro.action.UserSummaries;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.elephant.security.IUser;
39 import org.turro.external.Authentications;
40 import org.turro.external.IElephantAuthentication;
41 import org.turro.i18n.I_;
42 import org.turro.log.SystemLogType;
43 import org.turro.log.SystemLogger;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.sso.SSO;
46 
51 public class Authentication {
52 
53  public static void doLogin(String login, String pass, String redir, Object extra) throws IOException {
55  IContact user = resolveUser(login, pass, extra);
56  if (user != null) {
57  constructor.setMaxInactiveInterval(1 * 60 * 60);
58  constructor.setSessionAttribute(IUser.LOGGED_USER, user);
59  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/in", null, null);
60  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.TRUE);
61  if (user instanceof IContact) {
62  constructor.setSessionAttribute(Contacts.LOGGED_ICONTACT, user);
63  } else {
65  }
66  if(SSO.hasSSO()) {
67  SSO.getSSO().createAssertion(constructor.getRequest(), constructor.getResponse(),
69  }
70  } else {
71  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/failed", login, null);
72  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.FALSE);
75  }
76  Application.getApplication().sendRedirect(redir == null ? constructor.getLastReferringContext() : redir);
77  }
78 
79  @Deprecated
80  public static boolean authenticate(String login, String pass) throws IOException {
82  IContact user = Contacts.getEmpty();
83  if (user.validate(login, pass)) {
84  constructor.setSessionAttribute(IUser.LOGGED_USER, user);
85  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.TRUE);
86  if (user instanceof IContact) {
87  constructor.setSessionAttribute(Contacts.LOGGED_ICONTACT, user);
88  } else {
90  }
91  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/in", null, null);
92  return true;
93  } else {
94  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/failed", login, null);
95  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.FALSE);
98  return false;
99  }
100  }
101 
102  @Deprecated
103  public static void reauthenticate() throws IOException {
105  IContact user = constructor.getUser();
106  if (user != null && user.isValid()) {
107  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.FALSE);
108  user.reload();
109  constructor.setSessionAttribute(IUser.LOGGED_USER, user);
110  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.TRUE);
111  if (user instanceof IContact) {
112  constructor.setSessionAttribute(Contacts.LOGGED_ICONTACT, user);
113  } else {
115  }
116  }
117  }
118 
119  public static void doLogout(String redir) throws IOException {
121  if(SSO.hasSSO()) {
122  SSO.getProvider().removeAssertion(constructor.getRequest(), constructor.getResponse(),
124  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/out", "sso", null);
125  } else {
126  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/out", null, null);
127  }
128  constructor.setSessionAttribute(IUser.INTERNAL_SIGNIN, Boolean.FALSE);
129  Application.getApplication().sendRedirect(redir == null ? "/" : redir);
131  }
132 
133  public static boolean canLogin(String login, int minutes) {
134  if(!Strings.isBlank(login) && login.length() > 3) {
135  return SystemLogger.getInstance().getCountOf(login, "/log/failed", new CheckDate().addMinutes(-minutes).getDate()) < 3;
136  }
137  return false;
138  }
139 
140  public static boolean hasContact() {
141  IContact contact = getIContact();
142  return contact != null && contact.isValid();
143  }
144 
145  public static IContact getIContact() {
146  IContact contact = null;
147  try {
148  contact = getBehaveAsIContact();
149  if (contact == null) {
150  contact = getRealIContact();
151  }
152  } catch (Exception ex) {
153  contact = null;
154  }
155  return contact;
156  }
157 
158  public static IContact reloadIContact() {
160  HttpSession session = app.getHttpSession(false);
161  if(session != null && hasContact()) {
162  IContact contact = Contacts.getContactById(getIContact().getId());
163  if(contact.isValid()) {
164  session.setAttribute(IUser.LOGGED_USER, contact);
165  if(isBehaving()) {
166  session.setAttribute(Contacts.BEHAVEAS_ICONTACT, contact);
167  } else {
168  session.setAttribute(Contacts.LOGGED_ICONTACT, contact);
169  }
170  return contact;
171  }
172  }
175  return null;
176  }
177 
178  public static IContact getRealIContact() {
179  IContact contact = null;
180  try {
181  contact = getRealLoggedIContact();
182  if (contact == null) { // should be a viewer, no framework
183  contact = getLoggedIContact();
184  }
185  } catch (Exception ex) {
186  contact = null;
187  }
188  return contact;
189  }
190 
191  public static boolean canImpersonate() {
192  return isRealAdministrator();
193  }
194 
195  public static boolean isBehaving() {
197  return app.getHttpSession(false).getAttribute(BEHAVEAS_ICONTACT) != null;
198  }
199 
200  public static void impersonateContact(Object contact) {
202  }
203 
204  public static void impersonateIContact(IContact contact) {
206  if (contact != null && contact.isWebUser()) {
207  IContact user = contact;
209  app.getHttpSession(false).setAttribute(IUser.LOGGED_USER, user);
210  app.getHttpSession(false).setAttribute(Contacts.BEHAVEAS_ICONTACT, contact);
212  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/impersonate", null, user.getName());
213  } else {
214  contact = getRealLoggedIContact();
215  IContact user = contact;
217  app.getHttpSession(false).setAttribute(IUser.LOGGED_USER, user);
218  app.getHttpSession(false).removeAttribute(Contacts.BEHAVEAS_ICONTACT);
220  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/backtoself", null, null);
221  }
222  }
223 
224  private static IContact getBehaveAsIContact() {
226  return (IContact) app.getHttpSession(false).getAttribute(Contacts.BEHAVEAS_ICONTACT);
227  }
228 
229  private static IContact getRealLoggedIContact() {
230  Application app = Application.getApplication();
231  return (IContact) app.getHttpSession(false).getAttribute(Contacts.LOGGED_ICONTACT);
232  }
233 
234  public static IContact getLoggedIContact() {
236  }
237 
238  public static boolean isWebapp() {
239  IContact contact = getIContact();
240  return contact != null && contact.isWebapp();
241  }
242 
243  public static boolean isContactLogged() {
244  IContact contact = getIContact();
245  return contact != null && contact.isWebUser();
246  }
247 
248  public static boolean isAdministrator() {
249  IContact contact = getIContact();
250  return isWebapp() && contact.isAdmin();
251  }
252 
253  public static boolean isRealAdministrator() {
254  IContact contact = getRealIContact();
255  return contact != null && contact.isWebapp() && contact.isAdmin();
256  }
257 
258  public static boolean isLogged(IContact contact) {
259  IContact logged = getIContact();
260  return logged != null && logged.isValid() && logged.equals(contact);
261  }
262 
263  public static boolean sendReminder(IConstructor constructor, String name, String email) {
264  try {
265  Map args = new HashMap();
266  args.put("email", email);
267  args.put("name", name);
268  try {
269  args.put("link", ElephantContext.getServerUrl("http") + "?" + Actions.createAction((String) args.get("email"), "/user/changepass"));
270  } catch (Exception ex) {
271  Logger.getLogger(Authentication.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
272  }
274  .addUser((String) args.get("name"), (String) args.get("email"))
275  .putAll(args)
276  .sendTemplate("sign-reminder", I_.get("Password reminder") + " : " + ElephantContext.getSiteName());
277  return true;
278  } catch (EmailException ex) {
279  Logger.getLogger(Authentication.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
280  return false;
281  }
282  }
283 
284  public static boolean isCloudAdmin() {
285  return Secrets.isSecret("key=cloudadmin", Authentication.getIContact());
286  }
287 
288  private static IContact resolveUser(String login, String pass, Object extra) {
289  if(extra != null) { // might be an outsider
291  if(ea != null) {
292  IContact c = ea.doLogin(login, pass, extra);
293  if(c != null) return c;
294  }
295  } else {
296  IContact user = Contacts.getEmpty();
297  if(user.validate(login, pass)) return user;
298  }
299  return null;
300  }
301 
302  private Authentication() {
303  }
304 
305 }
static String createAction(String email, String redir)
Definition: Actions.java:90
static IContact getContact(Object object)
Definition: Contacts.java:109
static IContact getEmpty()
Definition: Contacts.java:56
static IContact getLoggedIContact(Application app)
Definition: Contacts.java:122
static final String LOGGED_ICONTACT
Definition: Contacts.java:45
static IContact getContactById(String id)
Definition: Contacts.java:72
static IMailSender getPool()
static boolean isSecret(String key, Object value)
Definition: Secrets.java:65
static boolean isRealAdministrator()
static void impersonateIContact(IContact contact)
static boolean canLogin(String login, int minutes)
static void doLogout(String redir)
static boolean sendReminder(IConstructor constructor, String name, String email)
static void doLogin(String login, String pass, String redir, Object extra)
static void impersonateContact(Object contact)
static boolean authenticate(String login, String pass)
static boolean isLogged(IContact contact)
static IContact getLoggedIContact()
abstract void sendRedirect(String uri)
HttpSession getHttpSession(boolean create)
static String getServerUrl(String scheme)
static IElephantAuthentication getFor(Object extra)
static String get(String msg)
Definition: I_.java:41
static ISystemLogger getInstance()
static IElephantSSO getSSO()
Definition: SSO.java:49
static boolean hasSSO()
Definition: SSO.java:34
static IAssertionProvider getProvider()
Definition: SSO.java:45
T addUser(String name, String email)
void setSessionAttribute(String key, Object value)
static final String INTERNAL_SIGNIN
Definition: IUser.java:33
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
static final String LOGGED_USER
Definition: IUser.java:32
boolean impersonateByEmail(String email)
boolean validate(String login, String password)
void removeAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact)
void createAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact)
IContact doLogin(String login, String pass, Object extra)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)
long getCountOf(String comment, String logPath, Date since)
void setAttribute(String key, Object value)