Пример #1
0
 /**
  * Take arguments for running
  *
  * @param array $args $_REQUEST args
  *
  * @return boolean success flag
  */
 function prepare($args)
 {
     if (!parent::prepare($args)) {
         return false;
     }
     if (!common_logged_in()) {
         // TRANS: Client error displayed trying to delete an application while not logged in.
         $this->clientError(_('You must be logged in to delete an application.'));
         return false;
     }
     $id = (int) $this->arg('id');
     $this->app = Oauth_application::staticGet('id', $id);
     if (empty($this->app)) {
         // TRANS: Client error displayed trying to delete an application that does not exist.
         $this->clientError(_('Application not found.'));
         return false;
     }
     $cur = common_current_user();
     if ($cur->id != $this->app->owner) {
         // TRANS: Client error displayed trying to delete an application the current user does not own.
         $this->clientError(_('You are not the owner of this application.'), 401);
         return false;
     }
     return true;
 }
Пример #2
0
 function showContent()
 {
     $offset = ($this->page - 1) * APPS_PER_PAGE;
     $limit = APPS_PER_PAGE + 1;
     $application = new Oauth_application();
     $application->owner = $this->scoped->getID();
     $application->whereAdd("name != 'anonymous'");
     $application->limit($offset, $limit);
     $application->orderBy('created DESC');
     $application->find();
     $cnt = 0;
     if ($application) {
         $al = new ApplicationList($application, $this->scoped, $this);
         $cnt = $al->show();
         if (0 == $cnt) {
             $this->showEmptyListMessage();
         }
     }
     $this->elementStart('p', array('id' => 'application_register'));
     $this->element('a', array('href' => common_local_url('newapplication'), 'class' => 'more'), 'Register a new application');
     $this->elementEnd('p');
     $this->pagination($this->page > 1, $cnt > APPS_PER_PAGE, $this->page, 'oauthappssettings');
 }
Пример #3
0
 function showConnection()
 {
     $app = Oauth_application::getKV('id', $this->connection->application_id);
     $this->out->elementStart('li', array('class' => 'application h-entry', 'id' => 'oauthclient-' . $app->id));
     $this->out->elementStart('a', array('href' => $app->source_url, 'class' => 'h-card p-name'));
     if (!empty($app->icon)) {
         $this->out->element('img', array('src' => $app->icon, 'class' => 'avatar u-photo'));
     }
     if ($app->name != 'anonymous') {
         $this->out->text($app->name);
     } else {
         // TRANS: Name for an anonymous application in application list.
         $this->out->element('span', 'p-name', _('Unknown application'));
     }
     $this->out->elementEnd('a');
     if ($app->name != 'anonymous') {
         // @todo FIXME: i18n trouble.
         // TRANS: Message has a leading space and a trailing space. Used in application list.
         // TRANS: Before this message the application name is put, behind it the organisation that manages it.
         $this->out->raw(_(' by '));
         $this->out->element('a', array('href' => $app->homepage, 'class' => 'h-card'), $app->organization);
     }
     // TRANS: Application access type
     $readWriteText = _('read-write');
     // TRANS: Application access type
     $readOnlyText = _('read-only');
     $access = $this->connection->access_type & Oauth_application::$writeAccess ? $readWriteText : $readOnlyText;
     $modifiedDate = common_date_string($this->connection->modified);
     // TRANS: Used in application list. %1$s is a modified date, %2$s is access type ("read-write" or "read-only")
     $txt = sprintf(_('Approved %1$s - "%2$s" access.'), $modifiedDate, $access);
     // @todo FIXME: i18n trouble.
     $this->out->raw(" - {$txt}");
     if (!empty($app->description)) {
         $this->out->element('p', array('class' => 'application_description'), $app->description);
     }
     $this->out->element('p', array('class' => 'access_token'), sprintf(_('Access token starting with: %s'), substr($this->connection->token, 0, 7)));
     $this->out->elementStart('form', array('id' => 'form_revoke_app', 'class' => 'form_revoke_app', 'method' => 'POST', 'action' => common_local_url('oauthconnectionssettings')));
     $this->out->elementStart('fieldset');
     $this->out->hidden('oauth_token', $this->connection->token);
     $this->out->hidden('token', common_session_token());
     // TRANS: Button label in application list to revoke access to user data.
     $this->out->submit('revoke', _m('BUTTON', 'Revoke'));
     $this->out->elementEnd('fieldset');
     $this->out->elementEnd('form');
     $this->out->elementEnd('li');
 }
