forked from contentacms/contenta_jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontenta_jsonapi.install
69 lines (62 loc) · 2.49 KB
/
contenta_jsonapi.install
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
<?php
use Drupal\Component\Serialization\Yaml;
/**
* Implements hook_install().
*/
function contenta_jsonapi_install() {
// Enable CORS for localhost.
/** @var \Drupal\Core\DrupalKernelInterface $drupal_kernel */
$drupal_kernel = \Drupal::service('kernel');
$file_path = $drupal_kernel->getAppRoot() . '/' . $drupal_kernel->getSitePath();
$filename = $file_path . '/default.services.yml';
if (file_exists($filename)) {
$services_yml = file_get_contents($filename);
$yml_data = Yaml::decode($services_yml);
if (empty($yml_data['parameters']['cors.config']['enabled'])) {
$yml_data['parameters']['cors.config']['enabled'] = TRUE;
$yml_data['parameters']['cors.config']['allowedHeaders'] = ['*'];
$yml_data['parameters']['cors.config']['allowedMethods'] = ['*'];
$yml_data['parameters']['cors.config']['allowedOrigins'] = ['localhost:*'];
file_put_contents($filename, Yaml::encode($yml_data));
}
}
// Now generate the OAuth2 keys.
_contenta_jsonapi_generate_keys();
}
/**
* Generates the OAuth Keys.
*/
function _contenta_jsonapi_generate_keys() {
// Build all the dependencies manually to avoid having to rely on the
// container to be ready.
$dir_name = 'keys';
/** @var \Drupal\simple_oauth\Service\KeyGeneratorService $key_gen */
$key_gen = \Drupal::service('simple_oauth.key.generator');
/** @var \Drupal\simple_oauth\Service\Filesystem\Filesystem $file_system */
$file_system = \Drupal::service('simple_oauth.filesystem');
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
$logger = \Drupal::service('logger.channel.contentacms');
$relative_path = DRUPAL_ROOT . '/../' . $dir_name;
if (!$file_system->isDirectory($relative_path)) {
$file_system->mkdir($relative_path);
}
$keys_path = $file_system->realpath($relative_path);
$pub_filename = sprintf('%s/public.key', $keys_path);
$pri_filename = sprintf('%s/private.key', $keys_path);
if ($file_system->fileExist($pub_filename) && $file_system->fileExist($pri_filename)) {
// 1. If the file already exists, then just set the correct permissions.
$file_system->chmod($pub_filename, 0600);
$file_system->chmod($pri_filename, 0600);
$logger->info('Key pair for OAuth 2 token signing already exists.');
}
else {
// 2. Generate the pair in the selected directory.
try {
$key_gen->generateKeys($keys_path);
} catch (\Exception $e) {
// Unable to generate files after all.
$logger->error($e->getMessage());
return;
}
}
}