+ /*
+ * Generate a new access token and save it to the database
+ *
+ * @param Consumer $consumer the OAuth consumer
+ * @param Token $rt the authorized request token
+ * @param string $verifier the OAuth 1.0a verifier
+ *
+ * @access private
+ *
+ * @return Token $at the new access token
+ */
+ private function generateNewAccessToken($consumer, $rt, $verifier)
+ {
+ $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__);
+ }
+
+ return $at;
+ }
+
+ /*
+ * 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.')
+ );
+ }
+ }
+