[1174] | 1 | |
---|
| 2 | import java.awt.Frame; |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.net.MalformedURLException; |
---|
| 5 | import java.net.URL; |
---|
| 6 | import java.security.GeneralSecurityException; |
---|
| 7 | import java.security.ProviderException; |
---|
| 8 | |
---|
| 9 | import javax.net.ssl.SSLHandshakeException; |
---|
| 10 | import javax.swing.JApplet; |
---|
| 11 | import javax.swing.JOptionPane; |
---|
| 12 | import javax.swing.SwingUtilities; |
---|
| 13 | import org.apache.commons.httpclient.HttpException; |
---|
| 14 | |
---|
| 15 | import netscape.javascript.JSObject; |
---|
| 16 | |
---|
| 17 | import br.gov.serpro.cert.DigitalCertificate; |
---|
| 18 | import br.gov.serpro.setup.Setup; |
---|
| 19 | import br.gov.serpro.ui.DialogBuilder; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * GUI que realiza o login com certificados A1 e A3 ou login com usuário e senha no expresso. |
---|
| 23 | * Esta classe estende a classe JApplet |
---|
| 24 | * @author Mário César Kolling - mario.kolling@serpro.gov.br |
---|
| 25 | */ |
---|
[3633] | 26 | public class LoginApplet extends JApplet { |
---|
[1174] | 27 | |
---|
[3633] | 28 | /** |
---|
| 29 | * Valor gerado aleatoriamente |
---|
| 30 | */ |
---|
| 31 | //TODO: Alterar a cor e fonte dos labels e das caixas de texto e senha. |
---|
| 32 | private static final long serialVersionUID = -6204158613173951516L; |
---|
| 33 | private DigitalCertificate dc; |
---|
[1174] | 34 | private Setup setup; |
---|
| 35 | |
---|
[3633] | 36 | /* (non-Javadoc) |
---|
| 37 | * @see java.applet.Applet#init() |
---|
| 38 | */ |
---|
| 39 | public void init() { |
---|
| 40 | super.init(); |
---|
| 41 | this.setSize(0, 0); |
---|
[1174] | 42 | this.setup = new Setup(this); |
---|
| 43 | this.setup.addLanguageResource("ExpressoCertLoginMessages"); |
---|
[3633] | 44 | } |
---|
[1174] | 45 | |
---|
[3633] | 46 | /* (non-Javadoc) |
---|
| 47 | * @see java.applet.Applet#start() |
---|
| 48 | */ |
---|
| 49 | @Override |
---|
| 50 | public void start() { |
---|
| 51 | super.start(); |
---|
[1174] | 52 | |
---|
[3633] | 53 | int useCertificate = DigitalCertificate.KEYSTORE_NOT_DETECTED; |
---|
| 54 | boolean tryAgain = true; |
---|
[1174] | 55 | |
---|
[3633] | 56 | do { |
---|
[1174] | 57 | |
---|
[3633] | 58 | // Cria uma instância de DigitalCertificate e a inicializa |
---|
| 59 | this.dc = new DigitalCertificate(this.getDocumentBase(), setup); |
---|
| 60 | useCertificate = dc.init(); |
---|
| 61 | String[] returnCode = null; |
---|
[1174] | 62 | |
---|
[3633] | 63 | try { |
---|
| 64 | switch (useCertificate) { |
---|
| 65 | case DigitalCertificate.KEYSTORE_DETECTED: |
---|
| 66 | // Mostra PinNeedeDialog. |
---|
| 67 | String pin = DialogBuilder.showPinDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup); |
---|
| 68 | if (pin != null) { |
---|
| 69 | dc.openKeyStore(pin.toCharArray()); |
---|
| 70 | returnCode = dc.getCredentials(pin); |
---|
[1174] | 71 | |
---|
[3633] | 72 | // Faz o login |
---|
[1174] | 73 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 74 | System.out.println("Código de retorno: " + returnCode[0].trim()); |
---|
| 75 | } |
---|
| 76 | |
---|
[3633] | 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 | } |
---|
| 88 | } |
---|
| 89 | else if (Integer.parseInt(returnCode[0].trim()) == 0) { |
---|
[1174] | 90 | |
---|
[3633] | 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()); |
---|
[1174] | 98 | |
---|
[3633] | 99 | JSObject passwdField = (JSObject) loginForm.getMember("passwd"); |
---|
| 100 | passwdField.setMember("value", returnCode[2].trim()); |
---|
[1174] | 101 | |
---|
[3633] | 102 | loginForm.call("submit", null); |
---|
| 103 | Thread.yield(); |
---|
[1174] | 104 | |
---|
[3633] | 105 | } else if (Integer.parseInt(returnCode[0].trim()) == 6) { |
---|
[1174] | 106 | |
---|
[3633] | 107 | tryAgain = false; |
---|
[1174] | 108 | |
---|
[3633] | 109 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
[1174] | 110 | System.out.println("Mensagem de retorno: " + returnCode[1].trim()); |
---|
| 111 | } |
---|
| 112 | |
---|
[3633] | 113 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), returnCode[1].trim(), this.setup); |
---|
[1174] | 114 | |
---|
| 115 | |
---|
| 116 | |
---|
[3633] | 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")) { |
---|
[1174] | 123 | e.printStackTrace(); |
---|
| 124 | } |
---|
[3633] | 125 | } |
---|
| 126 | } else { |
---|
| 127 | tryAgain = true; |
---|
| 128 | dc.destroy(); |
---|
| 129 | System.gc(); |
---|
[1174] | 130 | |
---|
| 131 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 132 | System.out.println("Mensagem de retorno: " + returnCode[1].trim()); |
---|
| 133 | } |
---|
| 134 | |
---|
[3633] | 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 | } |
---|
[1174] | 139 | |
---|
[3633] | 140 | } else { |
---|
[1174] | 141 | |
---|
[3633] | 142 | tryAgain = false; |
---|
| 143 | String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php"; |
---|
| 144 | try { |
---|
| 145 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 146 | } catch (MalformedURLException e) { |
---|
| 147 | // TODO Bloco catch gerado automaticamente |
---|
[1174] | 148 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 149 | e.printStackTrace(); |
---|
| 150 | } |
---|
[3633] | 151 | } |
---|
| 152 | } |
---|
[1174] | 153 | |
---|
[3633] | 154 | break; |
---|
| 155 | default: |
---|
[1174] | 156 | |
---|
[3633] | 157 | // Mostra Diálogo dizendo que token não foi encontrado |
---|
| 158 | // ou repositório de chaves públicas não foi configurado. |
---|
| 159 | // Tentar carregar token/keystore novamente? / Logon sem certificado digital? |
---|
[1174] | 160 | |
---|
[3633] | 161 | tryAgain = false; |
---|
[1174] | 162 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 163 | System.out.println("não achou token"); |
---|
| 164 | } |
---|
| 165 | |
---|
[3633] | 166 | dc.destroy(); |
---|
| 167 | System.gc(); |
---|
| 168 | |
---|
| 169 | String redirect = this.getCodeBase().getProtocol() + "://" + this.getCodeBase().getHost() + "/login.php"; |
---|
| 170 | try { |
---|
| 171 | this.getAppletContext().showDocument(new URL(redirect)); |
---|
| 172 | } catch (MalformedURLException e) { |
---|
| 173 | // TODO Bloco catch gerado automaticamente |
---|
[1174] | 174 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 175 | e.printStackTrace(); |
---|
| 176 | } |
---|
[3633] | 177 | } |
---|
| 178 | } |
---|
[1174] | 179 | |
---|
[3633] | 180 | } catch (SSLHandshakeException e) { |
---|
[1174] | 181 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 182 | e.printStackTrace(); |
---|
| 183 | } |
---|
[3633] | 184 | dc.destroy(); |
---|
| 185 | System.gc(); |
---|
[1174] | 186 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
[3633] | 187 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet001"), this.setup); |
---|
[1174] | 188 | |
---|
[3633] | 189 | Thread.yield(); |
---|
[1174] | 190 | |
---|
[3633] | 191 | tryAgain = true; |
---|
| 192 | } catch (HttpException e) { |
---|
[1174] | 193 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 194 | e.printStackTrace(); |
---|
| 195 | } |
---|
[3633] | 196 | |
---|
| 197 | tryAgain = true; |
---|
| 198 | Thread.yield(); |
---|
| 199 | } catch (GeneralSecurityException e) { |
---|
[1174] | 200 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 201 | e.printStackTrace(); |
---|
| 202 | } |
---|
[3633] | 203 | |
---|
[1174] | 204 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
[3633] | 205 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet002"), this.setup); |
---|
[1174] | 206 | |
---|
[3633] | 207 | Thread.yield(); |
---|
| 208 | tryAgain = true; |
---|
| 209 | } catch (IOException e) { |
---|
| 210 | dc.destroy(); |
---|
| 211 | System.gc(); |
---|
[1174] | 212 | |
---|
| 213 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 214 | e.printStackTrace(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | Throwable cause = null; |
---|
[3633] | 218 | if ((cause = e.getCause()) != null) { |
---|
| 219 | if (cause instanceof javax.security.auth.login.LoginException) { |
---|
[1174] | 220 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet003"), this.setup); |
---|
| 221 | } else { |
---|
| 222 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 223 | System.out.println("Exception lançada: " + cause.getClass().getCanonicalName()); |
---|
| 224 | } |
---|
| 225 | } |
---|
[3633] | 226 | } else { |
---|
| 227 | if (e instanceof java.net.ConnectException) { |
---|
[1174] | 228 | DialogBuilder.showMessageDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
| 229 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet004"), this.setup); |
---|
| 230 | } else { |
---|
| 231 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 232 | System.out.println("Exception lançada: " + e.getClass().getCanonicalName()); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[3633] | 237 | Thread.yield(); |
---|
[1174] | 238 | tryAgain = true; |
---|
[3633] | 239 | } catch (ProviderException e) { |
---|
[1174] | 240 | |
---|
[3633] | 241 | dc.destroy(); |
---|
| 242 | System.gc(); |
---|
[1174] | 243 | |
---|
| 244 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 245 | e.printStackTrace(); |
---|
| 246 | } |
---|
| 247 | |
---|
[3633] | 248 | int resultado = DialogBuilder.showConfirmDialog((Frame) SwingUtilities.getAncestorOfClass(Frame.class, this), |
---|
| 249 | //"Deseja tentar carregá-lo novamente?", |
---|
[1174] | 250 | this.setup.getLang("ExpressoCertLoginMessages", "LoginApplet005"), |
---|
[3633] | 251 | JOptionPane.QUESTION_MESSAGE, |
---|
| 252 | JOptionPane.OK_CANCEL_OPTION, this.setup); |
---|
[1174] | 253 | |
---|
[3633] | 254 | if (resultado == JOptionPane.OK_OPTION) { |
---|
| 255 | tryAgain = true; |
---|
| 256 | } else { |
---|
| 257 | tryAgain = false; |
---|
| 258 | } |
---|
[1174] | 259 | |
---|
[3633] | 260 | Thread.yield(); |
---|
| 261 | } |
---|
[1174] | 262 | |
---|
[3633] | 263 | } while (tryAgain); |
---|
[1174] | 264 | |
---|
[3633] | 265 | } |
---|
[1174] | 266 | |
---|
[3633] | 267 | /** |
---|
| 268 | * Destrói a Applet, executando códigos para desregistrar tokens, keystores, etc. |
---|
| 269 | */ |
---|
| 270 | @Override |
---|
| 271 | public void stop() { |
---|
| 272 | //super.destroy(); |
---|
[1174] | 273 | if (setup.getParameter("debug").equalsIgnoreCase("true")) { |
---|
| 274 | System.out.println("Finalizando Applet de Login!"); |
---|
| 275 | } |
---|
| 276 | |
---|
[3633] | 277 | this.dc.destroy(); |
---|
| 278 | this.dc = null; |
---|
| 279 | System.gc(); |
---|
| 280 | } |
---|
[1174] | 281 | } |
---|