Пример #4
0
 /**
  * Load attributes based on database arguments
  *
  * Loads all the DB stuff
  *
  * @param array $args $_REQUEST array
  *
  * @return success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     $id = (int) $this->arg('id');
     $this->application = Oauth_application::staticGet($id);
     $this->owner = User::staticGet($this->application->owner);
     if (!common_logged_in()) {
         $this->clientError(_('You must be logged in to view an application.'));
         return false;
     }
     if (empty($this->application)) {
         $this->clientError(_('No such application.'), 404);
         return false;
     }
     $cur = common_current_user();
     if ($cur->id != $this->owner->id) {
         $this->clientError(_('You are not the owner of this application.'), 401);
         return false;
     }
     return true;
 }
Пример #5
0
 /**
  * Load attributes based on database arguments
  *
  * Loads all the DB stuff
  *
  * @param array $args $_REQUEST array
  *
  * @return success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     $id = (int) $this->arg('id');
     $this->application = Oauth_application::getKV($id);
     $this->owner = User::getKV($this->application->owner);
     if (!common_logged_in()) {
         // TRANS: Client error displayed trying to display an OAuth application while not logged in.
         $this->clientError(_('You must be logged in to view an application.'));
     }
     if (empty($this->application)) {
         // TRANS: Client error displayed trying to display a non-existing OAuth application.
         $this->clientError(_('No such application.'), 404);
     }
     $cur = common_current_user();
     if ($cur->id != $this->owner->id) {
         // TRANS: Client error displayed trying to display an OAuth application for which the logged in user is not the owner.
         $this->clientError(_('You are not the owner of this application.'), 401);
     }
     return true;
 }
Пример #6
0
 /**
  * Take arguments for running
  *
  * @param array $args $_REQUEST args
  *
  * @return boolean success flag
  */
 function prepare($args)
 {
     if (!parent::prepare($args)) {
         return false;
     }
     if (!common_logged_in()) {
         $this->clientError(_('You must be logged in to delete an application.'));
         return false;
     }
     $id = (int) $this->arg('id');
     $this->app = Oauth_application::staticGet('id', $id);
     if (empty($this->app)) {
         $this->clientError(_('Application not found.'));
         return false;
     }
     $cur = common_current_user();
     if ($cur->id != $this->app->owner) {
         $this->clientError(_('You are not the owner of this application.'), 401);
         return false;
     }
     return true;
 }
 static function getByConsumerKey($key)
 {
     if (empty($key)) {
         return null;
     }
     $app = new Oauth_application();
     $app->consumer_key = $key;
     $app->limit(1);
     $result = $app->find(true);
     return empty($result) ? null : $app;
 }
 /**
  * Revoke access to an authorized OAuth application
  *
  * @param int $appId the ID of the application
  *
  */
 function revokeAccess($appId)
 {
     $cur = common_current_user();
     $app = Oauth_application::staticGet('id', $appId);
     if (empty($app)) {
         $this->clientError(_('No such application.'), 404);
         return false;
     }
     // XXX: Transaction here?
     $appUser = Oauth_application_user::getByKeys($cur, $app);
     if (empty($appUser)) {
         $this->clientError(_('You are not a user of that application.'), 401);
         return false;
     }
     $datastore = new ApiStatusNetOAuthDataStore();
     $datastore->revoke_token($appUser->token, 1);
     $result = $appUser->delete();
     if (!$result) {
         common_log_db_error($orig, 'DELETE', __FILE__);
         $this->clientError(sprintf(_('Unable to revoke access for app: %s.'), $app->id));
         return false;
     }
     $msg = 'User %s (id: %d) revoked access to app %s (id: %d)';
     common_log(LOG_INFO, sprintf($msg, $cur->nickname, $cur->id, $app->name, $app->id));
 }
