<commit_before><?php

namespace SocialiteProviders\WeChatServiceAccount;

use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
    /**
     * Unique Provider Identifier.
     */
    const IDENTIFIER = 'WECHAT_SERVICE_ACCOUNT';

    /**
     * {@inheritdoc}
     */
    protected $scopes = ['snsapi_userinfo'];

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state);
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return 'https://api.weixin.qq.com/sns/oauth2/access_token';
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        // HACK: Tencent return id when grant token, and can not get user by this token
        if (in_array('snsapi_base', $this->scopes)) {
            return ['openid' => $this->credentialsResponseBody['openid']];
        }
        $response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
            'query' => [
                'access_token' => $token, // HACK: Tencent use token in Query String, not in Header Authorization
                'openid'       => $this->credentialsResponseBody['openid'],
                'lang'         => 'zh_CN',
            ],
        ]);

        return json_decode($response->getBody(), true);
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user)->map([
            'id'       => $user['openid'],
            'nickname' => $user['nickname'],
            'name'     => null,
            'email'    => null,
            'avatar'   => $user['headimgurl'],
        ]);
    }

    /**<commit_msg>HACK: Tencent scope snsapi_base only return openid<commit_after><?php

namespace SocialiteProviders\WeChatServiceAccount;

use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
    /**
     * Unique Provider Identifier.
     */
    const IDENTIFIER = 'WECHAT_SERVICE_ACCOUNT';

    /**
     * {@inheritdoc}
     */
    protected $scopes = ['snsapi_userinfo'];

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state);
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return 'https://api.weixin.qq.com/sns/oauth2/access_token';
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        // HACK: Tencent return id when grant token, and can not get user by this token
        if (in_array('snsapi_base', $this->scopes)) {
            return ['openid' => $this->credentialsResponseBody['openid']];
        }
        $response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
            'query' => [
                'access_token' => $token, // HACK: Tencent use token in Query String, not in Header Authorization
                'openid'       => $this->credentialsResponseBody['openid'],
                'lang'         => 'zh_CN',
            ],
        ]);

        return json_decode($response->getBody(), true);
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user)->map([
            'id'       => $user['openid'],
            'nickname' => isset($user['nickname']) ? $user['nickname'] : null, // HACK: Tencent scope snsapi_base only return openid
            'name'     => null,
            'email'    => null,
            'avatar'   => isset($user['headimgurl']) ? $user['headimgurl'] : null,
        ]);
    }

    /**
{'fraction_non_alphanumeric': 0.15143074195998987, 'fraction_numerical': 0.002025829323879463, 'mean_word_length': 2.1574740207833734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '394861', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1197, 'n_words': 217}