Skip to content

Commit

Permalink
feat: add line provider (overtrue#231)
Browse files Browse the repository at this point in the history
* feat: add line provider

* feat: add line provider
  • Loading branch information
forecho authored Nov 23, 2021
1 parent 6b9b073 commit 78f293e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Providers/Line.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Overtrue\Socialite\Providers;

use Overtrue\Socialite\User;

/**
* @see https://developers.line.biz/en/docs/line-login/integrate-line-login/ [Integrating LINE Login with your web app]
*/
class Line extends Base
{
public const NAME = 'line';
protected string $baseUrl = 'https://api.line.me/oauth2/';
protected string $version = 'v2.1';
protected array $scopes = ['profile'];

protected function getAuthUrl(): string
{
$this->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,
]
);
}
}
1 change: 1 addition & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 78f293e

Please sign in to comment.