Пример #9
0
 /**
  * Verifies the OAuth request signature, sets the auth user
  * and access type (read-only or read-write)
  *
  * @param OAuthRequest $request the OAuth Request
  *
  * @return nothing
  */
 function checkOAuthRequest($request)
 {
     $datastore = new ApiGNUsocialOAuthDataStore();
     $server = new OAuthServer($datastore);
     $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
     $server->add_signature_method($hmac_method);
     try {
         $server->verify_request($request);
         $consumer = $request->get_parameter('oauth_consumer_key');
         $access_token = $request->get_parameter('oauth_token');
         $app = Oauth_application::getByConsumerKey($consumer);
         if (empty($app)) {
             common_log(LOG_WARNING, 'API OAuth - Couldn\'t find the OAuth app for consumer key: ' . $consumer);
             // TRANS: OAuth exception thrown when no application is found for a given consumer key.
             throw new OAuthException(_('No application for that consumer key.'));
         }
         // set the source attr
         if ($app->name != 'anonymous') {
             $this->source = $app->name;
         }
         $appUser = Oauth_application_user::getKV('token', $access_token);
         if (!empty($appUser)) {
             // If access_type == 0 we have either a request token
             // or a bad / revoked access token
             if ($appUser->access_type != 0) {
                 // Set the access level for the api call
                 $this->access = $appUser->access_type & Oauth_application::$writeAccess ? self::READ_WRITE : self::READ_ONLY;
                 // Set the auth user
                 if (Event::handle('StartSetApiUser', array(&$user))) {
                     $user = User::getKV('id', $appUser->profile_id);
                     if (!empty($user)) {
                         if (!$user->hasRight(Right::API)) {
                             // TRANS: Authorization exception thrown when a user without API access tries to access the API.
                             throw new AuthorizationException(_('Not allowed to use API.'));
                         }
                     }
                     $this->auth_user = $user;
                     // FIXME: setting the value returned by common_current_user()
                     // There should probably be a better method for this. common_set_user()
                     // does lots of session stuff.
                     global $_cur;
                     $_cur = $this->auth_user;
                     Event::handle('EndSetApiUser', array($user));
                 }
                 $msg = "API OAuth authentication for user '%s' (id: %d) on behalf of " . "application '%s' (id: %d) with %s access.";
                 common_log(LOG_INFO, sprintf($msg, $this->auth_user->nickname, $this->auth_user->id, $app->name, $app->id, ($this->access = self::READ_WRITE) ? 'read-write' : 'read-only'));
             } else {
                 // TRANS: OAuth exception given when an incorrect access token was given for a user.
                 throw new OAuthException(_('Bad access token.'));
             }
         } else {
             // Also should not happen.
             // TRANS: OAuth exception given when no user was found for a given token (no token was found).
             throw new OAuthException(_('No user for that token.'));
         }
     } catch (OAuthException $e) {
         $this->logAuthFailure($e->getMessage());
         common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
         $this->clientError($e->getMessage(), 401);
     }
 }
Пример #10
0
 /**
  * Does the app name already exist?
  *
  * Checks the DB to see someone has already registered an app
  * with the same name.
  *
  * @param string $name app name to check
  *
  * @return boolean true if the name already exists
  */
 function nameExists($name)
 {
     $app = Oauth_application::getKV('name', $name);
     return !empty($app);
 }
