-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOAuth2.php
More file actions
72 lines (60 loc) · 2.68 KB
/
Copy pathOAuth2.php
File metadata and controls
72 lines (60 loc) · 2.68 KB
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
<?php
/**
* REDCap External Module: REDCap REST
* Send API calls when saving particular instruments when a trigger condition is met.
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\REDCapREST;
abstract class OAuth2 {
protected $response;
protected array $info;
protected $module;
protected $instruction;
protected $instruction_index;
protected $token_endpoint;
protected $client_id;
protected $client_secret;
protected $access_token;
protected $access_token_expiry;
protected $refresh_token;
public function __construct(REDCapREST $module, array $instruction, int $index) {
$this->response = '';
$this->info = array('http_code' => 500);
$this->module = $module;
$this->instruction = $instruction;
$this->instruction_index = $index;
$configString = $this->module->pipeApiToken($instruction['oauth2-config']);
$config = json_decode($configString, true);
$this->token_endpoint = $config['auth-url'];
$this->client_id = $config['client-id'];
$this->client_secret = $config['client-secret'];
$cache = json_decode($instruction['oauth2-cache'], true);
$this->access_token = (isset($cache['access_token'])) ? $cache['access_token'] : null;
$this->access_token_expiry = (isset($cache['access_token_expiry'])) ? new \DateTime($cache['access_token_expiry']) : null;
$this->refresh_token = (isset($cache['refresh_token'])) ? $cache['refresh_token'] : null;
}
abstract public function oauth2Call(string $method, string $url, string $contentType, array $headers, array $curlOptions, string $payload, $allowRetry=false): void;
/**
* updateAccessToken()
* If there is a cached and unexpired access token then do nothing, otherwise obtain a new token
*/
abstract protected function updateAccessToken(): void;
/**
* saveAccessToken()
* Save access token and expiry time to current index of project settings (hidden oauth-cache setting)
*/
protected function saveAccessToken() {
$cache = json_decode($this->instruction['oauth2-cache'], true);
$cache['access_token'] = $this->access_token;
$cache['access_token_expiry'] = $this->access_token_expiry->format('Y-m-d H:i:s');
$projectSetting = $this->module->getProjectSetting("oauth2-cache");
$projectSetting[$this->instruction_index] = json_encode($cache, JSON_FORCE_OBJECT);
$this->module->setProjectSetting("oauth2-cache", $projectSetting);
}
public function getResponse(): string {
return $this->response;
}
public function getInfo(): array {
return $this->info;
}
}