add missing part of last commit, sending and retrieving account cookie

This commit is contained in:
Shane Caraveo 2011-02-16 19:58:06 -08:00
Родитель beac8857d3
Коммит 9a5535af05
6 изменённых файлов: 56 добавлений и 31 удалений

Просмотреть файл

@ -30,7 +30,7 @@ from pylons.decorators import jsonify
from pylons.decorators.util import get_pylons
from linkdrop.lib.base import BaseController, render
from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg
from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg, get_redirect_response
from linkdrop.lib.metrics import metrics
from linkdrop.lib.oauth import get_provider
from linkdrop.lib.oauth.base import AccessException
@ -144,7 +144,9 @@ OAuth authorization api.
except Exception, e:
log.exception('failed to verify the account')
self._redirectException(e)
return redirect(session.get('end_point_success', config.get('oauth_success')))
resp = get_redirect_response(session.get('end_point_success', config.get('oauth_success')))
resp.set_cookie('account_tokens', urllib.quote(json.dumps(acct.to_dict())))
raise resp.exception
def _redirectException(self, e):
err = urllib.urlencode([('error',str(e))])

Просмотреть файл

@ -31,7 +31,7 @@ from pylons.decorators import jsonify
from pylons.decorators.util import get_pylons
from linkdrop.lib.base import BaseController, render
from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg
from linkdrop.lib.helpers import json_exception_response, api_response, api_entry, api_arg, get_redirect_response
from linkdrop.lib.oauth import get_provider
from linkdrop.lib import constants
@ -44,25 +44,6 @@ from sqlalchemy import and_
log = logging.getLogger(__name__)
from webob.exc import status_map
def get_redirect_response(url, code=302, additional_headers=[]):
"""Raises a redirect exception to the specified URL
Optionally, a code variable may be passed with the status code of
the redirect, ie::
redirect(url(controller='home', action='index'), code=303)
XXX explain additional_headers
"""
exc = status_map[code]
resp = exc(location=url)
for k,v in additional_headers:
resp.headers.add(k, v)
return resp
class LinksController(BaseController):
"""
Links

Просмотреть файл

@ -33,6 +33,7 @@ import pprint
from xml.sax.saxutils import escape
import json
from webhelpers.html import literal
from webob.exc import status_map
import logging
@ -42,6 +43,22 @@ from linkdrop.lib.metrics import metrics
log = logging.getLogger(__name__)
def get_redirect_response(url, code=302, additional_headers=[]):
"""Raises a redirect exception to the specified URL
Optionally, a code variable may be passed with the status code of
the redirect, ie::
redirect(url(controller='home', action='index'), code=303)
XXX explain additional_headers
"""
exc = status_map[code]
resp = exc(location=url)
for k,v in additional_headers:
resp.headers.add(k, v)
return resp
## {{{ http://code.activestate.com/recipes/52281/ (r1) PSF License
import sgmllib, string

Просмотреть файл

@ -241,7 +241,7 @@
</style>
<script>
require(["require", "jquery", "blade/url"],
require(["require", "jquery", "blade/url", "jquery.cookie"],
function (require, $, url) {
var target = window.location.href.split('#')[1];
if (target && (target === 'oauth_success' || target === 'oauth_failure')) {
@ -251,11 +251,14 @@
// XXX hacky way to handle fennec, since we didn't open a window,
// catch the exception when using window.opener and redirect
try {
window.opener.postMessage(target, '*');
window.close();
var data = {
target: target,
account: JSON.parse($.cookie("account_tokens"))
};
window.opener.postMessage(JSON.stringify(data), '*');
window.close();
} catch(e) {
var url = location.protocol + "//" + location.host + "/settings/?target="+target;
window.location = url;
alert(e);
}
}
var search = window.location.href.split('?')[1];

Просмотреть файл

@ -64,6 +64,12 @@ function (storage, dispatch, rdapi) {
}
},
update: function (account_data) {
// XXX TODO
// get the account and push it into localstore, don't overwrite, we
// get one account at a time here
},
fetch: function (ok, error) {
rdapi('account/get', {
success: function (json) {
@ -172,6 +178,14 @@ function (storage, dispatch, rdapi) {
return impl.accounts(ok, error);
}
/**
* Updates the accounts from a json account object.
* @param {Object} cookie object to update from
*/
accounts.update = function (account_data) {
impl.update(account_data);
};
/**
* Gets the accounts. Forces a call to the server.
* @param {Function} ok function to receive the account info.

Просмотреть файл

@ -26,16 +26,24 @@
/*global define: false, window: false, location: false */
"use strict";
define([], function () {
define([ 'accounts'],
function (accounts) {
var authDone, win, lastTime = 0;
//Handle communication from the auth window, when it completes.
window.addEventListener("message", function (evt) {
//TODO: ideally lock down the domain check on evt.origin.
var status = evt.data;
if (status) {
if (status === 'oauth_success') {
var data, status;
try {
data = JSON.parse(evt.data);
} catch(e) {
return;
}
if (data.target) {
if (data.target === 'oauth_success' && data.account) {
status = true;
accounts.update(data.account);
} else {
status = false;
}