Work in progress on fixing auth... looks like the iframe doesn't work though. Sigh.

This commit is contained in:
Brion Vibber 2010-09-27 12:24:10 -07:00
parent ebbbaba378
commit eeaab2bc00
5 changed files with 49 additions and 25 deletions

View File

@ -33,6 +33,8 @@ if (!defined('STATUSNET')) {
class YammeradminpanelAction extends AdminPanelAction class YammeradminpanelAction extends AdminPanelAction
{ {
private $runner;
/** /**
* Returns the page title * Returns the page title
* *
@ -59,24 +61,30 @@ class YammeradminpanelAction extends AdminPanelAction
$this->init_auth = $this->trimmed('init_auth'); $this->init_auth = $this->trimmed('init_auth');
$this->verify_token = $this->trimmed('verify_token'); $this->verify_token = $this->trimmed('verify_token');
$this->runner = YammerRunner::init();
return $ok; return $ok;
} }
function handle($args) function handle($args)
{ {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->checkSessionToken();
if ($this->init_auth) { if ($this->init_auth) {
$url = $runner->requestAuth(); $url = $this->runner->requestAuth();
$form = new YammerAuthVerifyForm($this, $url); $form = new YammerAuthVerifyForm($this, $this->runner);
return $this->showAjaxForm($form); return $this->showAjaxForm($form);
} else if ($this->verify_token) { } else if ($this->verify_token) {
$runner->saveAuthToken($this->verify_token); $this->runner->saveAuthToken($this->verify_token);
$form = new YammerAuthProgressForm(); $form = new YammerAuthProgressForm();
return $this->showAjaxForm($form); return $this->showAjaxForm($form);
} else {
throw new ClientException('Invalid POST');
} }
} else {
return parent::handle($args); return parent::handle($args);
} }
}
function showAjaxForm($form) function showAjaxForm($form)
{ {
@ -99,18 +107,16 @@ class YammeradminpanelAction extends AdminPanelAction
{ {
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$runner = YammerRunner::init(); switch($this->runner->state())
switch($runner->state())
{ {
case 'init': case 'init':
$form = new YammerAuthInitForm($this); $form = new YammerAuthInitForm($this, $this->runner);
break; break;
case 'requesting-auth': case 'requesting-auth':
$form = new YammerAuthVerifyForm($this, $runner); $form = new YammerAuthVerifyForm($this, $this->runner);
break; break;
default: default:
$form = new YammerProgressForm($this, $runner); $form = new YammerProgressForm($this, $this->runner);
} }
$form->show(); $form->show();

View File

@ -56,6 +56,7 @@ class YammerAuthInitForm extends Form
function formData() function formData()
{ {
$this->out->hidden('init_auth', '1');
} }
/** /**

View File

@ -2,12 +2,12 @@
class YammerAuthVerifyForm extends Form class YammerAuthVerifyForm extends Form
{ {
private $verify_url; private $runner;
function __construct($out, $auth_url) function __construct($out, YammerRunner $runner)
{ {
parent::__construct($out); parent::__construct($out);
$this->verify_url = $auth_url; $this->runner = $runner;
} }
/** /**
@ -64,6 +64,9 @@ class YammerAuthVerifyForm extends Form
function formData() function formData()
{ {
$this->out->input('verify_token', _m('Verification code:'), '', _m("Click through and paste the code it gives you below..."));
$this->out->element('iframe', array('id' => 'yammer-oauth',
'src' => $this->runner->getAuthUrl()));
} }
/** /**
@ -74,9 +77,6 @@ class YammerAuthVerifyForm extends Form
function formActions() function formActions()
{ {
$this->out->input('verify-code', _m('Verification code:'), '', _m("Click through and paste the code it gives you below..."));
$this->out->submit('submit', _m('Verify code'), 'submit', null, _m('Verification code')); $this->out->submit('submit', _m('Verify code'), 'submit', null, _m('Verification code'));
$this->element('iframe', array('id' => 'yammer-oauth',
'src' => $this->auth_url));
} }
} }

View File

@ -123,7 +123,7 @@ class YammerRunner
public function requestAuth() public function requestAuth()
{ {
if ($this->state->state != 'init') { if ($this->state->state != 'init') {
throw ServerError("Cannot request Yammer auth; already there!"); throw new ServerException("Cannot request Yammer auth; already there!");
} }
$data = $this->client->requestToken(); $data = $this->client->requestToken();
@ -135,7 +135,22 @@ class YammerRunner
$this->state->modified = common_sql_now(); $this->state->modified = common_sql_now();
$this->state->update($old); $this->state->update($old);
return $this->getAuthUrl();
}
/**
* When already in requesting-auth state, grab the URL to send the user to
* to complete OAuth setup.
*
* @return string URL
*/
function getAuthUrl()
{
if ($this->state() == 'requesting-auth') {
return $this->client->authorizeUrl($this->state->oauth_token); return $this->client->authorizeUrl($this->state->oauth_token);
} else {
throw new ServerException('Cannot get Yammer auth URL when not in requesting-auth state!');
}
} }
/** /**
@ -152,7 +167,7 @@ class YammerRunner
public function saveAuthToken($verifier) public function saveAuthToken($verifier)
{ {
if ($this->state->state != 'requesting-auth') { if ($this->state->state != 'requesting-auth') {
throw ServerError("Cannot save auth token in Yammer import state {$this->state->state}"); throw new ServerException("Cannot save auth token in Yammer import state {$this->state->state}");
} }
$data = $this->client->accessToken($verifier); $data = $this->client->accessToken($verifier);

View File

@ -16,6 +16,8 @@ $runner = YammerRunner::init();
if (have_option('reset')) { if (have_option('reset')) {
echo "Resetting Yammer import state...\n"; echo "Resetting Yammer import state...\n";
$runner->reset(); $runner->reset();
echo "done.\n";
exit(0);
} }
switch ($runner->state()) switch ($runner->state())