-
Notifications
You must be signed in to change notification settings - Fork 1
/
Login.java
985 lines (918 loc) · 42.5 KB
/
Login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*
* SemanticWebBuilder es una plataforma para el desarrollo de portales y aplicaciones de integración,
* colaboración y conocimiento, que gracias al uso de tecnología semántica puede generar contextos de
* información alrededor de algún tema de interés o bien integrar información y aplicaciones de diferentes
* fuentes, donde a la información se le asigna un significado, de forma que pueda ser interpretada y
* procesada por personas y/o sistemas, es una creación original del Fondo de Información y Documentación
* para la Industria INFOTEC, cuyo registro se encuentra actualmente en trámite.
*
* INFOTEC pone a su disposición la herramienta SemanticWebBuilder a través de su licenciamiento abierto al público (‘open source’),
* en virtud del cual, usted podrá usarlo en las mismas condiciones con que INFOTEC lo ha diseñado y puesto a su disposición;
* aprender de él; distribuirlo a terceros; acceder a su código fuente y modificarlo, y combinarlo o enlazarlo con otro software,
* todo ello de conformidad con los términos y condiciones de la LICENCIA ABIERTA AL PÚBLICO que otorga INFOTEC para la utilización
* del SemanticWebBuilder 4.0.
*
* INFOTEC no otorga garantía sobre SemanticWebBuilder, de ninguna especie y naturaleza, ni implícita ni explícita,
* siendo usted completamente responsable de la utilización que le dé y asumiendo la totalidad de los riesgos que puedan derivar
* de la misma.
*
* Si usted tiene cualquier duda o comentario sobre SemanticWebBuilder, INFOTEC pone a su disposición la siguiente
* dirección electrónica:
* http://www.semanticwebbuilder.org
*/
package org.semanticwb.servlet.internal;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.security.Principal;
import java.util.Locale;
import java.util.regex.Matcher;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.semanticwb.Logger;
import org.semanticwb.SWBPlatform;
import org.semanticwb.SWBPortal;
import org.semanticwb.SWBUtils;
import org.semanticwb.base.util.SWBSoftkHashMap;
import org.semanticwb.model.SWBContext;
import org.semanticwb.model.User;
import org.semanticwb.model.UserRepository;
import org.semanticwb.platform.SemanticProperty;
import org.semanticwb.portal.SWBSessionObject;
import org.semanticwb.security.limiter.FailedAttempt;
// TODO: Auto-generated Javadoc
/**
* The Internal Servlet Login.
*
* @author Sergio Martínez (sergio.martinez@acm.org)
*/
public class Login implements InternalServlet
{
/** The log. */
private static Logger log = SWBUtils.getLogger(Login.class);
// private static String VALSESS = "swb4-auto";
// private static String CALLBACK = "swb4-callback";
/** The _realm. */
private static String _realm = "Semantic Web Builder";
/** The _name. */
private String _name = "login";
/** The handle error. */
private boolean handleError = false;
/** The secure. */
boolean secure = false;
String paramList = "";
/** The adm map. */
String admMap=null;
boolean admin = true;
//Constantes para primer implementación
/** The blocked list. */
private static SWBSoftkHashMap<String, FailedAttempt> blockedList = new SWBSoftkHashMap<String, FailedAttempt>();
//private static java.security.KeyPair rsakey = SWBUtils.CryptoWrapper.getRSAKey();
private static String codigoRSA1 = "\n"
+ "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/swbadmin/js/crypto/jsbn.js\"></script>\n"
+ "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/swbadmin/js/crypto/prng4.js\"></script>\n"
+ "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/swbadmin/js/crypto/rng.js\"></script>\n"
+ "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/swbadmin/js/crypto/rsa.js\"></script>\n"
+ "<script> \n"
+ "var rsa = new RSAKey();\n";
private static String codigoRSA2 = "function encrypt(){\n"
+ "var res = rsa.encrypt(document.login.wb_password.value);\n"
+ "if (res){ document.login.wb_password.value=res; return true;}\n"
+ "else {return false;}\n}\n"
+ "document.login.onsubmit=encrypt;\n"
+ "</script>";
/* (non-Javadoc)
* @see org.semanticwb.servlet.internal.InternalServlet#init(javax.servlet.ServletContext)
*/
public void init(ServletContext config)
{
log.event("Initializing InternalServlet Login...");
secure = SWBPlatform.getEnv("swb/secureAdmin", "false").equalsIgnoreCase("true");
paramList = SWBPlatform.getEnv("swb/ExternalParamList","");
admin = SWBPlatform.getEnv("swb/administration", "true").equalsIgnoreCase("true");
admMap=SWBContext.WEBSITE_ADMIN;
//TODO: preparar los aspectos configurables de la autenticación
}
/**
* Sets the handle error.
*
* @param handleError the new handle error
*/
public void setHandleError(boolean handleError)
{
this.handleError = handleError;
}
/**
* Send login log.
*
* @param request the request
* @param usr the usr
*/
public static void sendLoginLog(HttpServletRequest request, User usr)
{
//User session log
{
StringBuffer logbuf = new StringBuffer();
logbuf.append("lgn|");
logbuf.append(request.getRemoteAddr());
logbuf.append("|");
logbuf.append(SWBPortal.getMessageCenter().getAddress());
logbuf.append("|");
logbuf.append(usr.getSemanticObject().getModel().getName());
logbuf.append("|");
String lg = usr.getLogin();
if (lg == null)
{
lg = "_";
}
logbuf.append(lg);
logbuf.append("|");
logbuf.append("" + request.getSession(true).hashCode());
SWBPortal.getMessageCenter().sendMessage(logbuf.toString());
}
}
public static void sendMailLog(HttpServletRequest request, User usr)
{
StringBuffer logbuf = new StringBuffer();
if ("es".equals(usr.getLanguage())){
logbuf.append(usr.getFullName());
logbuf.append("\nLe informamos que se ha firmado el ");
logbuf.append(new java.util.Date());
logbuf.append(" desde la IP:");
logbuf.append(request.getRemoteAddr());
logbuf.append(" al sitio:");
logbuf.append(request.getServerName());
}else{
logbuf.append(usr.getFullName());
logbuf.append("\nWe inform you that you have logged in the ");
logbuf.append(new java.util.Date());
logbuf.append(" from IP:");
logbuf.append(request.getRemoteAddr());
logbuf.append(" to site:");
logbuf.append(request.getServerName());
}
try {
SWBUtils.EMAIL.sendBGEmail(usr.getEmail(), request.getServerName(), logbuf.toString());
} catch (java.net.SocketException sex) {
log.error(sex);
}
}
/* (non-Javadoc)
* @see org.semanticwb.servlet.internal.InternalServlet#doProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.semanticwb.servlet.internal.DistributorParams)
*/
public void doProcess(HttpServletRequest request, HttpServletResponse response, DistributorParams dparams) throws IOException
{
if (null == dparams.getWebPage())
{
return;
}
if (dparams.getWebPage() == null || (!admin && dparams.getWebPage().getWebSite().getId().equals(admMap)))
{
response.sendError(404, "La pagina " + request.getRequestURI() + " no existe... ");
log.debug("Distributor: SendError 404");
return ;
}
if (secure && admMap.equalsIgnoreCase(dparams.getWebPage().getWebSiteId()) && !request.isSecure()){
sendRedirect(response, "https://"+request.getServerName()+request.getRequestURI());
}
UserRepository ur = dparams.getWebPage().getWebSite().getUserRepository();
String authMethod = ur.getAuthMethod();
String context = ur.getLoginContext();
String CBHClassName = ur.getCallBackHandlerClassName();
Subject subject = SWBPortal.getUserMgr().getSubject(request, dparams.getWebPage().getWebSiteId());
String uri = request.getRequestURI();
String path = SWBPlatform.getContextPath();
boolean inLoginFase=false;
User user = null; //System.out.println("Externo: "+ur.getId()+":"+ur.isExternal());
if (ur.isExternal()) {
String[]list =paramList.split(",");
for (String param:list){ //System.out.println("paramlist:"+param);
if (request.getParameter(param)!=null) inLoginFase=true;
}
}
Iterator it = subject.getPrincipals().iterator();
if (it.hasNext())
{
user = (User) it.next();
}
if (request.getParameter("wb_logout") != null)
{
LoginContext lc;
try
{
if (null!=user) user.checkCredential("{123}456".toCharArray()); //Invalidate user even in cluster
lc = new LoginContext(context, subject);
lc.logout();
request.getSession(false).invalidate();
String url = request.getParameter("wb_goto");
if ((url == null))
{
// url = path + "/" + SWBPlatform.getEnv("swb/distributor") + "/" + dparams.getWebPage().getWebSiteId() + "/" + dparams.getWebPage().getId() + "/_lang/" + dparams.getUser().getLanguage();
url = dparams.getWebPage().getUrl(dparams.getUser().getLanguage());
}
log.debug("LOGOUT (Path, uri, url): " + path + " | " + uri + " | " + url);
sendRedirect(response, url);
return;
} catch (Exception elo)
{
log.error("LoggingOut " + subject, elo);
}
}
if (null != request.getParameter("user")){
User pcUser = null;
try {
String ids = new String(SWBUtils.CryptoWrapper.PBEAES128Decipher(SWBPlatform.getVersion(),
SWBUtils.TEXT.decodeBase64(request.getParameter("user")).getBytes()));
UserRepository pur = SWBContext.getUserRepository(ids.substring(0,ids.indexOf("|")));
pcUser=pur.getUserByLogin(ids.substring(ids.indexOf("|")+1));
String alg = pcUser.getPassword().substring(1,pcUser.getPassword().indexOf("}"));
if (pcUser.getPassword().equals(SWBUtils.CryptoWrapper.comparablePassword(request.getParameter("wb_old_password"), alg)))
{
if (request.getParameter("wb_new_password").equals(request.getParameter("wb_new_password2")))
{
pcUser.setPassword(request.getParameter("wb_new_password"));
pcUser.setRequestChangePassword(false);
if(null==pcUser.getLanguage()) pcUser.setLanguage("es"); //forzar lenguage si no se dio de alta.
subject.getPrincipals().clear();
subject.getPrincipals().add(pcUser);
pcUser.setLastLogin(new java.util.Date());
pcUser.checkCredential(request.getParameter("wb_new_password").toCharArray());
uri = uri.replaceFirst("/login/", "/swb/");
sendRedirect(response, uri);
return;
} else {
formChangePwd(request, response, dparams, pcUser, SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login", "errdifconf", new Locale(dparams.getUser().getLanguage())));
return;
}
}else {
formChangePwd(request, response, dparams, pcUser, SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login", "errnomatch", new Locale(dparams.getUser().getLanguage())));
return;
}
} catch (NullPointerException npe)
{
doResponse(request, response, dparams, null, authMethod);
return;
}catch (Exception sec){
formChangePwd(request, response, dparams, pcUser, "Error: "+sec.getMessage());
return;
}
}
if (uri != null)
{
path = uri.replaceFirst(_name, SWBPlatform.getEnv("swb/distributor"));
}
if (null != request.getParameter("wb_username") ) inLoginFase=true;
if (!inLoginFase)
{
log.debug("Request a new username...");
doResponse(request, response, dparams, null, authMethod);
return;
} else
{
CallbackHandler callbackHandler = null;
try
{
callbackHandler = getHandler(CBHClassName, request, response, authMethod, dparams.getWebPage().getWebSiteId());
} catch (Exception ex)
{
log.error("Can't Instanciate a CallBackHandler for UserRepository " + ur.getId() + "\n" + CBHClassName, ex);
response.sendError(500, "Authentication System failure!!!" +ex.getMessage());
return;
}
try
{
String matchKey = dparams.getWebPage().getWebSiteId()+"|"+request.getParameter("wb_username");
doLogin(callbackHandler, context, subject, request, matchKey, dparams.getWebPage().getWebSite().getUserRepository().getId());
//MAPS740508 - 18-09-2013 - Checar si recordar con cookie
if ("true".equals(request.getParameter("wb_rememberuser")))
{
setRememberCookie((User)subject.getPrincipals().iterator().next(),
response, dparams.getWebPage().getWebSite().getUserRepository().getId(),
dparams.getWebPage().getWebSite().getUserRepository());
}
} catch (Exception ex)
{
try {
User tmpuser = dparams.getWebPage().getWebSite().getUserRepository().getUserByLogin(request.getParameter("wb_username"));
String repo = dparams.getWebPage().getWebSite().getUserRepository().getId();
if (null!=tmpuser && tmpuser.isActive() && (tmpuser.isRequestChangePassword() ||
SWBPlatform.getSecValues().isForceChage(repo) || SWBPlatform.getSecValues().getExpires(repo)>0))
{
String alg = tmpuser.getPassword().substring(1,tmpuser.getPassword().indexOf("}"));
String tmpPass = request.getParameter("wb_password");
if (SWBPlatform.getSecValues().isEncrypt()) tmpPass = SWBUtils.CryptoWrapper.decryptPassword(tmpPass,
SWBPortal.getUserMgr().getSessionKey(request));
if (tmpuser.getPassword().equals(
SWBUtils.CryptoWrapper.comparablePassword(
tmpPass, alg)) && tmpuser.isRequestChangePassword())
{
formChangePwd(request, response, dparams, tmpuser, SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login", "errmustupd", new Locale(dparams.getUser().getLanguage())));
return;
}
}
} catch (Exception ne)
{
log.debug("Problema al intentar cambiar password!", ne); //continuar como nuevo logueo
}
markFailedAttepmt(request.getParameter("wb_username"));
log.debug("Can't log User", ex);
String alert = SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login", "alert", new Locale(dparams.getUser().getLanguage()));
// "User non existent";
if (isblocked(request.getParameter("wb_username"))){
alert = SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login", "blocked", new Locale(dparams.getUser().getLanguage()));
// "User has been temporarily blocked";
}
doResponse(request, response, dparams, alert, authMethod);
return;
}
}
String url = request.getParameter("wb_goto");
if (user.getUserRepository().isExternal())
{
if ((url == null || url.equals("/")) && user.getUserRepository().getBridge().doRedirect())
{
url = user.getUserRepository().getBridge().getRedirectURL();
}
}
if ((url == null || url.equals("/")))
{
log.debug("PATHs: Path:" + path + " - " + dparams.getWebPage().getWebSiteId() + " - " + dparams.getWebPage().getId());
url = /*SWBPlatform.getContextPath() + */ dparams.getWebPage().getUrl(dparams.getUser().getLanguage());
//SWBPlatform.getContextPath() + "/" + SWBPlatform.getEnv("swb/distributor") + "/" + dparams.getWebPage().getWebSiteId() + "/" + dparams.getWebPage().getId() + "/_lang/" + dparams.getUser().getLanguage();
}
if (null!=request.getQueryString() && request.getQueryString().indexOf("wb_logout")==-1) url = url+"?"+request.getQueryString();
sendRedirect(response, url);
}
/**
* public void doProcessxx(HttpServletRequest request, HttpServletResponse response, DistributorParams dparams) throws IOException
* {
* if (null == dparams.getWebPage())
* {
* return;
* }
* UserRepository ur = dparams.getWebPage().getWebSite().getUserRepository();
* String authMethod = ur.getAuthMethod();
* //String authMethod = ur.getProperty(UserRepository.SWBUR_AuthMethod);
* String context = ur.getLoginContext();
* //String context = ur.getProperty(UserRepository.SWBUR_LoginContext);
* String CBHClassName = ur.getCallBackHandlerClassName();
* //String CBHClassName = ur.getProperty(UserRepository.SWBUR_CallBackHandlerClassName);
* Subject subject = SWBPortal.getUserMgr().getSubject(request);
* HttpSession session = request.getSession(true);
* String enAuto = (String) session.getAttribute(VALSESS);
* String uri = request.getRequestURI();
* String path = SWBPlatform.getContextPath();
* User user = null;
* Iterator it = subject.getPrincipals().iterator();
* if (it.hasNext())
* {
* user = (User) it.next();
* }
* if (request.getParameter("wb_logout") != null)
* {
* LoginContext lc;
* try
* {
* lc = new LoginContext(context, subject);
* lc.logout();
* request.getSession(true).invalidate();
* String url = request.getParameter("wb_goto");
* if ((url == null || url.equals("/")))
* {
* url = path + "/" + SWBPlatform.getEnv("swb/distributor") + "/" + dparams.getWebPage().getWebSiteId() + "/" + dparams.getWebPage().getId() + "/_lang/" + dparams.getUser().getLanguage();
* log.debug("LOGOUT3(Path, uri, url): " + path + " | " + uri + " | " + url);
* sendRedirect(response, url);
* return;
* }
* } catch (Exception elo)
* {
* log.error("LoggingOut " + subject, elo);
* }
* }
*
* if (uri != null)
* {
* path = uri.replaceFirst(_name, SWBPlatform.getEnv("swb/distributor"));
* }
* if (handleError || user == null || !user.isSigned())
* {
* CallbackHandler callbackHandler = (CallbackHandler) session.getAttribute(CALLBACK);
* if (null == callbackHandler)
* {
* if (null != request.getParameter("wb_username"))
* {
* enAuto = "Working";
* }
* try
* {
* log.debug("New callbackHandler...");
* Constructor[] constructor = Class.forName(CBHClassName).getConstructors();
* int method = 0;
* for (int i = 0; i < constructor.length; i++)
* {
* if (constructor[i].getParameterTypes().length == 4)
* {
* method = i;
* }
* }
* callbackHandler = (CallbackHandler) constructor[method].newInstance(request, response, authMethod, dparams);
* //callbackHandler = new SWB4CallbackHandlerLoginPasswordImp(request, response, authMethod, dparams); //TODO proveer otros métodos
* session.setAttribute(CALLBACK, callbackHandler);
* } catch (Exception ex)
* {
* log.error("Can't Instanciate a CallBackHandler for UserRepository " + ur.getId()+"\n"+CBHClassName, ex);
* response.sendError(500, "Authentication System failure!!!");
* return;
* }
* } else
* {
* if (null == request.getParameter("wb_username"))
* {
* log.debug("Request a new username...");
* doResponse(request, response, dparams, null, authMethod);
* session.setAttribute(VALSESS, "Working");
* return;
* }
* ((SWB4CallbackHandler) callbackHandler).setRequest(request);
* ((SWB4CallbackHandler) callbackHandler).setResponse(response);
* }
* if (null == enAuto)
* {
* log.debug("Starts new Authentication process...");
* doResponse(request, response, dparams, null, authMethod);
* session.setAttribute(VALSESS, "Working");
* return;
* }
* LoginContext lc;
* try
* {
* log.trace("1er callback "+callbackHandler);
* request.getSession(true).invalidate();
* subject = SWBPortal.getUserMgr().getSubject(request);
* log.trace("es null??? "+((SWB4CallbackHandler) callbackHandler).getRequest()+" y es SWB4 " + (callbackHandler instanceof SWB4CallbackHandler));
* if (callbackHandler instanceof SWB4CallbackHandler && null == ((SWB4CallbackHandler) callbackHandler).getRequest())
* {
* log.trace("intentando...");
* ((SWB4CallbackHandler) callbackHandler).setRequest(request);
* ((SWB4CallbackHandler) callbackHandler).setResponse(response);
*
* }
* try
* {
* log.trace("testing for null: "+
* ((SWB4CallbackHandler) callbackHandler).getRequest());
* } catch (NullPointerException npe)
* {
* try
* {
* log.trace("re Build callbackHandler...");
* Constructor[] constructor = Class.forName(CBHClassName).getConstructors();
* int method = 0;
* for (int i = 0; i < constructor.length; i++)
* {
* if (constructor[i].getParameterTypes().length == 4)
* {
* method = i;
* }
* }
* callbackHandler = (CallbackHandler) constructor[method].newInstance(request, response, authMethod, dparams);
* //callbackHandler = new SWB4CallbackHandlerLoginPasswordImp(request, response, authMethod, dparams); //TODO proveer otros métodos
* session.setAttribute(CALLBACK, callbackHandler);
* } catch (Exception ex)
* {
* log.error("Can't Instanciate a CallBackHandler for UserRepository " + ur.getId(), ex);
* response.sendError(500, "Authentication System failure!!!");
* return;
* }
* }
* log.trace("2o callback "+callbackHandler);
* lc = new LoginContext(context, subject, callbackHandler);
* lc.login();
* // Obtener el principal asociado (el principal se modifica en el login)
* it = subject.getPrincipals().iterator();
* if (it.hasNext())
* {
* user = (User) it.next();
* log.trace("user checked?:"+user.hashCode()+":"+user.isSigned());
* }
* sendLoginLog(request, user);
* // session.removeAttribute(VALSESS);
* // session.removeAttribute(CALLBACK);
* // System.out.println(subject.toString());
* // System.out.println(lc.getSubject().toString());
* // System.out.println(((User)lc.getSubject().getPrincipals().iterator().next()).isSigned());
* } catch (LoginException ex)
* {
* log.debug("Can't log User", ex);
* doResponse(request, response, dparams, "User non existent", authMethod);
* return;
* }
* }
*
* String url = request.getParameter("wb_goto");
* if ((url == null || url.equals("/")))
* {
* log.debug("PATHs: Path:" + path + " - " + dparams.getWebPage().getWebSiteId() + " - " + dparams.getWebPage().getId());
* url =
* SWBPlatform.getContextPath() + "/" + SWBPlatform.getEnv("swb/distributor") + "/" + dparams.getWebPage().getWebSiteId() + "/" + dparams.getWebPage().getId() + "/_lang/" + dparams.getUser().getLanguage();
* }
*
* sendRedirect(response, url);
* //response.getWriter().print("Hello Login, Authenticated User: "+subject.getPrincipals().iterator().next().getName());
* }
*
* @param request the request
* @param response the response
* @param distributorParams the distributor params
* @param alert the alert
* @param authMethod the auth method
* @throws IOException Signals that an I/O exception has occurred.
*/
private void doResponse(HttpServletRequest request, HttpServletResponse response, DistributorParams distributorParams, String alert, String authMethod) throws IOException
{
if ("BASIC".equals(authMethod))
{
String realm = (null != distributorParams.getWebPage().getDescription(distributorParams.getUser().getLanguage()) ? distributorParams.getWebPage().getDescription(distributorParams.getUser().getLanguage()) : _realm);
basicChallenge(realm, response);//TODO Asignar nombre de Realm
}
if ("FORM".equals(authMethod))
{
formChallenge(request, response, distributorParams, alert);
}
if ("OTHER".equals(authMethod))
{
redirectAlternateLogin(request, response, distributorParams, alert);
}
}
/**
* Basic challenge.
*
* @param realm the realm
* @param response the response
*/
private void basicChallenge(String realm, HttpServletResponse response)
{
StringBuffer header = new StringBuffer();
header.append("Basic realm=\"");
header.append(realm);
header.append("\"");
response.setHeader("WWW-Authenticate", header.toString());
response.setHeader("Cache-Control", "no-cache=\"Authorization\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
/**
* Form challenge.
*
* @param request the request
* @param response the response
* @param distributorParams the distributor params
* @param alert the alert
* @throws IOException Signals that an I/O exception has occurred.
*/
private void formChallenge(HttpServletRequest request, HttpServletResponse response, DistributorParams distributorParams, String alert) throws IOException
{
String ruta = "/config/";
//TODO: Obtener objetivo del siguiente código
if (handleError)
{
ruta += "403";
} else
{
ruta += "login";
}
ruta += ".html";
String login = null;
try
{
String rutaSite = SWBPortal.getWorkPath() + ruta;
try
{
rutaSite = "/models/" + distributorParams.getWebPage().getWebSite().getId() + ruta;
login =
SWBPortal.readFileFromWorkPath(rutaSite);
login =
SWBPortal.UTIL.parseHTML(login, SWBPortal.getWebWorkPath() + "/models/" + distributorParams.getWebPage().getWebSite().getId() + "/config/images/");
} catch (Exception ignored)
{
}
if (null == login || "".equals(login))
{
login = SWBPortal.readFileFromWorkPath(ruta);
login =
SWBPortal.UTIL.parseHTML(login, SWBPortal.getWebWorkPath() + "/config/images/");
}
login = login.replaceFirst("<SWBVERSION>", SWBPlatform.getVersion());
if (SWBPlatform.getSecValues().isEncrypt()){
java.security.KeyPair key = SWBPortal.getUserMgr().getSessionKey(request);
String codigoRSA = codigoRSA1 + "rsa.setPublic(\""+ ((java.security.interfaces.RSAPublicKey)key.getPublic()).getModulus().toString(16)+"\", \""
+((java.security.interfaces.RSAPublicKey)key.getPublic()).getPublicExponent().toString(16)+"\");\n"
+codigoRSA2;
login = login.replaceFirst("<SWBSECURITY>", codigoRSA);
} else {
login = login.replaceFirst("<SWBSECURITY>", "");
}
if (distributorParams.getWebPage().getWebSite().getUserRepository().isUserRepRememberUser())
{
String rememberField = handleError?
"<br>"+SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login",
"remember", new Locale(distributorParams.getUser().getLanguage()))
+": <input type=\"checkbox\" name=\"wb_rememberuser\" value=\"true\" />":
"<tr>\n<th>\n<h2>"+SWBUtils.TEXT.getLocaleString("org.semanticwb.servlet.internal.Login",
"remember", new Locale(distributorParams.getUser().getLanguage()))
+":</h2></th><td><input type=\"checkbox\" name=\"wb_rememberuser\""
+ " value=\"true\" />\n</td>\n</tr>\n";
login = login.replaceFirst("<SWBREMEMBER>", rememberField);
} else
{
login = login.replaceFirst("<SWBREMEMBER>", "");
}
} catch (Exception e)
{
log.error("Error to load login page...", e);
}
if (handleError)
{
response.setStatus(403);
}
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
java.io.PrintWriter out = response.getWriter();
out.print(login);
if (null != alert)
{
out.print("<script>alert('" + alert + "');</script>");
}
out.flush();
out.close();
}
/**
* Send redirect.
*
* @param response the response
* @param url the url
*/
public void sendRedirect(HttpServletResponse response, String url)
{
try
{
response.setContentType("Text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><meta http-equiv=\"Refresh\" CONTENT=\"0; URL=" + url + "\" /><script>window.location='" + url + "';</script></head></html>");
out.flush();
//out.close();
} catch (IOException e)
{
log.error("Redirecting user", e);
}
}
/**
* Gets the handler.
*
* @param CBHClassName the cBH class name
* @param request the request
* @param response the response
* @param authType the auth type
* @param website the website
* @return the handler
* @throws ClassNotFoundException the class not found exception
* @throws InvocationTargetException the invocation target exception
* @throws InstantiationException the instantiation exception
* @throws IllegalAccessException the illegal access exception
*/
public static CallbackHandler getHandler(String CBHClassName, HttpServletRequest request, HttpServletResponse response, String authType, String website) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException
{
Constructor[] constructor = Class.forName(CBHClassName).getConstructors();
int method = 0;
for (int i = 0; i < constructor.length; i++)
{
if (constructor[i].getParameterTypes().length == 4)
{
method = i;
}
}
return (CallbackHandler) constructor[method].newInstance(request, response, authType, website);
}
/**
* Redirect alternate login.
*
* @param request the request
* @param response the response
* @param distributorParams the distributor params
* @param alert the alert
* @throws IOException Signals that an I/O exception has occurred.
*/
private void redirectAlternateLogin(HttpServletRequest request, HttpServletResponse response, DistributorParams distributorParams, String alert) throws IOException
{
String altURL = distributorParams.getWebPage().getWebSite().getUserRepository().getAlternateLoginURL();
if (null == altURL)
{
formChallenge(request, response, distributorParams, alert);
} else
{
altURL = SWBUtils.TEXT.replaceAll(altURL, "<website>", distributorParams.getWebPage().getWebSiteId());
altURL = SWBUtils.TEXT.replaceAll(altURL, "<webpage>", distributorParams.getWebPage().getId());
sendRedirect(response, altURL);
}
}
/**
* Do login.
*
* @param callbackHandler the callback handler
* @param context the context
* @param subject the subject
* @param request the request
* @param matchKey the match key
* @throws LoginException the login exception
*/
public static void doLogin(CallbackHandler callbackHandler, String context, Subject subject, HttpServletRequest request, String matchKey, String usserrep) throws LoginException
{
log.trace("doLogin of:"+request.getParameter("wb_username")+"|"+request.getParameter("wb_password"));
if (isblocked(matchKey)){
throw new LoginException("Login blocked for repeated attempts");
}
if (SWBPlatform.getSecValues().isMultiple()){
String login = request.getParameter("wb_username");
Iterator<SWBSessionObject> llist =SWBPortal.getUserMgr().listSessionObjects();
while(llist.hasNext()){
SWBSessionObject so = llist.next();
Iterator<Principal> lpri = so.getSubjectByUserRep(usserrep).getPrincipals().iterator();
if (lpri.hasNext() && login.equalsIgnoreCase(((User)lpri.next()).getLogin()))
throw new LoginException("User already logged in");
}
}
LoginContext lc;
User user=null;
log.trace("Sending calback:" + callbackHandler + " " + context);
// request.getSession(true).invalidate();
lc = new LoginContext(context, subject, callbackHandler);
lc.login();
Iterator it = subject.getPrincipals().iterator();
if (it.hasNext())
{
user = (User) it.next();
log.trace("l user checked?:" + user.hashCode() + ":" + user.isSigned());
}
if(null==user.getLanguage()) user.setLanguage("es"); //forzar lenguage si no se dio de alta.
cleanBlockedEntry(matchKey);
if (SWBPlatform.getSecValues().isSendMail(user.getUserRepository().getId()))
{
sendMailLog(request, user);
}
sendLoginLog(request, user);
}
/**
* Mark failed attepmt.
*
* @param matchKey the match key
*/
public static void markFailedAttepmt(String matchKey)
{
FailedAttempt failedAttempt = blockedList.get(matchKey);
if (null==failedAttempt) {
FailedAttempt fa = new FailedAttempt(matchKey);
blockedList.put(matchKey, fa);
failedAttempt = fa;
}
failedAttempt.failedAttempt(); //System.out.println("******************************:"+failedAttempt.getLogin()+":"+failedAttempt.getCont()+":"+failedAttempt.isBlocked()+":"+failedAttempt.getTsBlockedTime());
}
/**
* Checks if is blocked.
*
* @param matchKey the match key
* @return true, if is blocked
*/
public static boolean isblocked(String matchKey)
{
boolean ret=false;
FailedAttempt current = blockedList.get(matchKey);
if (null!=current){
ret = current.isBlocked();
if (current.isBlocked() && (current.getTsBlockedTime()+(1000*60*5))<System.currentTimeMillis()){
blockedList.remove(matchKey);
ret=false;
}
}
return ret;
}
/**
* Clean blocked entry.
*
* @param matchKey the match key
*/
private static void cleanBlockedEntry(String matchKey)
{
FailedAttempt current = blockedList.get(matchKey);
if (null!=current){
blockedList.remove(matchKey);
}
}
private void formChangePwd(HttpServletRequest request, HttpServletResponse response, DistributorParams distributorParams, User user, String Message) throws IOException
{
String ruta = "/config/password.html";
String login = null;
String repo = (user.getUserRepository()!=null)?user.getUserRepository().getId():distributorParams.getWebPage().getWebSite().getUserRepository().getId();
try
{
String rutaSite = SWBPortal.getWorkPath() + ruta;
try
{
rutaSite = "/models/" + distributorParams.getWebPage().getWebSite().getId() + ruta;
login =
SWBPortal.readFileFromWorkPath(rutaSite);
login =
SWBPortal.UTIL.parseHTML(login, SWBPortal.getWebWorkPath() + "/models/" + distributorParams.getWebPage().getWebSite().getId() + "/config/images/");
} catch (Exception ignored)
{
}
if (null == login || "".equals(login))
{
login = SWBPortal.readFileFromWorkPath(ruta);
login =
SWBPortal.UTIL.parseHTML(login, SWBPortal.getWebWorkPath() + "/config/images/");
}
login = login.replaceFirst("<SWBVERSION>", SWBPlatform.getVersion());
login = login.replaceFirst("<ussid>", Matcher.quoteReplacement("<input type=\"hidden\" name=\"user\" value=\""+
SWBUtils.TEXT.encodeBase64(new String(SWBUtils.CryptoWrapper.PBEAES128Cipher(SWBPlatform.getVersion(),
(""+user.getUserRepository().getId()+"|"+user.getLogin()).getBytes())))+"\">"));
login = login.replaceFirst("<val01>", (SWBPlatform.getSecValues().isDifferFromLogin(repo))?
Matcher.quoteReplacement("if (form.wb_new_password.value == '"+user.getLogin()+
"') { ret=false; alert('Error: password must be diferent from login.');}"):"");
login = login.replaceFirst("<val02>", (SWBPlatform.getSecValues().getMinlength(repo)>0)?
Matcher.quoteReplacement("if (form.wb_new_password.value.length < "+SWBPlatform.getSecValues().getMinlength(repo)+
") { ret=false; alert('Error: password must have at least "+SWBPlatform.getSecValues().getMinlength(repo)+" characters.');}"):"");
login = login.replaceFirst("<val03>", (SWBPlatform.getSecValues().getComplexity(repo)==1)?
Matcher.quoteReplacement("if (!form.wb_new_password.value.match(/^.*(?=.*[a-zA-Z])(?=.*[0-9])().*$/) ) { ret=false; alert('Error: password must have leters and numbers.');}"):"");
login = login.replaceFirst("<val04>", (SWBPlatform.getSecValues().getComplexity(repo)==2)?
Matcher.quoteReplacement("if (!form.wb_new_password.value.match(/^.*(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\\W])().*$/) ) { ret=false; alert('Error: password must have leters, numbres and special symbols.');}"):"");
login = login.replaceFirst("<val05>", (SWBPlatform.getSecValues().getComplexity(repo)==3)?
Matcher.quoteReplacement("if (!form.wb_new_password.value.match(/"+SWBPlatform.getSecValues().getCustomExp(repo)+"/) ) { ret=false; alert('"+SWBPlatform.getSecValues().getCustomMsg(repo)+"');}"):"");
} catch (Exception e)
{
log.error("Error to load password page...", e);
}
if (handleError)
{
response.setStatus(403);
}
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
java.io.PrintWriter out = response.getWriter();
out.print(login);
if (null != Message)
{
out.print("<script>alert(\"" + Message + "\");</script>");
}
out.flush();
out.close();
}
public void setRememberCookie(final User user, final HttpServletResponse response, final String id, final UserRepository urep) {
try {
if (urep.isUserRepRememberUser() && user.isSigned()) {
String uid = user.getShortURI();
SemanticProperty sp = SWBPlatform.getSemanticMgr().getModel(SWBPlatform.getSemanticMgr().SWBAdmin).getSemanticProperty(SWBPlatform.getSemanticMgr().SWBAdminURI + "/PrivateKey");
//System.out.println("sp:"+sp);
String pass = SWBPlatform.getSemanticMgr().getModel(SWBPlatform.getSemanticMgr().SWBAdmin).getModelObject().getProperty(sp);
//System.out.println("pass:"+pass);
if (null!=pass){
byte[] buid = SWBUtils.CryptoWrapper.PBEAES128Cipher(pass, uid.getBytes());
Cookie cookie = new Cookie("swb."+id, SWBUtils.TEXT.encodeBase64(new String(buid)));
cookie.setPath(SWBPortal.getContextPath()+"/");
cookie.setMaxAge(60 * 60 * 24 * 365);
String name = "Set-Cookie";
cookie.setVersion(1);
response.setHeader(name, getCookieData(cookie));
}
}
} catch (GeneralSecurityException ex) {
log.error("Can't create remembering cookie", ex);
}
}
private String getCookieData(Cookie cookie){
StringBuffer buf = new StringBuffer();
buf.append(cookie.getName());
buf.append("=");
buf.append ('"');
buf.append (cookie.getValue());
buf.append ('"');
buf.append ("; Version=1");
buf.append ("; Max-Age=");
buf.append (cookie.getMaxAge());
buf.append ("; Path=");
//buf.append ('"');
buf.append (cookie.getPath());
//buf.append ('"');
buf.append ("; HttpOnly");
return buf.toString();
}
}