Пример #11
0
 function new_access_token($token, $consumer, $verifier)
 {
     common_debug(sprintf("New access token from request token %s, consumer %s and verifier %s ", $token, $consumer, $verifier), __FILE__);
     $rt = new Token();
     $rt->consumer_key = $consumer->key;
     $rt->tok = $token->key;
     $rt->type = 0;
     // request
     $app = Oauth_application::getByConsumerKey($consumer->key);
     assert(!empty($app));
     if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) {
         // authorized
         common_debug('Request token found.', __FILE__);
         // find the app and profile associated with this token
         $tokenAssoc = Oauth_token_association::staticGet('token', $rt->tok);
         if (!$tokenAssoc) {
             throw new Exception(_('Could not find a profile and application associated with the request token.'));
         }
         // check to see if we have previously issued an access token for this application
         // and profile
         $appUser = new Oauth_application_user();
         $appUser->application_id = $app->id;
         $appUser->profile_id = $tokenAssoc->profile_id;
         $result = $appUser->find(true);
         if (!empty($result)) {
             common_log(LOG_INFO, sprintf("Existing access token found for application %s, profile %s.", $app->id, $tokenAssoc->profile_id));
             $at = new Token();
             // fetch the full access token
             $at->consumer_key = $consumer->key;
             $at->tok = $appUser->token;
             $result = $at->find(true);
             if (!$result) {
                 throw new Exception(_('Could not issue access token.'));
             }
             // Yay, we can re-issue the access token
             return new OAuthToken($at->tok, $at->secret);
         } else {
             common_log(LOG_INFO, sprintf("Creating new access token for application %s, profile %s.", $app->id, $tokenAssoc->profile_id));
             // make a brand new access token
             $at = new Token();
             $at->consumer_key = $consumer->key;
             $at->tok = common_good_rand(16);
             $at->secret = common_good_rand(16);
             $at->type = 1;
             // access
             $at->verifier = $verifier;
             $at->verified_callback = $rt->verified_callback;
             // 1.0a
             $at->created = common_sql_now();
             if (!$at->insert()) {
                 $e = $at->_lastError;
                 common_debug('access token "' . $at->tok . '" not inserted: "' . $e->message . '"', __FILE__);
                 return null;
             } else {
                 common_debug('access token "' . $at->tok . '" inserted', __FILE__);
                 // burn the old one
                 $orig_rt = clone $rt;
                 $rt->state = 2;
                 // used
                 if (!$rt->update($orig_rt)) {
                     return null;
                 }
                 common_debug('request token "' . $rt->tok . '" updated', __FILE__);
             }
             // insert a new Oauth_application_user record w/access token
             $appUser = new Oauth_application_user();
             $appUser->profile_id = $tokenAssoc->profile_id;
             $appUser->application_id = $app->id;
             $appUser->access_type = $app->access_type;
             $appUser->token = $at->tok;
             $appUser->created = common_sql_now();
             $result = $appUser->insert();
             if (!$result) {
                 common_log_db_error($appUser, 'INSERT', __FILE__);
                 // TRANS: Server error displayed when a database error occurs.
                 $this->serverError(_('Database error inserting OAuth application user.'));
             }
             // Okay, good
             return new OAuthToken($at->tok, $at->secret);
         }
     } else {
         // the token was not authorized or not verfied
         common_log(LOG_INFO, sprintf("API OAuth - Attempt to exchange unauthorized or unverified request token %s for an access token.", $rt->tok));
         return null;
     }
 }
 function sourceLink($source)
 {
     $source_name = _($source);
     switch ($source) {
         case 'web':
         case 'xmpp':
         case 'mail':
         case 'omb':
         case 'api':
             break;
         default:
             $name = null;
             $url = null;
             $ns = Notice_source::staticGet($source);
             if ($ns) {
                 $name = $ns->name;
                 $url = $ns->url;
             } else {
                 $app = Oauth_application::staticGet('name', $source);
                 if ($app) {
                     $name = $app->name;
                     $url = $app->source_url;
                 }
             }
             if (!empty($name) && !empty($url)) {
                 $source_name = '<a href="' . $url . '">' . $name . '</a>';
             }
             break;
     }
     return $source_name;
 }
Пример #13
0
 /**
  * Show the source of the notice
  *
  * Either the name (and link) of the API client that posted the notice,
  * or one of other other channels.
  *
  * @return void
  */
 function showNoticeSource()
 {
     if ($this->notice->source) {
         $this->out->text(' ');
         $this->out->elementStart('span', 'source');
         $this->out->text(_('from'));
         $source_name = _($this->notice->source);
         $this->out->text(' ');
         switch ($this->notice->source) {
             case 'web':
             case 'xmpp':
             case 'mail':
             case 'omb':
             case 'system':
             case 'api':
                 $this->out->element('span', 'device', $source_name);
                 break;
             default:
                 $name = $source_name;
                 $url = null;
                 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
                     $ns = Notice_source::staticGet($this->notice->source);
                     if ($ns) {
                         $name = $ns->name;
                         $url = $ns->url;
                     } else {
                         $app = Oauth_application::staticGet('name', $this->notice->source);
                         if ($app) {
                             $name = $app->name;
                             $url = $app->source_url;
                         }
                     }
                 }
                 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
                 if (!empty($name) && !empty($url)) {
                     $this->out->elementStart('span', 'device');
                     $this->out->element('a', array('href' => $url, 'rel' => 'external', 'title' => $title), $name);
                     $this->out->elementEnd('span');
                 } else {
                     $this->out->element('span', 'device', $name);
                 }
                 break;
         }
         $this->out->elementEnd('span');
     }
 }
