/**
  * Finalize the mapping process, this ties a user on a network to a Principal id for that user.
  * This is typically the endpoint of a given login request on a third party social network.
  * The method bindmap must have been called first since that setups the request to a login request on a
  * foreign social network.
  *
  * @param array $params
  */
 private static function finalizemap(&$params)
 {
     error_log("Finalizing map with parameters: " . var_export($params, true));
     $next = isset($params['next']) ? $params['next'] : null;
     $nid = isset($params['nid']) ? $params['nid'] : null;
     $sid = isset($params['sid']) ? $params['sid'] : null;
     $snid = isset($params['snid']) ? $params['snid'] : null;
     $api_key = isset($params['api_key']) ? $params['api_key'] : null;
     $sig = isset($params['sig']) ? $params['sig'] : null;
     $canvas = isset($params['canvas']) ? true : false;
     $network = isset($params['network']) ? true : false;
     $iframe = isset($params['fb_sig_in_iframe']) ? $params['fb_sig_in_iframe'] == '1' ? true : false : false;
     $auth_token = isset($params['auth_token']) ? $params['auth_token'] : null;
     try {
         // Get some information about the calling application and registered networks.
         error_log("Finalizing map from {$snid} to {$nid}");
         $ringside_rest = RingsideSocialUtils::getAdminClient($snid);
         $deployed_app = $ringside_rest->admin_getAppProperties(array('application_id', 'api_key', 'secret_key', 'canvas_url', 'callback_url'), null, null, $api_key, $snid);
         $trust_info = $ringside_rest->admin_getTrustInfo(array($snid, $nid));
         $network_app_props = $ringside_rest->admin_getAppKeys(null, null, $deployed_app['api_key'], $snid);
         $host_network = $trust_info[0];
         $auth_network = $trust_info[1];
         $network_api_key = $deployed_app['api_key'];
         $network_secret = $deployed_app['secret_key'];
         self::getApiKeyAndSecretForNetwork($auth_network['trust_key'], $network_app_props, $network_api_key, $network_secret);
         // validate against social network this auth token and get registered user
         error_log("For auth network {$auth_network['trust_key']}, API key is {$network_api_key} and secret is {$network_secret}");
         $auth_network_rest = new RingsideApiClientsRest($network_api_key, $network_secret);
         $auth_network_rest->setDefaultServer($auth_network['trust_auth_url'], null);
         $auth_user_info = $auth_network_rest->auth_getSession($auth_token);
         $auth_user = $auth_user_info['uid'];
         // if a profile was not created relative to that network, you need to 'create' profile first.
         $pid = null;
         if ($auth_user != null) {
             // We have successfully authenticated the user against the remote network...
             error_log(var_export($deployed_app, true));
             $ringside_rest->admin_mapUser($auth_user, $nid, $sid, $snid, $deployed_app['application_id']);
         }
         // Map the USER
         // OK we mapped redirect user.
         self::postMapRedirect($next, $canvas, $iframe, $network, $host_network, $deployed_app);
     } catch (Exception $e) {
         error_log("Error: When finalizing the mapping: " . $e->getMessage());
         error_log($e->getTraceAsString());
         echo "Exception when finalizing the UID mapping, " . $e->getMessage();
     }
     return;
 }