-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.php
89 lines (69 loc) · 2.4 KB
/
start.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
<?php
declare(strict_types = 1);
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
if(! (isset($_POST["email"]) && isset($_POST["token"])))
die("params invalid");
$email = $_POST["email"];
$gRecaptchaResponse = htmlspecialchars($_POST["token"]);
$source_ip = $_SERVER['REMOTE_ADDR'];
$recaptcha = new \ReCaptcha\ReCaptcha(getenv('RECAPTCHA_SECRET'));
$resp = $recaptcha->setExpectedHostname(getenv('RECAPTCHA_SITE_NAME'))
->verify($gRecaptchaResponse, $source_ip);
if (! $resp->isSuccess()) {
$errors = $resp->getErrorCodes();
header('Location: error.html?msg=captch-failed');
exit;
}
use \Auth0\SDK\API\Authentication;
use Auth0\SDK\Store\SessionStore;
use Auth0\SDK\Store\CookieStore;
use Auth0\SDK\Helpers\TransientStoreHandler;
use Auth0\SDK\Auth0;
$auth0_api = new Authentication(
getenv('AUTH0_DOMAIN'),
getenv('AUTH0_CLIENT_ID'),
getenv('AUTH0_CLIENT_SECRET'), null, null,
//$guzzleOptions = ['proxy' => 'http:https://localhost:8888', 'verify' => false]
);
$config = [
'audience' => getenv('AUTH0_MANAGEMENT_AUDIENCE'),
];
try {
$result = $auth0_api->client_credentials($config);
} catch (Exception $e) {
die( $e->getMessage() );
}
$access_token = $result["access_token"];
use Auth0\SDK\API\Management;
$mgmt_api = new Management( $access_token, getenv('AUTH0_DOMAIN'));
$connection_name = getenv('DB_CONNECTION_NAME');
$search_params = array(
'q' => urlencode("identities.connection:\"$connection_name\" AND email:\"$email\""),
'search_engine' => 'v3',
'fields' => ['user_id', 'email']
);
// todo: switch to /api/v2/user-by-email
try {
$search_result = $mgmt_api->users()->getAll($search_params);
} catch (Exception $e) {
die( $e->getMessage() );
}
if(!empty($search_result)) {
header('Location: error.html?msg=email-taken');
exit;
}
// Generate and store a state value.
$transient_store = new CookieStore();
$state_handler = new TransientStoreHandler($transient_store);
$state_value = $state_handler->issue(Auth0::TRANSIENT_STATE_KEY);
$authParams = array(
'redirect_uri' => getenv('AUTH0_CALLBACK_URL'),
'response_type' => 'code',
'response_mode' => 'form_post',
'scope' => 'openid email',
'state' => $state_value
);
$auth0_api->email_passwordless_start($email, 'link', $authParams, $source_ip);
echo "Thank you. Please check your mailbox.<br/>";
echo '<a href="index.html">Try again</a>';