Пример #14
0
 function getApplications($offset = 0, $limit = null)
 {
     $qry = 'SELECT a.* ' . 'FROM oauth_application_user u, oauth_application a ' . 'WHERE u.profile_id = %d ' . 'AND a.id = u.application_id ' . 'AND u.access_type > 0 ' . 'ORDER BY u.created DESC ';
     if ($offset > 0) {
         if (common_config('db', 'type') == 'pgsql') {
             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
         } else {
             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
         }
     }
     $application = new Oauth_application();
     $cnt = $application->query(sprintf($qry, $this->id));
     return $application;
 }
 /**
  * Revoke an access token
  *
  * XXX: Confirm revoke before doing it
  *
  * @param int $appId the ID of the application
  *
  */
 function revokeAccess($token)
 {
     $cur = common_current_user();
     $appUser = Oauth_application_user::getByUserAndToken($cur, $token);
     if (empty($appUser)) {
         // TRANS: Client error when trying to revoke access for an application while not being a user of it.
         $this->clientError(_('You are not a user of that application.'), 401);
         return false;
     }
     $app = Oauth_application::staticGet('id', $appUser->application_id);
     $datastore = new ApiStatusNetOAuthDataStore();
     $datastore->revoke_token($appUser->token, 1);
     $result = $appUser->delete();
     if (!$result) {
         common_log_db_error($orig, 'DELETE', __FILE__);
         // TRANS: Client error when revoking access has failed for some reason.
         // TRANS: %s is the application ID revoking access failed for.
         $this->clientError(sprintf(_('Unable to revoke access for application: %s.'), $app->id));
         return false;
     }
     $msg = 'API OAuth - user %s (id: %d) revoked access token %s for app id %d';
     common_log(LOG_INFO, sprintf($msg, $cur->nickname, $cur->id, $appUser->token, $appUser->application_id));
     $msg = sprintf(_('You have successfully revoked access for %1$s and the access token starting with %2$s.'), $app->name, substr($appUser->token, 0, 7));
     $this->showForm($msg, true);
 }
Пример #16
0
 function new_access_token($token, $consumer)
 {
     common_debug('new_access_token("' . $token->key . '","' . $consumer->key . '")', __FILE__);
     $rt = new Token();
     $rt->consumer_key = $consumer->key;
     $rt->tok = $token->key;
     $rt->type = 0;
     // request
     $app = Oauth_application::getByConsumerKey($consumer->key);
     if (empty($app)) {
         common_debug("empty app!");
     }
     if ($rt->find(true) && $rt->state == 1) {
         // authorized
         common_debug('request token found.', __FILE__);
         // find the associated user of the app
         $appUser = new Oauth_application_user();
         $appUser->application_id = $app->id;
         $appUser->token = $rt->tok;
         $result = $appUser->find(true);
         if (!empty($result)) {
             common_debug("Oath app user found.");
         } else {
             common_debug("Oauth app user not found. app id {$app->id} token {$rt->tok}");
             return null;
         }
         // go ahead and make the access token
         $at = new Token();
         $at->consumer_key = $consumer->key;
         $at->tok = common_good_rand(16);
         $at->secret = common_good_rand(16);
         $at->type = 1;
         // access
         $at->created = DB_DataObject_Cast::dateTime();
         if (!$at->insert()) {
             $e = $at->_lastError;
             common_debug('access token "' . $at->tok . '" not inserted: "' . $e->message . '"', __FILE__);
             return null;
         } else {
             common_debug('access token "' . $at->tok . '" inserted', __FILE__);
             // burn the old one
             $orig_rt = clone $rt;
             $rt->state = 2;
             // used
             if (!$rt->update($orig_rt)) {
                 return null;
             }
             common_debug('request token "' . $rt->tok . '" updated', __FILE__);
             // update the token from req to access for the user
             $orig = clone $appUser;
             $appUser->token = $at->tok;
             // It's at this point that we change the access type
             // to whatever the application's access is.  Request
             // tokens should always have an access type of 0, and
             // therefore be unuseable for making requests for
             // protected resources.
             $appUser->access_type = $app->access_type;
             $result = $appUser->update($orig);
             if (empty($result)) {
                 common_debug('couldn\'t update OAuth app user.');
                 return null;
             }
             // Okay, good
             return new OAuthToken($at->tok, $at->secret);
         }
     } else {
         return null;
     }
 }
Пример #17
0
 /**
  * Verifies the OAuth request signature, sets the auth user
  * and access type (read-only or read-write)
  *
  * @param OAuthRequest $request the OAuth Request
  *
  * @return nothing
  */
 function checkOAuthRequest($request)
 {
     $datastore = new ApiStatusNetOAuthDataStore();
     $server = new OAuthServer($datastore);
     $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
     $server->add_signature_method($hmac_method);
     try {
         $server->verify_request($request);
         $consumer = $request->get_parameter('oauth_consumer_key');
         $access_token = $request->get_parameter('oauth_token');
         $app = Oauth_application::getByConsumerKey($consumer);
         if (empty($app)) {
             common_log(LOG_WARNING, 'Couldn\'t find the OAuth app for consumer key: ' . $consumer);
             throw new OAuthException('No application for that consumer key.');
         }
         // set the source attr
         $this->source = $app->name;
         $appUser = Oauth_application_user::staticGet('token', $access_token);
         if (!empty($appUser)) {
             // If access_type == 0 we have either a request token
             // or a bad / revoked access token
             if ($appUser->access_type != 0) {
                 // Set the access level for the api call
                 $this->access = $appUser->access_type & Oauth_application::$writeAccess ? self::READ_WRITE : self::READ_ONLY;
                 // Set the auth user
                 if (Event::handle('StartSetApiUser', array(&$user))) {
                     $this->auth_user = User::staticGet('id', $appUser->profile_id);
                     Event::handle('EndSetApiUser', array($user));
                 }
                 $msg = "API OAuth authentication for user '%s' (id: %d) on behalf of " . "application '%s' (id: %d) with %s access.";
                 common_log(LOG_INFO, sprintf($msg, $this->auth_user->nickname, $this->auth_user->id, $app->name, $app->id, ($this->access = self::READ_WRITE) ? 'read-write' : 'read-only'));
             } else {
                 throw new OAuthException('Bad access token.');
             }
         } else {
             // Also should not happen
             throw new OAuthException('No user for that token.');
         }
     } catch (OAuthException $e) {
         common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
         $this->clientError($e->getMessage(), 401, $this->format);
         exit;
     }
 }
