-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrcauth.php
106 lines (95 loc) · 3.43 KB
/
rcauth.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
<?php
/**
* Fax SMS Module Member
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2018-2019 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
$ignoreAuth = 1;
require_once(__DIR__ . "/../../../globals.php");
if (empty($_SESSION['url'])) {
http_response_code(401);
exit();
}
use OpenEMR\Common\Crypto\CryptoGen;
use RingCentral\SDK\SDK;
$url = $_SESSION['url'];
$callbackUrl = $_SESSION['redirect_uri'];
function processCode()
{
$vendor = '_ringcentral';
$authUser = 0;
$credentials = sqlQuery("SELECT * FROM `module_faxsms_credentials` WHERE `auth_user` = ? AND `vendor` = ?", array($authUser, $vendor))['credentials'];
if(empty($credentials)) {
// for legacy
$cacheDir = $GLOBALS['OE_SITE_DIR'] . '/documents/logs_and_misc/_cache';
$credentials = file_get_contents($cacheDir . '/_credentials.php');
if(empty($credentials)) {
die('Credential Error');
}
}
$cryptoGen = new CryptoGen();
$credentials = json_decode($cryptoGen->decryptStandard($credentials), true);
$serverUrl = !$credentials['production'] ? "https://platform.devtest.ringcentral.com" : "https://platform.ringcentral.com";
$callbackUrl = $credentials['redirect_url'];
$rcsdk = new SDK($credentials['appKey'], $credentials['appSecret'], $serverUrl, 'OpenEMR', '1.0.0');
$platform = $rcsdk->platform();
$qs = $platform->parseAuthRedirectUrl($_SERVER['QUERY_STRING']);
$qs["redirectUri"] = $callbackUrl;
// log in
$apiResponse = $platform->login($qs);
$_SESSION['sessionAccessToken'] = $apiResponse->text();
// archive authentication data for future reauths.
$file = $cacheDir . DIRECTORY_SEPARATOR . 'platform.json';
$content = json_encode($platform->auth()->data(), JSON_PRETTY_PRINT);
$cryptoGen = new CryptoGen();
$content = $cryptoGen->encryptStandard($content);
file_put_contents($file, $content);
}
if (isset($_GET['code'])) {
processCode();
exit();
}
?>
<script>
var tokenUrl = '<?php echo $url; ?>';
var redirectUrl = '<?php echo $callbackUrl; ?>';
var config = {
authUri: tokenUrl,
redirectUri: redirectUrl,
};
var OAuthCode = function (config) {
this.config = config;
this.loginPopup = function () {
console.log("URL: " + tokenUrl);
this.loginPopupUri(this.config['authUri'], this.config['redirectUri']);
};
this.loginPopupUri = function (authUri, redirectUrl) {
win = window.open(authUri, 'auth2F', 'width=800, height=600');
var pollOAuth = window.setInterval(function () {
if (win.closed) {
window.clearInterval(pollOAuth);
}
try {
console.log(win.document.URL);
if (win.document.URL.indexOf(redirectUrl) !== -1) {
window.clearInterval(pollOAuth);
win.close();
location.reload();
}
} catch (e) {
//console.log(e);
}
}, 300);
}
};
var oauth = new OAuthCode(config);
window.addEventListener("load", function () {
if (tokenUrl) {
oauth.loginPopup();
}
});
</script>