3 * This file is part of NixNote
4 * Copyright 2012 Randy Baumgarte
6 * This file may be licensed under the terms of of the
7 * GNU General Public License Version 2 (the ``GPL'').
9 * Software distributed under the License is distributed
10 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
11 * express or implied. See the GPL for the specific language
12 * governing rights and limitations.
14 * You should have received a copy of the GPL along with this
15 * program. If not, go to http://www.gnu.org/licenses/gpl.html
16 * or write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* This method is used to present the user with the web view of Evernote
23 * that they need to grant permission to Nixnote.
27 package cx.fbn.nevernote.oauth;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLDecoder;
32 import com.trolltech.qt.core.QUrl;
33 import com.trolltech.qt.gui.QDialog;
34 import com.trolltech.qt.gui.QGridLayout;
35 import com.trolltech.qt.gui.QIcon;
36 import com.trolltech.qt.network.QSslSocket;
37 import com.trolltech.qt.webkit.QWebView;
39 import cx.fbn.nevernote.Global;
40 import cx.fbn.nevernote.utilities.ApplicationLogger;
42 public class OAuthWindow extends QDialog {
43 private final static String consumerKey = "baumgarr";
44 private final static String consumerSecret = "60d4cdedb074b0ac";
45 public String response;
47 private final String temporaryCredUrl;
48 private final String permanentCredUrl;
51 static final String urlBase = "https://"+Global.getServer();
54 public String errorMessage;
56 static final String requestTokenUrl = urlBase + "/oauth";
57 static final String accessTokenUrl = urlBase + "/oauth";
58 static final String authorizationUrlBase = urlBase + "/OAuth.action";
59 private final String iconPath = new String("classpath:cx/fbn/nevernote/");
60 private final QWebView tempPage;
61 private final QWebView authPage;
62 private final QGridLayout grid;
63 private NNOAuthNetworkAccessManager manager;
65 static final String callbackUrl = "index.jsp?action=callbackReturn";
66 private final ApplicationLogger logger;
70 public OAuthWindow(ApplicationLogger l) {
72 int millis = (int) System.currentTimeMillis();
73 int time = millis / 1000;
76 // Create the URLs needed for authentication with Evernote
77 temporaryCredUrl = "https://"+Global.getServer() + "/oauth?oauth_consumer_key=" +consumerKey + "&oauth_signature=" +
78 consumerSecret + "%26&oauth_signature_method=PLAINTEXT&oauth_timestamp="+String.valueOf(time)+
79 "&oauth_nonce="+String.valueOf(millis) +"&oauth_callback=nnoauth";
81 permanentCredUrl = "https://"+Global.getServer() + "/oauth?oauth_consumer_key=" +consumerKey + "&oauth_signature=" +
82 consumerSecret + "%26&oauth_signature_method=PLAINTEXT&oauth_timestamp="+String.valueOf(time)+
83 "&oauth_nonce="+String.valueOf(millis) +"&oauth_token=";
87 setWindowTitle(tr("Please Grant Nixnote Access"));
88 setWindowIcon(new QIcon(iconPath+"icons/password.png"));
89 grid = new QGridLayout();
91 tempPage = new QWebView();
92 authPage = new QWebView();
93 grid.addWidget(authPage);
94 tempPage.loadFinished.connect(this, "temporaryCredentialsReceived()");
99 // Check that SSL sockets are supported
100 logger.log(logger.MEDIUM, "SSL Sockets Supported: " +QSslSocket.supportsSsl());
101 if (!QSslSocket.supportsSsl()) {
102 errorMessage = new String(tr("SSL Support not found. Aborting connection"));
106 // Load the temporary URL to start the authentication procesess. When
107 // finished, this QWebView will contain the URL to start the
108 // authentication process.
109 QUrl tu = new QUrl(temporaryCredUrl);
114 // This method is triggered when the temporary credentials are received from Evernote
115 public void temporaryCredentialsReceived() {
116 logger.log(logger.MEDIUM, "Temporary Credentials Received");
117 String contents = tempPage.page().mainFrame().toPlainText();
118 logger.log(logger.MEDIUM, "Temporary Credentials:" +contents);
119 int index = contents.indexOf("&oauth_token_secret");
120 contents = contents.substring(0,index);
121 QUrl accessUrl = new QUrl(urlBase+"/OAuth.action?" +contents);
122 manager = new NNOAuthNetworkAccessManager(logger);
123 authPage.page().setNetworkAccessManager(manager);
124 manager.tokenFound.connect(this, "tokenFound(String)");
126 authPage.load(accessUrl);
127 grid.addWidget(authPage);
130 // This method is signaled when NNOAuthNetworkAccessManager finds an OAuth token
131 // in the network request.
132 public void tokenFound(String token) {
133 logger.log(logger.MEDIUM, "*** TOKEN *** " +token);
134 if (token.indexOf("auth_verifier") <= 0) {
135 errorMessage = new String(tr("Error receiving authorization"));
139 tempPage.disconnect();
140 tempPage.loadFinished.connect(this, "permanentCredentialsReceived()");
141 logger.log(logger.HIGH,"Permanent URL: " +permanentCredUrl+token);
142 tempPage.load(new QUrl(permanentCredUrl+token));
146 // This method is used when the permanent credentials are finally
147 // received to grant access to Evernote.
148 public void permanentCredentialsReceived() {
149 String contents = tempPage.page().mainFrame().toPlainText();
150 if (contents.startsWith("oauth_token=S%3D")) {
151 logger.log(logger.HIGH, "Permanent Credentials:" +contents);
155 decoded = URLDecoder.decode(contents,"UTF-8");
156 logger.log(logger.HIGH, "Decoded URL:"+decoded);
158 } catch (UnsupportedEncodingException e) {