Пример #18
0
 /**
  * Get the source of the notice
  *
  * @return Notice_source $ns A notice source object. 'code' is the only attribute
  *                           guaranteed to be populated.
  */
 function getSource()
 {
     if (empty($this->source)) {
         return false;
     }
     $ns = new Notice_source();
     switch ($this->source) {
         case 'web':
         case 'xmpp':
         case 'mail':
         case 'omb':
         case 'system':
         case 'api':
             $ns->code = $this->source;
             break;
         default:
             $ns = Notice_source::getKV($this->source);
             if (!$ns) {
                 $ns = new Notice_source();
                 $ns->code = $this->source;
                 $app = Oauth_application::getKV('name', $this->source);
                 if ($app) {
                     $ns->name = $app->name;
                     $ns->url = $app->source_url;
                 }
             }
             break;
     }
     return $ns;
 }
 function new_access_token($token, $consumer, $verifier = null)
 {
     common_debug(sprintf("New access token from request token %s, consumer %s and verifier %s ", $token, $consumer, $verifier), __FILE__);
     $rt = new Token();
     $rt->consumer_key = $consumer->key;
     $rt->tok = $token->key;
     $rt->type = 0;
     // request
     $app = Oauth_application::getByConsumerKey($consumer->key);
     assert(!empty($app));
     if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) {
         // authorized
         common_debug('Request token found.', __FILE__);
         // find the app and profile associated with this token
         $tokenAssoc = Oauth_token_association::getKV('token', $rt->tok);
         if (!$tokenAssoc) {
             throw new Exception(_('Could not find a profile and application associated with the request token.'));
         }
         // Check to see if we have previously issued an access token for
         // this application and profile; if so we can just return the
         // existing access token. That seems to be the best practice. It
         // makes it so users only have to authorize the app once per
         // machine.
         $appUser = new Oauth_application_user();
         $appUser->application_id = $app->id;
         $appUser->profile_id = $tokenAssoc->profile_id;
         $result = $appUser->find(true);
         if (!empty($result)) {
             common_log(LOG_INFO, sprintf("Existing access token found for application %s, profile %s.", $app->id, $tokenAssoc->profile_id));
             $at = null;
             // Special case: we used to store request tokens in the
             // Oauth_application_user record, and the access_type would
             // always be 0 (no access) as a failsafe until an access
             // token was issued and replaced the request token. There could
             // be a few old Oauth_application_user records storing request
             // tokens still around, and we don't want to accidentally
             // return a useless request token instead of a new access
             // token. So if we find one, we generate a new access token
             // and update the existing Oauth_application_user record before
             // returning the new access token. This should be rare.
             if ($appUser->access_type == 0) {
                 $at = $this->generateNewAccessToken($consumer, $rt, $verifier);
                 $this->updateAppUser($appUser, $app, $at);
             } else {
                 $at = new Token();
                 // fetch the full access token
                 $at->consumer_key = $consumer->key;
                 $at->tok = $appUser->token;
                 $result = $at->find(true);
                 if (!$result) {
                     throw new Exception(_('Could not issue access token.'));
                 }
             }
             // Yay, we can re-issue the access token
             return new OAuthToken($at->tok, $at->secret);
         } else {
             common_log(LOG_INFO, sprintf("Creating new access token for application %s, profile %s.", $app->id, $tokenAssoc->profile_id));
             $at = $this->generateNewAccessToken($consumer, $rt, $verifier);
             $this->newAppUser($tokenAssoc, $app, $at);
             // Okay, good
             return new OAuthToken($at->tok, $at->secret);
         }
     } else {
         // the token was not authorized or not verfied
         common_log(LOG_INFO, sprintf("API OAuth - Attempt to exchange unauthorized or unverified request token %s for an access token.", $rt->tok));
         return null;
     }
 }
