2019-09-29 02:00:04 +08:00
|
|
|
<?php
|
|
|
|
function oauth2($_action, $_type, $_data = null) {
|
|
|
|
global $pdo;
|
|
|
|
global $redis;
|
|
|
|
global $lang;
|
|
|
|
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'access_denied'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch ($_action) {
|
|
|
|
case 'add':
|
|
|
|
switch ($_type) {
|
|
|
|
case 'client':
|
2019-10-03 01:00:36 +08:00
|
|
|
$client_id = bin2hex(random_bytes(6));
|
|
|
|
$client_secret = bin2hex(random_bytes(12));
|
2019-09-29 02:00:04 +08:00
|
|
|
$redirect_uri = $_data['redirect_uri'];
|
2019-10-03 01:00:36 +08:00
|
|
|
$scope = 'profile';
|
|
|
|
// For future use
|
2019-09-29 02:00:04 +08:00
|
|
|
// $grant_type = isset($_data['grant_type']) ? $_data['grant_type'] : 'authorization_code';
|
|
|
|
// $scope = isset($_data['scope']) ? $_data['scope'] : 'profile';
|
2019-10-03 01:00:36 +08:00
|
|
|
// if ($grant_type != "authorization_code" && $grant_type != "password") {
|
|
|
|
// $_SESSION['return'][] = array(
|
|
|
|
// 'type' => 'danger',
|
|
|
|
// 'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
// 'msg' => 'access_denied'
|
|
|
|
// );
|
|
|
|
// return false;
|
|
|
|
// }
|
2019-09-29 02:00:04 +08:00
|
|
|
if ($scope != "profile") {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'Invalid scope'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT 'client' FROM `oauth_clients`
|
|
|
|
WHERE `client_id` = :client_id");
|
|
|
|
$stmt->execute(array(':client_id' => $client_id));
|
|
|
|
$num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
|
|
|
|
if ($num_results != 0) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
2019-09-29 02:00:04 +08:00
|
|
|
'msg' => 'Client ID exists'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$stmt = $pdo->prepare("INSERT INTO `oauth_clients` (`client_id`, `client_secret`, `redirect_uri`, `scope`)
|
|
|
|
VALUES (:client_id, :client_secret, :redirect_uri, :scope)");
|
2019-09-29 02:00:04 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':client_id' => $client_id,
|
|
|
|
':client_secret' => $client_secret,
|
2019-10-03 01:00:36 +08:00
|
|
|
':redirect_uri' => $redirect_uri,
|
|
|
|
':scope' => $scope
|
2019-09-29 02:00:04 +08:00
|
|
|
));
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'success',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
2019-09-29 02:00:04 +08:00
|
|
|
'msg' => 'Added client access'
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'edit':
|
|
|
|
switch ($_type) {
|
|
|
|
case 'client':
|
|
|
|
$ids = (array)$_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$is_now = oauth2('details', 'client', $id);
|
|
|
|
if (!empty($is_now)) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$redirect_uri = (!empty($_data['redirect_uri'])) ? $_data['redirect_uri'] : $is_now['redirect_uri'];
|
2019-09-29 02:00:04 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'access_denied'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
if (isset($_data['revoke_tokens'])) {
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens`
|
|
|
|
WHERE `client_id` IN (
|
|
|
|
SELECT `client_id` FROM `oauth_clients` WHERE `id` = :id
|
|
|
|
)");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens`
|
|
|
|
WHERE `client_id` IN (
|
|
|
|
SELECT `client_id` FROM `oauth_clients` WHERE `id` = :id
|
|
|
|
)");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => array('object_modified', htmlspecialchars($id))
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
2019-10-03 01:00:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isset($_data['renew_secret'])) {
|
|
|
|
$client_secret = bin2hex(random_bytes(12));
|
|
|
|
$stmt = $pdo->prepare("UPDATE `oauth_clients` SET `client_secret` = :client_secret WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':client_secret' => $client_secret,
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => array('object_modified', htmlspecialchars($id))
|
|
|
|
);
|
|
|
|
continue;
|
2019-09-29 02:00:04 +08:00
|
|
|
}
|
|
|
|
if (empty($redirect_uri)) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
2019-09-29 02:00:04 +08:00
|
|
|
'msg' => 'Redirect/Callback URL cannot be empty'
|
|
|
|
);
|
2019-10-03 01:00:36 +08:00
|
|
|
continue;
|
2019-09-29 02:00:04 +08:00
|
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE `oauth_clients` SET
|
|
|
|
`redirect_uri` = :redirect_uri
|
|
|
|
WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':id' => $id,
|
|
|
|
':redirect_uri' => $redirect_uri
|
|
|
|
));
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => array('object_modified', htmlspecialchars($id))
|
|
|
|
);
|
2019-09-29 02:00:04 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
switch ($_type) {
|
|
|
|
case 'client':
|
|
|
|
(array)$ids = $_data['id'];
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
if (!is_numeric($id)) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'access_denied'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
2019-10-03 01:00:36 +08:00
|
|
|
continue;
|
2019-09-29 02:00:04 +08:00
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$stmt = $pdo->prepare("DELETE FROM `oauth_clients`
|
|
|
|
WHERE `id` = :id");
|
2019-09-29 02:00:04 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'success',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => array('items_deleted', htmlspecialchars($id))
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'access_token':
|
|
|
|
(array)$access_tokens = $_data['access_token'];
|
|
|
|
foreach ($access_tokens as $access_token) {
|
|
|
|
if (!ctype_alnum($access_token)) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'access_denied'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens`
|
|
|
|
WHERE `access_token` = :access_token");
|
2019-09-29 02:00:04 +08:00
|
|
|
$stmt->execute(array(
|
|
|
|
':access_token' => $access_token
|
|
|
|
));
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'success',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
2019-09-29 02:00:04 +08:00
|
|
|
'msg' => sprintf($lang['success']['items_deleted'], implode(', ', $access_tokens))
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'refresh_token':
|
|
|
|
(array)$refresh_tokens = $_data['refresh_token'];
|
|
|
|
foreach ($refresh_tokens as $refresh_token) {
|
|
|
|
if (!ctype_alnum($refresh_token)) {
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'danger',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
|
|
|
'msg' => 'access_denied'
|
2019-09-29 02:00:04 +08:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `refresh_token` = :refresh_token");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':refresh_token' => $refresh_token
|
|
|
|
));
|
|
|
|
}
|
2019-10-03 01:00:36 +08:00
|
|
|
$_SESSION['return'][] = array(
|
2019-09-29 02:00:04 +08:00
|
|
|
'type' => 'success',
|
2019-10-03 01:00:36 +08:00
|
|
|
'log' => array(__FUNCTION__, $_action, $_type, $_data),
|
2019-09-29 02:00:04 +08:00
|
|
|
'msg' => sprintf($lang['success']['items_deleted'], implode(', ', $refresh_tokens))
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'get':
|
|
|
|
switch ($_type) {
|
|
|
|
case 'clients':
|
|
|
|
$stmt = $pdo->query("SELECT `id` FROM `oauth_clients`");
|
|
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
while ($row = array_shift($rows)) {
|
|
|
|
$oauth_clients[] = $row['id'];
|
|
|
|
}
|
|
|
|
return $oauth_clients;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'details':
|
|
|
|
switch ($_type) {
|
|
|
|
case 'client':
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM `oauth_clients`
|
|
|
|
WHERE `id` = :id");
|
|
|
|
$stmt->execute(array(':id' => $_data));
|
|
|
|
$oauth_client_details = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return $oauth_client_details;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|