comparison src/main/java/com/glavsoft/rfb/protocol/state/AuthenticationState.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children 17b702648079
comparison
equal deleted inserted replaced
-1:000000000000 0:4689cc86d6cb
1 // Copyright (C) 2010, 2011 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.rfb.protocol.state;
26
27 import com.glavsoft.exceptions.AuthenticationFailedException;
28 import com.glavsoft.exceptions.ClosedConnectionException;
29 import com.glavsoft.exceptions.FatalException;
30 import com.glavsoft.exceptions.TransportException;
31 import com.glavsoft.exceptions.UnsupportedProtocolVersionException;
32 import com.glavsoft.exceptions.UnsupportedSecurityTypeException;
33 import com.glavsoft.rfb.protocol.ProtocolContext;
34 import com.glavsoft.rfb.protocol.auth.AuthHandler;
35
36 public class AuthenticationState extends ProtocolState {
37
38 private static final int AUTH_RESULT_OK = 0;
39 // private static final int AUTH_RESULT_FAILED = 1;
40 // private static final int AUTH_RESULT_TOO_MANY = 2;
41 private final AuthHandler authHandler;
42
43 public AuthenticationState(ProtocolContext context,
44 AuthHandler authHandler) {
45 super(context);
46 this.authHandler = authHandler;
47 }
48
49 @Override
50 public boolean next() throws UnsupportedProtocolVersionException, TransportException,
51 UnsupportedSecurityTypeException, AuthenticationFailedException, FatalException {
52 authenticate();
53 return true;
54 }
55
56 private void authenticate() throws TransportException, AuthenticationFailedException,
57 FatalException, UnsupportedSecurityTypeException {
58 boolean isTight = authHandler.authenticate(reader, writer,
59 context.getSettings().authCapabilities, context.getPasswordRetriever());
60 // skip when protocol < 3.8 and NONE_AUTH
61 if (authHandler.useSecurityResult()) {
62 checkSecurityResult();
63 }
64 changeStateTo(isTight ? new InitTightState(context) : new InitState(context));
65 context.getSettings().setTight(isTight);
66 }
67
68 /**
69 * Check Security Result received from server
70 * May be:
71 * * 0 - OK
72 * * 1 - Failed
73 * @throws TransportException
74 * @throws AuthenticationFailedException
75 */
76 protected void checkSecurityResult() throws TransportException,
77 AuthenticationFailedException {
78 if (reader.readInt32() != AUTH_RESULT_OK) {
79 try {
80 String reason = reader.readString();
81 throw new AuthenticationFailedException(reason);
82 } catch (ClosedConnectionException e) {
83 // protocol version 3.3 and 3.7 does not send reason string,
84 // but silently closes the connection
85 throw new AuthenticationFailedException("Authentication failed");
86 }
87 }
88 }
89 }