Ignore:
Timestamp:
04/25/11 09:43:20 (13 years ago)
Author:
rafaelraymundo
Message:

Ticket #1739 - Login com certificado em atributo customizável.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.0.1/security/ExpressoCertLogin/src/LoginApplet.java

    r3633 r4123  
    4343        this.setup.addLanguageResource("ExpressoCertLoginMessages"); 
    4444    } 
     45     
     46    private boolean parseVercert(String[] answer){ 
     47        boolean tryAgain = false; 
     48        // Faz o login 
     49        if (setup.getParameter("debug").equalsIgnoreCase("true") && answer != null) { 
     50            System.out.println("Código de retorno: " + answer[0].trim()); 
     51        } 
     52 
     53        if (answer == null){ // Ação cancelada 
     54            tryAgain = false; 
     55            String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() 
     56                    + ":" + this.getCodeBase().getPort() + "/login.php"; 
     57            try { 
     58                this.getAppletContext().showDocument(new URL(redirect)); 
     59            } catch (MalformedURLException e) { 
     60                // TODO Bloco catch gerado automaticamente 
     61                if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     62                    e.printStackTrace(); 
     63                } 
     64            } 
     65        } 
     66        else if (Integer.parseInt(answer[0].trim()) == 0) { 
     67 
     68            tryAgain = false; 
     69            // Pega usuário e senha de credentials[1] e credentials[2], respectivamente 
     70            // adiciona na página e faz o submit. 
     71            JSObject document = (JSObject) JSObject.getWindow(this).getMember("document"); 
     72            JSObject loginForm = (JSObject) document.getMember("flogin"); 
     73            JSObject loginField = (JSObject) loginForm.getMember("user"); 
     74            loginField.setMember("value", answer[1].trim()); 
     75 
     76            JSObject passwdField = (JSObject) loginForm.getMember("passwd"); 
     77            passwdField.setMember("value", answer[2].trim()); 
     78 
     79            loginForm.call("submit", null); 
     80            Thread.yield(); 
     81 
     82        } else if (Integer.parseInt(answer[0].trim()) == 6) { 
     83 
     84            tryAgain = false; 
     85 
     86            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     87                System.out.println("Mensagem de retorno: " + answer[1].trim()); 
     88            } 
     89 
     90            DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), answer[1].trim(), this.setup); 
     91 
     92            String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() 
     93                    + ":" + this.getCodeBase().getPort() + "/login.php?cd=98&ts=202"; 
     94            try { 
     95                this.getAppletContext().showDocument(new URL(redirect)); 
     96            } catch (MalformedURLException e) { 
     97                // TODO Bloco catch gerado automaticamente 
     98                if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
     99                    e.printStackTrace(); 
     100                } 
     101            } 
     102        } else { 
     103            tryAgain = true; 
     104            dc.destroy(); 
     105            System.gc(); 
     106 
     107            if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
     108                System.out.println("Mensagem de retorno: " + answer[1].trim()); 
     109            } 
     110 
     111            // Mostra mensagem de erro para o usuário 
     112            DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), answer[1].trim(), this.setup); 
     113            Thread.yield(); 
     114        } 
     115 
     116        return tryAgain; 
     117    } 
     118 
     119    private boolean parseHandleCertificateResponse(String certificate){ 
     120         
     121        // Envia certificado 
     122         
     123        JSObject document = (JSObject) JSObject.getWindow(this).getMember("document"); 
     124        JSObject certificateForm = (JSObject) document.getMember("certificateForm"); 
     125        JSObject certificateField = (JSObject) certificateForm.getMember("certificado"); 
     126        certificateField.setMember("value", certificate); 
     127 
     128        // submit e cai fora 
     129        certificateForm.call("submit", null); 
     130        
     131        dc.destroy(); 
     132        System.gc(); 
     133        Thread.yield(); 
     134 
     135        return false; 
     136    } 
    45137 
    46138    /* (non-Javadoc) 
     
    57149 
    58150            // Cria uma instância de DigitalCertificate e a inicializa 
     151            // Aqui pega document base e verifica em que aplicação estamos. 
     152 
    59153            this.dc = new DigitalCertificate(this.getDocumentBase(), setup); 
    60154            useCertificate = dc.init(); 
    61             String[] returnCode = null; 
    62155 
    63156            try { 
     157 
     158                String redirect = ""; 
     159 
     160                // Testa em qual aplicação estamos. 
     161                URL documentURL = this.getDocumentBase(); 
     162 
     163                if (documentURL.getPath().matches(".*login.php$")){ 
     164                    redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() 
     165                        + ":" + this.getCodeBase().getPort() + "/login.php"; 
     166                } 
     167                else { 
     168                    redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() 
     169                        + ":" + this.getCodeBase().getPort() + "/preferences/index.php"; 
     170                } 
     171 
    64172                switch (useCertificate) { 
    65173                    case DigitalCertificate.KEYSTORE_DETECTED: 
    66174                        // Mostra PinNeedeDialog. 
    67175                        String pin = DialogBuilder.showPinDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup); 
     176                         
    68177                        if (pin != null) { 
    69178                            dc.openKeyStore(pin.toCharArray()); 
    70                             returnCode = dc.getCredentials(pin); 
    71  
    72                             // Faz o login 
    73                             if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    74                                 System.out.println("Código de retorno: " + returnCode[0].trim()); 
     179                            if (documentURL.getPath().matches(".*login.php$")){ 
     180                                tryAgain = parseVercert(dc.getCredentials(pin, new URL(this.getCodeBase().getProtocol()+"://" + 
     181                                    this.getCodeBase().getHost() + ":" + this.getCodeBase().getPort() + 
     182                                    "/security/vercert.php"))); 
    75183                            } 
    76  
    77                             if (returnCode == null){ // Ação cancelada 
    78                                 tryAgain = false; 
    79                                 String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php"; 
    80                                 try { 
    81                                     this.getAppletContext().showDocument(new URL(redirect)); 
    82                                 } catch (MalformedURLException e) { 
    83                                     // TODO Bloco catch gerado automaticamente 
    84                                     if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    85                                         e.printStackTrace(); 
    86                                     } 
    87                                 } 
     184                            else { 
     185                                tryAgain = parseHandleCertificateResponse(dc.getPEMCertificate()); 
    88186                            } 
    89                             else if (Integer.parseInt(returnCode[0].trim()) == 0) { 
    90  
    91                                 tryAgain = false; 
    92                                 // Pega usuário e senha de credentials[1] e credentials[2], respectivamente 
    93                                 // adiciona na página e faz o submit. 
    94                                 JSObject document = (JSObject) JSObject.getWindow(this).getMember("document"); 
    95                                 JSObject loginForm = (JSObject) document.getMember("flogin"); 
    96                                 JSObject loginField = (JSObject) loginForm.getMember("user"); 
    97                                 loginField.setMember("value", returnCode[1].trim()); 
    98  
    99                                 JSObject passwdField = (JSObject) loginForm.getMember("passwd"); 
    100                                 passwdField.setMember("value", returnCode[2].trim()); 
    101  
    102                                 loginForm.call("submit", null); 
    103                                 Thread.yield(); 
    104  
    105                             } else if (Integer.parseInt(returnCode[0].trim()) == 6) { 
    106  
    107                                 tryAgain = false; 
    108  
    109                                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    110                                     System.out.println("Mensagem de retorno: " + returnCode[1].trim()); 
    111                                 } 
    112  
    113                                 DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), returnCode[1].trim(), this.setup); 
    114  
    115  
    116  
    117                                 String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php?cd=98&ts=202"; 
    118                                 try { 
    119                                     this.getAppletContext().showDocument(new URL(redirect)); 
    120                                 } catch (MalformedURLException e) { 
    121                                     // TODO Bloco catch gerado automaticamente 
    122                                     if (this.setup.getParameter("debug").equalsIgnoreCase("true")) { 
    123                                         e.printStackTrace(); 
    124                                     } 
    125                                 } 
    126                             } else { 
    127                                 tryAgain = true; 
    128                                 dc.destroy(); 
    129                                 System.gc(); 
    130  
    131                                 if (setup.getParameter("debug").equalsIgnoreCase("true")) { 
    132                                     System.out.println("Mensagem de retorno: " + returnCode[1].trim()); 
    133                                 } 
    134  
    135                                 // Mostra mensagem de erro para o usuário 
    136                                 DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), returnCode[1].trim(), this.setup); 
    137                                 Thread.yield(); 
    138                             } 
    139187 
    140188                        } else { 
    141189 
     190                            // TODO: Notifica usuário 
    142191                            tryAgain = false; 
    143                             String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php"; 
    144192                            try { 
    145193                                this.getAppletContext().showDocument(new URL(redirect)); 
     
    155203                    default: 
    156204 
    157                         // Mostra Diálogo dizendo que token não foi encontrado 
     205                        // TODO: notifica usuário que token não foi encontrado 
    158206                        // ou repositório de chaves públicas não foi configurado. 
    159207                        // Tentar carregar token/keystore novamente? / Logon sem certificado digital? 
     
    167215                        System.gc(); 
    168216 
    169                         String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php"; 
    170217                        try { 
    171218                            this.getAppletContext().showDocument(new URL(redirect)); 
Note: See TracChangeset for help on using the changeset viewer.