-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provider.php
110 lines (94 loc) · 2.74 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace SocialiteProviders\PayPing;
use Illuminate\Http\Request;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'PAYPING';
private $verifier;
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl, $guzzle = [])
{
$this->verifier = $this->generateVerified();
parent::__construct($request, $clientId, $clientSecret, $redirectUrl, $guzzle);
}
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://oauth.payping.ir/connect/authorize', $state
) . '&code_challenge=' . $this->generateCodeChallenge($this->verifier) . '&code_challenge_method=S256';
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://oauth.payping.ir/connect/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://oauth.payping.ir/connect/userinfo', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody()->getContents(), true)['account'];
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['uuid'],
'nickname' => $user['username'],
'name' => $user['firstname'] . ' ' . $user['lastname'],
'email' => $user['email'],
'avatar' => $user['profilepicture'],
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
'code_verifier'=> $this->verifier,
]);
}
private function generateVerified()
{
$random = bin2hex(openssl_random_pseudo_bytes(32));
$verifier = $this->base64UrlSafeEncode(pack('H*', $random));
return $verifier;
}
/**
* ساخت یک challenge code
* @param $codeVerifier
* @return string
*/
private function generateCodeChallenge($codeVerifier)
{
return $this->base64UrlSafeEncode(pack('H*', hash('sha256', $codeVerifier)));
}
/**
* escape رشته
* @param $string
* @return string
*/
private function base64UrlSafeEncode($string)
{
return rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
}
}