-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-user.php
113 lines (89 loc) · 3.21 KB
/
create-user.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
<?php
$data = json_decode(file_get_contents('php:https://input'), true);
$given_name = htmlspecialchars($data["given_name"]);
$family_name = htmlspecialchars($data["family_name"]);
$password = htmlspecialchars($data["password"]);
if(! (isset($given_name) && isset($family_name) && isset($password))) {
die('missing input');
}
function getAuthorizationHeader(){
$headers = null;
if (isset($_SERVER['Authorization'])) {
$headers = trim($_SERVER["Authorization"]);
}
else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
} elseif (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
// Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
//print_r($requestHeaders);
if (isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
}
return $headers;
}
function getBearerToken() {
$headers = getAuthorizationHeader();
// HEADER: Get the access token from the header
if (!empty($headers)) {
if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
return $matches[1];
}
}
return null;
}
$access_token = htmlspecialchars($_COOKIE["otp_access_token"]);
if(! isset($access_token) )
die('missing input');
header('Content-Type: application/json');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
use \Auth0\SDK\API\Authentication;
$auth0_api = new Authentication(
getenv('AUTH0_DOMAIN'),
getenv('AUTH0_CLIENT_ID'),
getenv('AUTH0_CLIENT_SECRET')
);
$pwl_user_info = $auth0_api->userinfo($access_token);
$email = $pwl_user_info['email'];
if(! isset($email)) {
die('email missing');
}
$config = [
'client_secret' => getenv('AUTH0_CLIENT_SECRET'),
'client_id' => getenv('AUTH0_CLIENT_ID'),
'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') );
$username = rand(); // get username from backend database
$DB_CONNECTION_NAME = getenv('DB_CONNECTION_NAME');
$user_data = array(
'connection' => $DB_CONNECTION_NAME,
'given_name' => $given_name,
'family_name' => $family_name,
'email' => $email,
'username' => "$username",
'password' => $password,
'email_verified'=> true,
'verify_email' => false);
try {
$create_result = $mgmt_api->users()->create($user_data);
} catch (Exception $e) {
die( $e->getMessage() );
}
try {
$delete_result = $mgmt_api->users()->delete($pwl_user_info['sub']);
} catch (Exception $e) {
die( $e->getMessage() );
}
$result = array('success' => true, 'user_id' => $create_result['user_id'], 'email' => $email, 'connection' => $DB_CONNECTION_NAME );
echo json_encode($result, JSON_PRETTY_PRINT);