Permalink
Browse files

A bunch of work to implement PIN-based re-authorization of existing a…

…ccounts. Still need to do new accounts this way.
  • Loading branch information...
1 parent 556261d commit 34c60a31ebc1d9fdfb6a4a3382f76068907b0a8f @funkatron funkatron committed May 28, 2011
Showing with 1,129 additions and 21 deletions.
  1. +34 −16 app/spaz.js
  2. +149 −2 app/views/spaz.accountprefs.js
  3. +16 −3 index.html
  4. +930 −0 vendors/jsOAuth-1.1.js
View
@@ -84,6 +84,17 @@ Spaz.loadOAuthServices = function() {
};
+Spaz.getQueryVars = function(qstring) {
+ var qvars = [];
+ var qvars_tmp = qstring.split('&');
+ for (var i = 0; i < qvars_tmp.length; i++) {;
+ var y = qvars_tmp[i].split('=');
+ qvars[y[0]] = decodeURIComponent(y[1]);
+ };
+ return qvars;
+};
+
+
/**
* Bootstraps the app
*/
@@ -264,13 +275,7 @@ Spaz.initialize = function() {
*/
Spaz.Controller.initIntercept();
- /*
- if we have a username and password set, trigger an "account_switched" event
- to kick things off
- */
- if (Spaz.Prefs.getUsername() && Spaz.Prefs.getAccountType()) {
- sch.trigger('account_switched', document, Spaz.Prefs.getCurrentAccount());
- }
+
/*
@@ -358,9 +363,9 @@ Spaz.initialize = function() {
/*
- initialinze URL shortener
+ initialize URL shortener
*/
- var initUrlShortener = function() {
+ (function() {
var method;
@@ -411,9 +416,8 @@ Spaz.initialize = function() {
});
// sch.debug(air.NativeApplication.nativeApplication.spazPrefs);
- };
+ }());
- initUrlShortener();
/*
initialize Image uploader popbox
@@ -424,10 +428,24 @@ Spaz.initialize = function() {
// load User JS file
Spaz.loadUserJS();
- /*
- load news popup
- */
- setTimeout(Spaz.Newspopup.build, 3000);
- sch.debug('ended document.ready()');
+ if (Spaz.AccountPrefs.reauthTwitterAccounts()) {
+ //
+ } else {
+ /*
+ if we have a username and password set, trigger an "account_switched" event
+ to kick things off
+ */
+ if (Spaz.Prefs.getUsername() && Spaz.Prefs.getAccountType()) {
+ sch.trigger('account_switched', document, Spaz.Prefs.getCurrentAccount());
+ }
+
+ /*
+ load news popup
+ */
+ setTimeout(Spaz.Newspopup.build, 3000);
+
+ sch.debug('ended document.ready()');
+ }
+
};
@@ -164,6 +164,10 @@ Spaz.AccountPrefs.init = function(){
var id = Spaz.AccountPrefs.getSelectedAccountId();
if (id) {
var editing = that.spaz_acc.get(id);
+
+ $('#twitter-reauthorize').unbind('click').click(function() {
+ Spaz.AccountPrefs.authorizeTwitterAccount(editing);
+ });
Spaz.UI.openPopboxInline('#account-details');
@@ -173,11 +177,15 @@ Spaz.AccountPrefs.init = function(){
$idEdit.val(editing.id);
$username.val(editing.username).focus();
$password.val(editing.password);
+
+ $('#twitter-username').text(editing.username);
+ $('#twitter-pin-row').hide();
$accountType.val(editing.type).change((function(){
// Run `fn` immediately, then bind it as a callback
function fn(){
- var isOauth = ($accountType.val() === SPAZCORE_ACCOUNT_TWITTER);
- $password.add($password.siblings()).toggle(!isOauth);
+ var isTwitter = ($accountType.val() === SPAZCORE_ACCOUNT_TWITTER);
+ $('div.reauthorize-twitter-controls').toggle(isTwitter);
+ $('div.username-password-controls').toggle(!isTwitter);
}
fn();
return fn;
@@ -447,3 +455,142 @@ Spaz.AccountPrefs.updateWindowTitleAndToolsMenu = function(accountId){
});
};
+
+
+
+Spaz.AccountPrefs.getOauth = function() {
+ return OAuth({
+ 'consumerKey':SPAZCORE_CONSUMERKEY_TWITTER,
+ 'consumerSecret':SPAZCORE_CONSUMERSECRET_TWITTER,
+ 'requestTokenUrl':'https://twitter.com/oauth/request_token',
+ 'authorizationUrl':'https://twitter.com/oauth/authenticate',
+ 'accessTokenUrl':'https://twitter.com/oauth/access_token'
+ });
+};
+
+
+/**
+ * check to find twitter accounts that need upgrading
+ */
+Spaz.AccountPrefs.getOldTwitterAccounts = function() {
+ var twitter_accts = Spaz.Prefs._accounts.getByType(SPAZCORE_SERVICE_TWITTER);
+ var matches = [];
+ for (var i=0; i < twitter_accts.length; i++) {
+ if (!!!(Spaz.Prefs._accounts.getMeta(twitter_accts[i].id, 'twitter_dm_access'))) {
+ matches.push(twitter_accts[i]);
+ air.trace(JSON.stringify(twitter_accts[i]));
+ }
+ }
+ return matches;
+};
+
+
+
+
+Spaz.AccountPrefs.reauthTwitterAccounts = function() {
+
+ var matches = Spaz.AccountPrefs.getOldTwitterAccounts();
+
+ if (matches.length > 0) {
+ var go = confirm([
+ "Twitter has chosen to change how 3rd party applications connect to their service.\n\n ",
+ "As a consequence, you need to re-authorize your Twitter accounts to have continued access",
+ " to direct messages on the system.\n\n",
+ "Click OK to re-authorize now."
+ ].join('')
+ );
+ if (go) {
+
+ // loop through accounts, authorizing each one
+ (function reauth(new_matches) {
+
+ if (new_matches) {
+ matches = new_matches;
+ }
+
+ if (matches.length < 1) {
+ return;
+ }
+
+ alert("Let's try to re-authorize "+matches[0].username+"@twitter");
+
+ $('#account-list li[data-account-id="'+matches[0].id+'"]').trigger('click');
+ setTimeout(function() {
+ $('#edit-account').trigger('click');
+ setTimeout(function() {
+ Spaz.AccountPrefs.authorizeTwitterAccount(matches[0], function(last_acc_id) {
+ matches.splice(0,1);
+ reauth(matches);
+ });
+ }, 100);
+ }, 100);
+ })();
+
+ return true;
+
+ }
+
+ return false;
+
+
+ } else {
+ return false;
+ }
+};
+
+
+
+Spaz.AccountPrefs.authorizeTwitterAccount = function(acc_obj, success) {
+
+ var oauth = Spaz.AccountPrefs.getOauth();
+
+ oauth.fetchRequestToken(function(url) {
+ if (acc_obj && acc_obj.username) {
+ url += '&force_login=true&screen_name='+acc_obj.username;
+ } else {
+ url += '&force_login=true';
+ }
+ var auth_window = window.open(url, 'authorize', 'height=500,width=500');
+
+ $('#twitter-pin-row').show();
+ $('#twitter-pin').val(''); // clear
+ $('#twitter-pin-ok').unbind('click').click(function() {
+ if ($('#twitter-pin').val()) {
+ oauth.setVerifier($('#twitter-pin').val());
+ if (auth_window) {
+ auth_window.close();
+ }
+ oauth.fetchAccessToken(function(data) {
+ var acc_id;
+ var qvars = Spaz.getQueryVars(data.text);
+ var auth_pickle = qvars.screen_name+':'+qvars.oauth_token+':'+qvars.oauth_token_secret;
+ if (acc_obj && acc_obj.id) { // edit existing
+ Spaz.Prefs._accounts.setAuthKey(acc_obj.id, auth_pickle);
+ // Spaz.Prefs._accounts.setMeta(acc_obj.id, 'twitter_dm_access', true);
+ acc_id = acc_obj.id;
+ } else { // add new
+ acc_id = App.Users.add(qvars.screen_name.toLowerCase(), auth_pickle, type);
+ App.Users.setMeta(acc_id, 'twitter-api-base-url', api_base_url);
+ App.Users.setMeta(acc_id, 'twitter_dm_access', true);
+ }
+ $('#twitter-pin-row').hide();
+ alert($L('Account re-authorized'));
+ if (success) {
+ success(acc_id);
+ }
+ },
+ function(data) {
+ alert($L('Problem getting access token from Twitter'));
+ }
+ );
+ } else {
+ alert($L('You need to enter a PIN'));
+ }
+ });
+ },
+ function(data) {
+ alert($L('Problem getting Request Token from Twitter'));
+ }
+ );
+
+};
View
@@ -32,6 +32,9 @@
<script src="vendors/BlenderEffect.js" type="text/javascript" charset="utf-8"></script>
<script src="vendors/Tween.js" type="text/javascript" charset="utf-8"></script>
+ <!-- jsOAuth -->
+ <script src="vendors/jsOAuth-1.1.js" type="text/javascript" charset="utf-8"></script>
+
<!-- jQuery libs -->
<script src="vendors/jquery/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script>
@@ -478,12 +481,22 @@ <h2 class="CollapsiblePanelTab" tabindex="0">
<div class="content">
<!-- a place to store the ID we're editing -->
<input type="hidden" name="id_edit" value="" id="id_edit" />
-
- <div class="formrow">
+
+ <div class="formrow reauthorize-twitter-controls">
+ <h2 id="twitter-username"></h2>
+ <button id="twitter-reauthorize" class="primary">Re-Authorize</button>
+ </div>
+ <div class="formrow reauthorize-twitter-controls" id="twitter-pin-row">
+ <label for="twitter-pin">PIN</label>
+ <input type="text" name="twitter-pin" id="twitter-pin" value="" />
+ <button id="twitter-pin-ok" class="primary">OK</button>
+ </div>
+
+ <div class="formrow username-password-controls">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="" />
</div>
- <div class="formrow">
+ <div class="formrow username-password-controls">
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" />
</div>
Oops, something went wrong.

0 comments on commit 34c60a3

Please sign in to comment.