Commit 284b6b24 authored by 康帅杰's avatar 康帅杰 :speech_balloon:
Browse files

添加苹果登陆支持

parent ad4d6e5a
Showing with 135 additions and 2 deletions
+135 -2
......@@ -12,7 +12,8 @@
"guzzlehttp/guzzle": "~6.0",
"illuminate/contracts": "~5.4",
"illuminate/http": "~5.4",
"illuminate/support": "~5.4"
"illuminate/support": "~5.4",
"yjtec/applesignin":"^1.0"
},
"require-dev": {
"phpunit/phpunit": "7.5",
......
require "jwt"
key_file = "AuthKey_LAAL6V64JL.p8"
team_id = "PQCCZX8QUD"
client_id = "com.hnyjkj.com"
key_id = "LAAL6V64JL"
validity_period = 180 # In days. Max 180 (6 months) according to Apple docs.
private_key = OpenSSL::PKey::EC.new IO.read key_file
token = JWT.encode(
{
iss: team_id,
iat: Time.now.to_i,
exp: Time.now.to_i + 86400 * validity_period,
aud: "https://appleid.apple.com",
sub: client_id
},
private_key,
"ES256",
header_fields=
{
kid: key_id
}
)
puts token
\ No newline at end of file
......@@ -9,6 +9,7 @@ use Yjtec\Socialite\Two\GithubProvider;
use Yjtec\Socialite\Two\WechatProvider;
use Yjtec\Socialite\Two\WechatAppProvider;
use Yjtec\Socialite\Two\GitlabProvider;
use Yjtec\Socialite\Two\AppleProvider;
class SocialiteManager extends Manager implements Contracts\Factory
{
/**
......@@ -49,6 +50,12 @@ class SocialiteManager extends Manager implements Contracts\Factory
);
}
public function createAppleDriver(){
$config = $this->app['config']['services.apple'];
return $this->buildProvider(
AppleProvider::class,$config
);
}
/**
* Build an OAuth 2 provider instance.
......
......@@ -257,11 +257,12 @@ abstract class AbstractProvider implements ProviderContract
public function getAccessTokenResponse($code)
{
$postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
// dd($this->getTokenFields($code));
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
$postKey => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true);
}
......
<?php
namespace Yjtec\Socialite\Two;
use Yjtec\AppleSignin\Facades\AppleSignin;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class AppleProvider extends AbstractProvider implements ProviderInterface
{
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl, $guzzle = [])
{
$secret = AppleSignin::secret();
parent::__construct($request,$clientId,$secret,$redirectUrl,$guzzle);
}
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://appleid.apple.com/auth/authorize', $state);
}
protected function getTokenUrl()
{
return 'https://appleid.apple.com/auth/token';
}
public function user()
{
$response = $this->getAccessTokenResponse($this->getCode());
$jwt = Arr::get($response,'id_token');
$user = $this->mapUserToObject(
(array) AppleSignin::decode($jwt)
);
return $user->setToken(Arr::get($response, 'access_token'))
->setRefreshToken(Arr::get($response, 'refresh_token'))
->setExpiresIn(Arr::get($response, 'expires_in'));
}
protected function getUserByToken($token)
{
}
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['sub']
]);
}
protected function getCodeFields($state = null)
{
$fields = [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'response_type' => 'code',
];
if ($this->usesState()) {
$fields['state'] = $state;
}
return array_merge($fields, $this->parameters);
}
protected function getTokenFields($code)
{
return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
}
}
<?php
namespace Yjtec\Socialite\Tests\Two;
use PHPUnit\Framework\TestCase;
use Yjtec\Socialite\Two\AppleProvider;
use Illuminate\Support\Arr;
class GithubProviderTest extends TestCase
{
public function testInstance()
{
$provider = new AppleProvider;
$this->assertTrue($provider instanceof AppleProvider);
}
public function testDecodeToken()
{
$response = [
"access_token" => "a4522c53f49ea4f5d9ebb749a49c02339.0.nrvyu.aHFUMahu_POzf4Yy2jQUXA",
"token_type" => "Bearer",
"expires_in" => 3600,
"refresh_token" => "rf7f6cad1a0a24fa79625e91a8529972a.0.nrvyu.NbM6f-JlUFGq6pAu_5rhFQ",
"id_token" => "eyJraWQiOiJlWGF1bm1MIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLmhueWprai5jb20iLCJleHAiOjE1ODM5MDU3NTksImlhdCI6MTU4MzkwNTE1OSwic3ViIjoiMDAxNTg0LjA2OTVmOWFhMDRjNDQ4N2ZhOTgwMmZmOTQ0NWMzMWY0LjEwMDIiLCJhdF9oYXNoIjoiQzNHRjd5anNrVmxXOVY3Y3Q2VXpKQSIsImF1dGhfdGltZSI6MTU4MzkwNTE1Nywibm9uY2Vfc3VwcG9ydGVkIjp0cnVlfQ.2VnedCmSF0wohZAOVOO-lk4l-0ZYMXSjY3EHNqLJNyUm95KKY1fy7a6eRer8Yyw_kPbh-mXFhm0qVfgEJOneIkNPZZ87bi-HBIqRfbBR7CzuIpYS0EGWm2hXxjDbd9Drqeamc1PREq_q3V8FGk8L-IhOMksxUYuoSyazV6LrAdIu4SiWAErnbcCVOnaB9T08ftkfQHqEShKifLnGUoHgH_ywx61L-B-m9bOqQH-hbneN958A2oI5Jq75IcPX6hrkbqQUCfBzHEiUDvOtaGBoqPVRmPW5KAZvZpH8V4DyEoYOlMuerewt1YdA1ke-2O1ozmoKXL__naktg7a3k-_tQA"
];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment