-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive_orcid.php
112 lines (100 loc) · 3.53 KB
/
receive_orcid.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
<?php
\OCP\Util::writeLog('user_orcid','Received code: '.serialize($_GET), \OC_Log::WARN);
require_once('user_orcid/lib/lib_orcid.php');
$code = $_GET['code'];
$user = \OCP\User::getUser();
$clientAppID = OC_Appconfig::getValue('user_orcid', 'clientAppID');
$clientSecret = OC_Appconfig::getValue('user_orcid', 'clientSecret');
$appUri = \OC::$WEBROOT . '/apps/user_orcid/receive_orcid.php';
$redirectURL = (empty($_SERVER['HTTPS'])?'http':'https') . '://' . $_SERVER['SERVER_NAME'] .
$appUri;
$content = "client_id=".$clientAppID."&".
"client_secret=".$clientSecret."&".
"grant_type=authorization_code&".
"code=".$code."&".
"redirect_uri=".$redirectURL;
$url = "https://orcid.org/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_UNRESTRICTED_AUTH, TRUE);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($status===0 || $status>=300 || $json_response===null || $json_response===false){
\OCP\Util::writeLog('user_orcid', 'ERROR: bad ws response. '.$json_response, \OC_Log::ERROR);
OCP\JSON::error();
}
else{
$response = json_decode($json_response, true);
\OCP\Util::writeLog('user_orcid','Got token: '.serialize($response), \OC_Log::WARN);
}
if(!empty($response) && !empty($response['orcid'])){
if(!\OC_User::isLoggedIn()){
$user = OCA\FilesOrcid\Lib::getUserFromOrcid($response['orcid']);
if(!empty($user)){
// Successful login
if(OCP\App::isEnabled('files_sharding') && \OCA\FilesSharding\Lib::isMaster()){
$userServer = OCA\FilesSharding\Lib::getServerForUser($user);
if(!empty($userServer)){
$parsedRedirect = parse_url($userServer);
if($_SERVER['HTTP_HOST']!==$parsedRedirect['host']){
$redirect = $userServer;
$orcidClientAppID = OC_Appconfig::getValue('user_orcid', 'clientAppID');
}
}
}
if(!empty($redirect)){
\OC_User::setUserId($user);
// Redirect (and relogin on slave)
$url = "https://orcid.org/oauth/authorize?client_id=".
$orcidClientAppID."&response_type=code&scope=/authenticate&redirect_uri=".
rtrim($userServer, '/')."/apps/user_orcid/receive_orcid.php";
header('Location: '.$url);
exit();
}
else{
// Local user
\OC_Util::teardownFS();
//\OC\Files\Filesystem::initMountPoints($owner);
\OC_User::setUserId($user);
\OC_Util::setupFS($user);
\OCP\Util::writeLog('user_orcid', 'Logged in user: '.$user.', user: '.\OCP\USER::getUser(), \OC_Log::WARN);
OC_Util::redirectToDefaultPage();
}
}
else{
failedLogin();
}
}
else{
// ORCID setting
OCA\FilesOrcid\Lib::setOrcid($user, $response['orcid']);
//\OCP\Config::setUserValue($user, 'user_orcid', 'access_token', $response['access_token']);
$tmpl = new OCP\Template("user_orcid", "thanks");
echo $tmpl->fetchPage();
}
}
else{
if(!\OC_User::isLoggedIn()){
// Failed login attempt
failedLogin();
}
else{
// Failed ORCID setting
OCP\JSON::error();
}
}
//////////////////////////
function failedLogin(){
$location = empty(OC::$WEBROOT)?'/':OC::$WEBROOT;
$message = "Login with ORCID failed. ".
"Notice: You must first log in via institution, then attach your ORCID ID in your settings.";
header('Location: '.$location."?message=".urlencode($message));
exit();
}