Coverage Report - jp.co.y_net.amm.page.LoginPage
 
Classes in this File Line Coverage Branch Coverage Complexity
LoginPage
0%
0/64
0%
0/26
0
LoginPage$1
0%
0/22
0%
0/12
0
 
 1  0
 package jp.co.y_net.amm.page;
 2  
 
 3  
 import jp.co.y_net.amm.AppSession;
 4  
 import jp.co.y_net.amm.AppSession.LoginUser;
 5  
 import jp.co.y_net.amm.common.AppLogger;
 6  
 import jp.co.y_net.amm.common.AppUtils;
 7  
 import jp.co.y_net.amm.common.ResourceReader;
 8  
 import jp.co.y_net.amm.dao.AppDef;
 9  
 import jp.co.y_net.amm.dao.Usr;
 10  
 import jp.co.y_net.amm.service.Logincheck;
 11  
 import jp.co.y_net.amm.service.Logincheck.LogincheckResult;
 12  
 
 13  
 import org.apache.commons.lang.StringUtils;
 14  
 import org.apache.wicket.AttributeModifier;
 15  
 import org.apache.wicket.RestartResponseException;
 16  
 import org.apache.wicket.markup.html.form.Button;
 17  
 import org.apache.wicket.markup.html.form.Form;
 18  
 import org.apache.wicket.markup.html.form.HiddenField;
 19  
 import org.apache.wicket.markup.html.form.TextField;
 20  
 import org.apache.wicket.markup.html.link.ExternalLink;
 21  
 import org.apache.wicket.model.Model;
 22  
 import org.apache.wicket.request.Url;
 23  
 import org.apache.wicket.request.Url.StringMode;
 24  
 import org.apache.wicket.request.flow.RedirectToUrlException;
 25  
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 26  
 
 27  
 public class LoginPage extends CommonFrameA {
 28  
     
 29  
     public LoginPage() {
 30  0
         this(new PageParameters());
 31  0
     }
 32  0
     public LoginPage(PageParameters params) {
 33  
         
 34  
         /* トップへ戻る */
 35  0
         add(new ExternalLink("lnkTop", ResourceReader.getStringQuick("url.back.top")));
 36  
         
 37  
         /* フィードバックパネルの設置 */
 38  0
         createFeedbackPanel();
 39  
         
 40  0
         final Model<String> modelChallengestringA = Model.of(challengestring.execute());
 41  0
         final HiddenField<String> textChallenge = new HiddenField<String>("textChallenge", modelChallengestringA);
 42  0
         final TextField<String> textLoginId = new TextField<String>("textLoginId", Model.of(""));
 43  0
         final HiddenField<String> textChallengeResponse = new HiddenField<String>("textChallengeResponse", Model.of(""));
 44  
         
 45  0
         Button btnLogin = new Button("btnLogin");
 46  0
         btnLogin.add(new AttributeModifier("onclick", "return eventLogin(true)"));
 47  
         
 48  0
         Form<Void> form =new Form<Void>("form") {
 49  
             @Override
 50  
             public void onSubmit() {
 51  
                 /* ログイン処理 */
 52  0
                 String challengestringA = modelChallengestringA.getObject();
 53  0
                 String loginidB = textLoginId.getValue();
 54  0
                 String cresE = textChallengeResponse.getValue();
 55  
                 
 56  
                 /* ------------------------ */
 57  
                 
 58  0
                 LogincheckResult logincheckResult = logincheck.executeFromAmm(loginidB, cresE, challengestringA);
 59  
                 
 60  0
                 if(0 == logincheckResult.errorCode) {
 61  
                     /* 認証成功 */
 62  
                     
 63  
                     /*
 64  
                      * すでに別のユーザでセッションが作成されているときの処理
 65  
                      */
 66  0
                     if(existOtherSession(loginidB)) {
 67  0
                         error("別のユーザでログイン中です。一度ログアウトしてください。");
 68  0
                         return;
 69  
                     }
 70  
                     
 71  
                     /* セッション開始 */
 72  0
                     startSession(loginidB);
 73  
                     
 74  
                     /* 画面遷移 */
 75  0
                     if(AppSession.get().returnPageClass != null) {
 76  
                         /* 呼び出し元が設定されている場合 */
 77  0
                         setResponsePage(AppSession.get().returnPageClass, AppSession.get().returnPageParameters);
 78  0
                     } else {
 79  
                         /* 呼び出し元が設定されていない場合 */
 80  
 //                        setResponsePage(getApplication().getHomePage());
 81  0
                         if(isMember()) {
 82  0
                             throw new RedirectToUrlException(ResourceReader.getStringQuick("url.back.member"));
 83  
                         }
 84  0
                         if(isOrgAdmin()) {
 85  0
                             throw new RedirectToUrlException(ResourceReader.getStringQuick("url.back.orgadmin"));
 86  
                         }
 87  0
                         if(isSystemAdmin()) {
 88  0
                             throw new RedirectToUrlException(ResourceReader.getStringQuick("url.back.sysadmin"));
 89  
                         }
 90  
                     }
 91  
                     
 92  
                 } else {
 93  
                     
 94  0
                     error(Logincheck.errorMessage.get(logincheckResult.errorCode));
 95  
                 }
 96  0
             }
 97  
         };
 98  0
         add(form);
 99  0
         form.add(textChallenge);
 100  0
         form.add(textLoginId);
 101  0
         form.add(textChallengeResponse);
 102  0
         form.add(btnLogin);
 103  
         
 104  
         
 105  
         /* 
 106  
          * パラメータにログインIDが含まれている場合の処理
 107  
          */
 108  0
         String paramLoginid = params.get("loginid").toString();
 109  0
         if (StringUtils.isEmpty(paramLoginid) == false) {
 110  
             
 111  
             /* 2015/10/02 引数にチャレンジレスポンスを追加 */
 112  0
             Usr user = usrDao.getByLoginid(paramLoginid);
 113  0
             String paramCres = params.get("cres").toString();
 114  0
             if (checkParam(user, paramCres)) {
 115  
 //            if (user != null && StringUtils.isEmpty(user.getCres()) == false) {
 116  
 //                if(user.getCresdate() > AppUtils.createNowLong())  {
 117  
                     
 118  
                     /* チャレンジレスポンスの有効期限以内のときはログイン認証を割愛してログインする */
 119  0
                     paramLoginid = AppUtils.urlDecode(paramLoginid); // URLデコード
 120  
                     
 121  
                     /* 
 122  
                      * チャレンジレスポンスの破棄
 123  
                      *  次のケースで無限ループとなるため。
 124  
                      * 1. 権限管理されたページに権限の不足しているユーザがアクセス
 125  
                      * 2. Loginへ自動転送され、チャレンジレスポンスが有効なため、自動ログイン(「1.」へ戻る)
 126  
                      */
 127  0
                     user.setCres(null);
 128  0
                     user.setCresdate(-1L);
 129  0
                     usrDao.update(user, AppDef.USERID_NONE); // 【更新処理】
 130  
                     
 131  
                     /*
 132  
                      * すでに別のユーザでセッションが作成されているときの処理
 133  
                      */
 134  0
                     if(existOtherSession(paramLoginid)) {
 135  0
                         error("別のユーザでログイン中です。一度ログアウトしてください。");
 136  0
                         return;
 137  
                     }
 138  
                     
 139  
                     /* セッション開始 */
 140  0
                     startSession(paramLoginid);
 141  
                     
 142  
                     /* 画面遷移 */
 143  0
                     if(AppSession.get().returnPageClass != null) {
 144  
                         /* 呼び出し元が設定されている場合 */
 145  0
                         throw new RestartResponseException(
 146  0
                                 AppSession.get().returnPageClass, AppSession.get().returnPageParameters);
 147  
                     } else {
 148  
                         /* 呼び出し元が設定されていない場合 */
 149  
                     }
 150  
 //                }
 151  
             }
 152  
         }
 153  0
     }
 154  
     
 155  
     /**
 156  
      * 識別パラメータの検証
 157  
      * ユーザ識別パラメータとは、
 158  
      *  ログイン後にアクセスできる画面を呼び出すときに必要なパラメータ。
 159  
      *  [UTF-8でURLエンコーディングされたログインID]と、
 160  
      *  ログイン認証時に使用した[チャレンジレスポンスの値]を使用する。
 161  
      *  例: ?loginid=user%40example.com&cres=374491d2deadaf228cd82b985d27e6af97d0518c2c1691be336a73035ad5eb9e
 162  
      *  
 163  
      * @param user 
 164  
      * @param paramCres 
 165  
      * @return
 166  
      */
 167  
     private boolean checkParam(Usr user, String paramCres) {
 168  0
         if(user == null) return false;
 169  0
         if(StringUtils.isEmpty(user.getCres())) return false;
 170  0
         if(StringUtils.isEmpty(paramCres)) return false;
 171  
         
 172  0
         if(user.getCresdate() > AppUtils.createNowLong())  {
 173  
             /* 有効期限内 */
 174  0
             if(paramCres.equals(user.getCres())) {
 175  0
                 return true; // チャレンジレスポンスが一致する
 176  
             } else {
 177  0
                 AppLogger.info("ユーザ識別パラメータが指定されましたが、チャレンジレスポンスの値が一致しませんでした。");
 178  0
                 return false;
 179  
             }
 180  
         } else {
 181  
             /* 有効期限を過ぎている */
 182  0
             return false;
 183  
         }
 184  
     }
 185  
     /**
 186  
      * ログイン認証後の処理
 187  
      * @param loginid 
 188  
      */
 189  0
     private void  startSession(String loginid) {
 190  
         
 191  
         /* セッションへ登録 */
 192  0
         Usr usr = usrDao.getByLoginid(loginid);
 193  0
         AppSession.get().setLoginUser(usr);
 194  
         
 195  
         /* jsessionidの変更(セッション固定化攻撃対策)*/
 196  0
         AppSession.get().replaceSession();
 197  
 
 198  0
     } 
 199  
     /**
 200  
      * すでに別のユーザでセッションが作成されているときの処理
 201  
      * @param loginid
 202  
      * @return
 203  
      */
 204  0
     private boolean existOtherSession(String loginid) {
 205  
         boolean existOtherSession;
 206  0
         if(AppSession.get().isLogin()) {
 207  0
             LoginUser loginUser = AppSession.get().getLoginUser();
 208  0
             if(loginUser.getLoginid().equals(loginid) == false) {
 209  0
                 existOtherSession = true;
 210  0
             } else {
 211  0
                 existOtherSession = false;
 212  
             }
 213  0
         } else {
 214  0
             existOtherSession = false;
 215  
         }
 216  0
         return existOtherSession;
 217  
     }
 218  
     
 219  
     /**
 220  
      * コンストラクタ呼び出しが行われなくても処理対象となるメソッド
 221  
      * 戻るボタンによる遷移でも呼び出される。
 222  
      * @see org.apache.wicket.Component#onConfigure()
 223  
      */
 224  
     @Override
 225  
     protected void onConfigure() {
 226  
         /*
 227  
          * URLの調整を行う。
 228  
          *  HTTPSでの接続が必要な時に、HTTPとして呼び出された場合には、URLを変更する。
 229  
          */
 230  0
         if (ResourceReader.getBooleanQuick("use.ssl")) {
 231  0
             Url url = getRequestCycle().getRequest().getOriginalUrl();
 232  0
             if("https".equalsIgnoreCase(url.getProtocol()) == false) {
 233  0
                 url.setProtocol("https");
 234  0
                 throw new RedirectToUrlException(url.toString(StringMode.FULL));
 235  
             }
 236  
         }
 237  0
     }
 238  
 }