From 78f293e941dfd79c75736de74a37efd9ef33d899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=AD=A3=E6=B5=B7?= Date: Tue, 23 Nov 2021 17:05:02 +0800 Subject: [PATCH] feat: add line provider (#231) * feat: add line provider * feat: add line provider --- src/Providers/Line.php | 77 ++++++++++++++++++++++++++++++++++++++++ src/SocialiteManager.php | 1 + 2 files changed, 78 insertions(+) create mode 100644 src/Providers/Line.php diff --git a/src/Providers/Line.php b/src/Providers/Line.php new file mode 100644 index 0000000..844b1e3 --- /dev/null +++ b/src/Providers/Line.php @@ -0,0 +1,77 @@ +state = $this->state ?: md5(uniqid()); + return $this->buildAuthUrlFromBase('https://access.line.me/oauth2/'.$this->version.'/authorize'); + } + + protected function getTokenUrl(): string + { + return $this->baseUrl.$this->version.'/token'; + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokenFields(string $code): array + { + return [ + 'client_id' => $this->getClientId(), + 'client_secret' => $this->getClientSecret(), + 'code' => $code, + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->redirectUrl, + ]; + } + + /** + * @param string $token + * + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function getUserByToken(string $token): array + { + $response = $this->getHttpClient()->get( + 'https://api.line.me/v2/profile', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ] + ); + + return \json_decode($response->getBody(), true) ?? []; + } + + protected function mapUserToObject(array $user): User + { + return new User( + [ + 'id' => $user['userId'] ?? null, + 'name' => $user['displayName'] ?? null, + 'nickname' => $user['displayName'] ?? null, + 'avatar' => $user['pictureUrl'] ?? null, + 'email' => null, + ] + ); + } +} diff --git a/src/SocialiteManager.php b/src/SocialiteManager.php index ad8512b..2ddfdbc 100644 --- a/src/SocialiteManager.php +++ b/src/SocialiteManager.php @@ -31,6 +31,7 @@ class SocialiteManager implements FactoryInterface Providers\Facebook::NAME => Providers\Facebook::class, Providers\DingTalk::NAME => Providers\DingTalk::class, Providers\OpenWeWork::NAME => Providers\OpenWeWork::class, + Providers\Line::NAME => Providers\Line::class, ]; public function __construct(array $config)