public function updateAnonymousToken()

in modules/quanthub_core/src/UserInfo.php [155:213]


  public function updateAnonymousToken() {
    // Oidc plugin id is dynamic hash, we firstly get id from oidc settings.
    $generic_realms = $this->configFactory->get('oidc.settings')->get('generic_realms');
    if (count($generic_realms) == 0) {
      return;
    }

    $oidc_plugin_id = array_shift($generic_realms);
    $oidc_plugin = $this->configFactory->get('oidc.realm.quanthub_b2c_realm.' . $oidc_plugin_id);
    if (!isset($oidc_plugin)) {
      return;
    }
    $anonymous_endpoint = $oidc_plugin->get(self::ANONYMOUS_TOKEN_ENDPOINT);

    if ($anonymous_endpoint) {
      try {
        $response = $this->httpClient->get($anonymous_endpoint, [
          'headers' => [
            'Content-Type' => 'application/json',
          ],
        ]);

        $user_info_data = json_decode($response->getBody(), TRUE);
        $this->cache->set(self::ANONYMOUS_TOKEN_CID, $user_info_data['token'], strtotime($user_info_data['expiresOn']));
      }
      catch (RequestException $e) {
        $this->logger->error('Failed to retrieve tokens for anonymous user: @error.', [
          '@error' => $e->getMessage(),
        ]);

        throw new \RuntimeException('Failed to retrieve the user info anonymous token', 0, $e);
      }
    }
    else {
      $this->logger->error('Failed to retrieve tokens for anonymous user: Anonymous token is not set');
    }

    $user_attributes_endpoint = $oidc_plugin->get(self::USER_ATTRIBUTES_ENDPOINT);
    if (!$this->cache->get(self::ANONYMOUS_QUANTHUB_USER_ID) && !empty($user_info_data['token']) && !empty($user_info_data['expiresOn'])) {
      try {
        $response = $this->httpClient->get($user_attributes_endpoint, [
          'headers' => [
            'Authorization' => 'Bearer ' . $user_info_data['token'],
            'Content-Type' => 'application/json',
          ],
        ]);

        $user_attributes_data = json_decode($response->getBody(), TRUE);
        $this->cache->set(self::ANONYMOUS_QUANTHUB_USER_ID, $user_attributes_data['userAttributes']['USER_ID'], strtotime($user_info_data['expiresOn']));
      }
      catch (RequestException $e) {
        $this->logger->error('Failed to retrieve quanthub user id for anonymous user: @error.', [
          '@error' => $e->getMessage(),
        ]);

        throw new \RuntimeException('Failed to retrieve quanthub user id for anonymous token', 0, $e);
      }
    }
  }