-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple-signin.php
223 lines (191 loc) · 6.35 KB
/
apple-signin.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Plugin Name: Apple Signin
* Plugin URI: https://github.com/keygen-sh/example-wordpress-plugin
* Description: Signin with Apple.
* Version: 1.0.0
* Author: Abdul Haq
* Author URI: https://it.haq.life/?ref=plugins
*/
include_once(plugin_dir_path(__FILE__) . 'variables.php');
//signup with apple
add_action('wp_ajax_nopriv_apple_signin', 'apple_signin');
add_action('wp_ajax_apple_signin', 'apple_signin');
function apple_signin()
{
session_start();
$client_id = 'com.lockersuites.app';
$client_secret = generateJWT();
$redirect_uri = get_site_url() . '/wp-admin/admin-ajax.php?action=apple_signin';
$post_code = $_REQUEST['code'];
if (isset($post_code)) {
// if ($_SESSION['state'] != $_POST['state']) {
// die('Authorization server returned an invalid state parameter');
// }
if (isset($_REQUEST['error'])) {
die('Authorization server returned an error: ' . htmlspecialchars($_REQUEST['error']));
}
$body = [
'grant_type' => 'authorization_code',
'code' => $post_code,
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret,
];
$body = wp_json_encode($body);
$options = [
'body' => $body,
];
$ch = curl_init('https://appleid.apple.com/auth/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'authorization_code',
'code' => $post_code,
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//"cache-control: no-cache",
//"content-type: application/json"
));
$response = curl_exec($ch);
//echo $response;
$response = json_decode($response, true);
// echo 'here: ' . $client_secret;
// exit();
if (!isset($response['access_token'])) {
echo '<p>Error getting an access token:</p>';
echo $response['error'];
echo $response['access_token'];
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<p><a href="/">Start Over</a></p>';
die();
}
echo '<h3>Access Token Response</h3>';
echo '<pre>';
print_r($response);
echo '</pre>';
$claims = explode('.', $response['id_token'])[1];
$claims = json_decode(base64_decode($claims));
echo '<pre>';
print_r($claims);
print_r($claims->email);
echo '</pre>';
//login or signup to wordpress
$user = get_user_by('email', $claims->email);
$user_id = $user->ID;
// echo $user_id;
// exit();
if ($user_id) {
//login
$user = get_user_by('id', $user_id);
if ($user) {
wp_set_current_user($user_id, $user->user_login);
wp_set_auth_cookie($user_id);
//do_action('wp_login', $user->user_login);
wp_redirect($post_login_url);
exit();
}
} else {
//signup
$new_user_id = wp_create_user($claims->email, '', $claims->email);
if (is_wp_error($new_user_id)) {
$error = $new_user_id->get_error_message();
//handle error here
}
$new_user = get_user_by('id', $new_user_id);
wp_set_current_user($new_user_id, $new_user->user_login);
wp_set_auth_cookie($new_user_id);
wp_redirect($post_login_url);
exit();
}
die();
}
}
function generateJWT()
{
$header = [
'alg' => 'ES256',
'kid' => $kid
];
$body = [
'iss' => $iss,
'iat' => time(),
'exp' => time() + 3600,
'aud' => 'https://appleid.apple.com',
'sub' => $sub
];
//$privKey = openssl_pkey_get_private(file_get_contents('AuthKey_8UJ8MDL8W9.p8'));
$privKey = openssl_pkey_get_private($private_key);
if (!$privKey) {
echo 'here';
return false;
}
$payload = encode(json_encode($header)) . '.' . encode(json_encode($body));
$signature = '';
$success = openssl_sign($payload, $signature, $privKey, OPENSSL_ALGO_SHA256);
if (!$success) return false;
$raw_signature = fromDER($signature, 64);
return $payload . '.' . encode($raw_signature);
}
function encode($data)
{
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
}
function fromDER(string $der, int $partLength)
{
$hex = unpack('H*', $der)[1];
if ('30' !== mb_substr($hex, 0, 2, '8bit')) { // SEQUENCE
throw new \RuntimeException();
}
if ('81' === mb_substr($hex, 2, 2, '8bit')) { // LENGTH > 128
$hex = mb_substr($hex, 6, null, '8bit');
} else {
$hex = mb_substr($hex, 4, null, '8bit');
}
if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
throw new \RuntimeException();
}
$Rl = hexdec(mb_substr($hex, 2, 2, '8bit'));
$R = retrievePositiveInteger(mb_substr($hex, 4, $Rl * 2, '8bit'));
$R = str_pad($R, $partLength, '0', STR_PAD_LEFT);
$hex = mb_substr($hex, 4 + $Rl * 2, null, '8bit');
if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
throw new \RuntimeException();
}
$Sl = hexdec(mb_substr($hex, 2, 2, '8bit'));
$S = retrievePositiveInteger(mb_substr($hex, 4, $Sl * 2, '8bit'));
$S = str_pad($S, $partLength, '0', STR_PAD_LEFT);
return pack('H*', $R . $S);
}
/**
* @param string $data
*
* @return string
*/
function preparePositiveInteger(string $data)
{
if (mb_substr($data, 0, 2, '8bit') > '7f') {
return '00' . $data;
}
while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') <= '7f') {
$data = mb_substr($data, 2, null, '8bit');
}
return $data;
}
/**
* @param string $data
*
* @return string
*/
function retrievePositiveInteger(string $data)
{
while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') > '7f') {
$data = mb_substr($data, 2, null, '8bit');
}
return $data;
}