Completed
Push — master ( 3888fe...7b95f2 )
by Carlos
02:54
created

src/Providers/FeiShu.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Overtrue\Socialite\Providers;
4
5
use Overtrue\Socialite\Exceptions\AuthorizeFailedException;
6
use Overtrue\Socialite\User;
7
8
/**
9
 * @see https://open.feishu.cn/document/uQjL04CN/ucDOz4yN4MjL3gzM
10
 */
11
class FeiShu extends Base
12
{
13
    public const NAME = 'feishu';
14
    protected string $baseUrl = 'https://open.feishu.cn/open-apis/';
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
15
    protected string $expiresInKey = 'refresh_expires_in';
16
17
    protected function getAuthUrl(): string
18
    {
19
        return $this->buildAuthUrlFromBase($this->baseUrl . 'authen/v1/index');
20
    }
21
22
    protected function getCodeFields(): array
23
    {
24
        return [
25
            'redirect_uri' => $this->redirectUrl,
26
            'app_id' => $this->getClientId(),
27
        ];
28
    }
29
30
    protected function getTokenUrl(): string
31
    {
32
        return $this->baseUrl . 'authen/v1/access_token';
33
    }
34
35
    /**
36
     * @param string $code
37
     *
38
     * @return array
39
     * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
40
     *
41
     */
42
    public function tokenFromCode($code): array
43
    {
44
        return $this->normalizeAccessTokenResponse($this->getTokenFromCode($code));
45
    }
46
47
    /**
48
     * @param string $code
49
     *
50
     * @return array
51
     * @throws AuthorizeFailedException
52
     *
53
     * @throws AuthorizeFailedException
54
     */
55
    protected function getTokenFromCode(string $code): array
56
    {
57
        $response = $this->getHttpClient()->post(
58
            $this->getTokenUrl(),
59
            [
60
                'json' => [
61
                    'app_access_token' => $this->config->get('app_access_token'),
62
                    'code' => $code,
63
                    'grant_type' => 'authorization_code',
64
                ],
65
            ]
66
        );
67
        $response = \json_decode($response->getBody(), true) ?? [];
68
69
        if (empty($response['data'])) {
70
            throw new AuthorizeFailedException('Invalid token response', $response);
71
        }
72
73
        return $this->normalizeAccessTokenResponse($response['data']);
74
    }
75
76
    /**
77
     * @param string $token
78
     *
79
     * @return array
80
     */
81
    protected function getUserByToken(string $token): array
82
    {
83
        $response = $this->getHttpClient()->get(
84
            $this->baseUrl . '/authen/v1/user_info',
85
            [
86
                'headers' => ['Content-Type' => 'application/json', 'AuthoriBearer ' . $token],
87
                'query' => array_filter(
88
                    [
89
                        'user_access_token' => $token,
90
                    ]
91
                ),
92
            ]
93
        );
94
95
        $response = \json_decode($response->getBody(), true) ?? [];
96
97
        if (empty($response['data'])) {
98
            throw new \InvalidArgumentException('You have error! ' . json_encode($response, JSON_UNESCAPED_UNICODE));
99
        }
100
101
        return $response['data'];
102
    }
103
104
    /**
105
     * @param array $user
106
     *
107
     * @return User
108
     */
109
    protected function mapUserToObject(array $user): User
110
    {
111
        return new User(
112
            [
113
                'id' => $user['user_id'] ?? null,
114
                'name' => $user['name'] ?? null,
115
                'nickname' => $user['name'] ?? null,
116
                'avatar' => $user['avatar_url'] ?? null,
117
                'email' => $user['email'] ?? null,
118
            ]
119
        );
120
    }
121
}
122