Пример #20
0
 /**
  * Does the app name already exist?
  *
  * Checks the DB to see someone has already registered an app
  * with the same name.
  *
  * @param string $name app name to check
  *
  * @return boolean true if the name already exists
  */
 function nameExists($name)
 {
     $newapp = Oauth_application::staticGet('name', $name);
     if (empty($newapp)) {
         return false;
     } else {
         return $newapp->id != $this->app->id;
     }
 }
 /**
  * Data elements of the form
  *
  * @return void
  */
 function formData()
 {
     if ($this->application) {
         $id = $this->application->id;
         $icon = $this->application->icon;
         $name = $this->application->name;
         $description = $this->application->description;
         $source_url = $this->application->source_url;
         $organization = $this->application->organization;
         $homepage = $this->application->homepage;
         $callback_url = $this->application->callback_url;
         $this->type = $this->application->type;
         $this->access_type = $this->application->access_type;
     } else {
         $id = '';
         $icon = '';
         $name = '';
         $description = '';
         $source_url = '';
         $organization = '';
         $homepage = '';
         $callback_url = '';
         $this->type = '';
         $this->access_type = '';
     }
     $this->out->elementStart('ul', 'form_data');
     $this->out->elementStart('li', array('id' => 'application_icon'));
     if (!empty($icon)) {
         $this->out->element('img', array('src' => $icon));
     }
     $this->out->element('input', array('name' => 'MAX_FILE_SIZE', 'type' => 'hidden', 'id' => 'MAX_FILE_SIZE', 'value' => ImageFile::maxFileSizeInt()));
     $this->out->element('label', array('for' => 'app_icon'), _('Icon'));
     $this->out->element('input', array('name' => 'app_icon', 'type' => 'file', 'id' => 'app_icon'));
     // TRANS: Form guide.
     $this->out->element('p', 'form_guide', _('Icon for this application'));
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     $this->out->hidden('application_id', $id);
     // TRANS: Form input field label for application name.
     $this->out->input('name', _('Name'), $this->out->arg('name') ? $this->out->arg('name') : $name);
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     $maxDesc = Oauth_application::maxDesc();
     if ($maxDesc > 0) {
         // TRANS: Form input field instructions.
         // TRANS: %d is the number of available characters for the description.
         $descInstr = sprintf(_m('Describe your application in %d character', 'Describe your application in %d characters', $maxDesc), $maxDesc);
     } else {
         // TRANS: Form input field instructions.
         $descInstr = _('Describe your application');
     }
     // TRANS: Form input field label.
     $this->out->textarea('description', _('Description'), $this->out->arg('description') ? $this->out->arg('description') : $description, $descInstr);
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     // TRANS: Form input field instructions.
     $instruction = _('URL of the homepage of this application');
     // TRANS: Form input field label.
     $this->out->input('source_url', _('Source URL'), $this->out->arg('source_url') ? $this->out->arg('source_url') : $source_url, $instruction);
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     // TRANS: Form input field instructions.
     $instruction = _('Organization responsible for this application');
     // TRANS: Form input field label.
     $this->out->input('organization', _('Organization'), $this->out->arg('organization') ? $this->out->arg('organization') : $organization, $instruction);
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     // TRANS: Form input field instructions.
     $instruction = _('URL for the homepage of the organization');
     // TRANS: Form input field label.
     $this->out->input('homepage', _('Homepage'), $this->out->arg('homepage') ? $this->out->arg('homepage') : $homepage, $instruction);
     $this->out->elementEnd('li');
     $this->out->elementStart('li');
     // TRANS: Form input field instructions.
     $instruction = _('URL to redirect to after authentication');
     // TRANS: Form input field label.
     $this->out->input('callback_url', 'Callback URL', $this->out->arg('callback_url') ? $this->out->arg('callback_url') : $callback_url, $instruction);
     $this->out->elementEnd('li');
     $this->out->elementStart('li', array('id' => 'application_types'));
     $attrs = array('name' => 'app_type', 'type' => 'radio', 'id' => 'app_type-browser', 'class' => 'radio', 'value' => Oauth_application::$browser);
     // Default to Browser
     if (empty($this->application) || empty($this->application->type) || $this->application->type == Oauth_application::$browser) {
         $attrs['checked'] = 'checked';
     }
     $this->out->element('input', $attrs);
     $this->out->element('label', array('for' => 'app_type-browser', 'class' => 'radio'), _('Browser'));
     $attrs = array('name' => 'app_type', 'type' => 'radio', 'id' => 'app_type-dekstop', 'class' => 'radio', 'value' => Oauth_application::$desktop);
     if (!empty($this->application) && $this->application->type == Oauth_application::$desktop) {
         $attrs['checked'] = 'checked';
     }
     $this->out->element('input', $attrs);
     $this->out->element('label', array('for' => 'app_type-desktop', 'class' => 'radio'), _('Desktop'));
     // TRANS: Form guide.
     $this->out->element('p', 'form_guide', _('Type of application, browser or desktop'));
     $this->out->elementEnd('li');
     $this->out->elementStart('li', array('id' => 'default_access_types'));
     $attrs = array('name' => 'default_access_type', 'type' => 'radio', 'id' => 'default_access_type-r', 'class' => 'radio', 'value' => 'r');
     // default to read-only access
     if (empty($this->application) || empty($this->application->access_type) || $this->application->access_type & Oauth_application::$readAccess) {
         $attrs['checked'] = 'checked';
     }
     $this->out->element('input', $attrs);
     $this->out->element('label', array('for' => 'default_access_type-ro', 'class' => 'radio'), _('Read-only'));
     $attrs = array('name' => 'default_access_type', 'type' => 'radio', 'id' => 'default_access_type-rw', 'class' => 'radio', 'value' => 'rw');
     if (!empty($this->application) && $this->application->access_type & Oauth_application::$readAccess && $this->application->access_type & Oauth_application::$writeAccess) {
         $attrs['checked'] = 'checked';
     }
     $this->out->element('input', $attrs);
     $this->out->element('label', array('for' => 'default_access_type-rw', 'class' => 'radio'), _('Read-write'));
     // TRANS: Form guide.
     $this->out->element('p', 'form_guide', _('Default access for this application: read-only, or read-write'));
     $this->out->elementEnd('li');
     $this->out->elementEnd('ul');
 }
