OSDN Git Service

Change authentication from userid/password to oauth
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / oauth / OAuthWindow.java
1
2 /*
3   * This file is part of NixNote 
4  * Copyright 2012 Randy Baumgarte
5  * 
6  * This file may be licensed under the terms of of the
7  * GNU General Public License Version 2 (the ``GPL'').
8  *
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.
13  *
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.
18  *
19 */
20
21
22 /* This method is used to present the user with the web view of Evernote
23  * that they need to grant permission to Nixnote.
24  */
25
26
27 package cx.fbn.nevernote.oauth;
28
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLDecoder;
31
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;
38
39 import cx.fbn.nevernote.Global;
40 import cx.fbn.nevernote.utilities.ApplicationLogger;
41
42 public class OAuthWindow extends QDialog {
43         private final static String consumerKey = "baumgarr"; 
44         private final static String consumerSecret = "60d4cdedb074b0ac";
45         public String response;
46
47         private final String temporaryCredUrl;    
48         private final String permanentCredUrl;
49
50
51         static final String urlBase = "https://"+Global.getServer();
52
53         public boolean error;
54         public String errorMessage;
55
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;
64
65         static final String callbackUrl = "index.jsp?action=callbackReturn";
66         private final ApplicationLogger logger;
67
68
69         // Constructor.
70         public OAuthWindow(ApplicationLogger l) {
71                 logger = l;
72                 int millis = (int) System.currentTimeMillis();
73                 int time = millis / 1000;
74
75
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";
80
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=";
84
85
86                 // Build the window
87                 setWindowTitle(tr("Please Grant Nixnote Access"));
88                 setWindowIcon(new QIcon(iconPath+"icons/password.png"));
89                 grid = new QGridLayout();
90                 setLayout(grid);
91                 tempPage = new QWebView();
92                 authPage = new QWebView();
93                 grid.addWidget(authPage);
94                 tempPage.loadFinished.connect(this, "temporaryCredentialsReceived()");
95
96                 error = false;
97                 errorMessage = "";
98                 
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"));
103                         error = true;
104                 }
105                 
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);
110                 tempPage.load(tu);
111         }
112
113         
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)");
125
126                 authPage.load(accessUrl);  
127                 grid.addWidget(authPage);
128         }
129
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"));
136                         error = true;
137                         this.close();
138                 }
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));
143         }
144
145         
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);
152                         String decoded;
153                         try {
154                                 response = "";
155                                 decoded = URLDecoder.decode(contents,"UTF-8");
156                                 logger.log(logger.HIGH, "Decoded URL:"+decoded);
157                                 response = decoded;
158                         } catch (UnsupportedEncodingException e) {
159                                 e.printStackTrace();
160                         }
161
162                         this.close();
163                 }
164         }
165
166 }