+ /*
+ * Add a new app user (Oauth_application_user) record
+ *
+ * @param Oauth_token_association $tokenAssoc token-to-app association
+ * @param Oauth_application $app the OAuth client app
+ * @param Token $at the access token
+ *
+ * @access private
+ *
+ * @return void
+ */
+ private function newAppUser($tokenAssoc, $app, $at)
+ {
+ $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.
+ throw new Exception(
+ _('Database error inserting OAuth application user.')
+ );
+ }
+ }
+
+ /*
+ * Update an existing app user (Oauth_application_user) record
+ *
+ * @param Oauth_application_user $appUser existing app user rec
+ * @param Oauth_application $app the OAuth client app
+ * @param Token $at the access token
+ *
+ * @access private
+ *
+ * @return void
+ */
+ private function updateAppUser($appUser, $app, $at)
+ {
+ $original = clone($appUser);
+ $appUser->access_type = $app->access_type;
+ $appUser->token = $at->tok;
+
+ $result = $appUser->update($original);
+
+ if (!$result) {
+ common_log_db_error($appUser, 'UPDATE', __FILE__);
+ // TRANS: Server error displayed when a database error occurs.
+ throw new Exception(
+ _('Database error updating OAuth application user.')
+ );
+ }
+ }
+
+ /**
+ * Revoke specified access token
+ *
+ * Revokes the token specified by $token_key.
+ * Throws exceptions in case of error.
+ *
+ * @param string $token_key the token to be revoked
+ * @param int $type type of token (0 = req, 1 = access)
+ *
+ * @access public
+ *
+ * @return void
+ */
+ public function revoke_token($token_key, $type = 0) {
+ $rt = new Token();
+ $rt->tok = $token_key;
+ $rt->type = $type;
+ $rt->state = 0;
+
+ if (!$rt->find(true)) {
+ // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
+ throw new Exception(_('Tried to revoke unknown token.'));
+ }
+
+ if (!$rt->delete()) {
+ // TRANS: Exception thrown when an attempt is made to remove a revoked token.
+ throw new Exception(_('Failed to delete revoked token.'));
+ }
+ }
+
+ /*
+ * Create a new request token. Overrided to support OAuth 1.0a callback
+ *
+ * @param OAuthConsumer $consumer the OAuth Consumer for this token
+ * @param string $callback the verified OAuth callback URL
+ *
+ * @return OAuthToken $token a new unauthorized OAuth request token
+ */
+ function new_request_token($consumer, $callback)
+ {
+ $t = new Token();
+ $t->consumer_key = $consumer->key;
+ $t->tok = common_good_rand(16);
+ $t->secret = common_good_rand(16);
+ $t->type = 0; // request
+ $t->state = 0; // unauthorized
+ $t->verified_callback = $callback;
+
+ if ($callback === 'oob') {
+ // six digit pin
+ $t->verifier = mt_rand(0, 9999999);
+ } else {
+ $t->verifier = common_good_rand(8);
+ }
+
+ $t->created = DB_DataObject_Cast::dateTime();
+ if (!$t->insert()) {
+ return null;
+ } else {
+ return new OAuthToken($t->tok, $t->secret);
+ }
+ }
+}