Пример #22
0
 function showConnection()
 {
     $app = Oauth_application::staticGet('id', $this->connection->application_id);
     $this->out->elementStart('li', array('class' => 'application', 'id' => 'oauthclient-' . $app->id));
     $this->out->elementStart('span', 'vcard author');
     $this->out->elementStart('a', array('href' => $app->source_url, 'class' => 'url'));
     if (!empty($app->icon)) {
         $this->out->element('img', array('src' => $app->icon, 'class' => 'photo avatar'));
     }
     if ($app->name != 'anonymous') {
         $this->out->element('span', 'fn', $app->name);
     }
     $this->out->elementEnd('a');
     if ($app->name == 'anonymous') {
         $this->out->element('span', 'fn', "Unknown application");
     }
     $this->out->elementEnd('span');
     if ($app->name != 'anonymous') {
         // @todo FIXME: i18n trouble.
         $this->out->raw(_(' by '));
         $this->out->element('a', array('href' => $app->homepage, 'class' => 'url'), $app->organization);
     }
     // TRANS: Application access type
     $readWriteText = _('read-write');
     // TRANS: Application access type
     $readOnlyText = _('read-only');
     $access = $this->connection->access_type & Oauth_application::$writeAccess ? $readWriteText : $readOnlyText;
     $modifiedDate = common_date_string($this->connection->modified);
     // TRANS: Used in application list. %1$s is a modified date, %2$s is access type ("read-write" or "read-only")
     $txt = sprintf(_('Approved %1$s - "%2$s" access.'), $modifiedDate, $access);
     $this->out->raw(" - {$txt}");
     if (!empty($app->description)) {
         $this->out->element('p', array('class' => 'application_description'), $app->description);
     }
     $this->out->element('p', array('class' => 'access_token'), sprintf(_('Access token starting with: %s'), substr($this->connection->token, 0, 7)));
     $this->out->elementStart('form', array('id' => 'form_revoke_app', 'class' => 'form_revoke_app', 'method' => 'POST', 'action' => common_local_url('oauthconnectionssettings')));
     $this->out->elementStart('fieldset');
     $this->out->hidden('oauth_token', $this->connection->token);
     $this->out->hidden('token', common_session_token());
     // TRANS: Button label
     $this->out->submit('revoke', _m('BUTTON', 'Revoke'));
     $this->out->elementEnd('fieldset');
     $this->out->elementEnd('form');
     $this->out->elementEnd('li');
 }