forked from fiasco/next-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleOauth.php
More file actions
180 lines (158 loc) · 6.15 KB
/
Copy pathSimpleOauth.php
File metadata and controls
180 lines (158 loc) · 6.15 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Drupal\next\Plugin\Next\PreviewUrlGenerator;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\next\Entity\NextSiteInterface;
use Drupal\next\Exception\InvalidPreviewUrlRequest;
use Drupal\next\Plugin\ConfigurablePreviewUrlGeneratorBase;
use Drupal\next\PreviewSecretGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the preview_url_generator plugin based on simple_oauth.
*
* @PreviewUrlGenerator(
* id = "simple_oauth",
* label = "Simple OAuth",
* description = "This plugin generates token for role-based access control.
* Access control is handle using OAuth scopes."
* )
*/
class SimpleOauth extends ConfigurablePreviewUrlGeneratorBase {
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected ModuleHandlerInterface $moduleHandler;
/**
* SimpleOauth constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\next\PreviewSecretGeneratorInterface $preview_secret_generator
* The preview secret generator.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $current_user, TimeInterface $time, PreviewSecretGeneratorInterface $preview_secret_generator, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $current_user, $time, $preview_secret_generator, $entity_type_manager);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user'),
$container->get('datetime.time'),
$container->get('next.preview_secret_generator'),
$container->get('entity_type.manager'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'secret_expiration' => NULL,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['secret_expiration'] = [
'#title' => $this->t('Secret expiration time'),
'#description' => $this->t('The value, in seconds, to be used as expiration time for the validation secret. <strong>It is recommended to use short-lived secrets for increased security.</strong>'),
'#type' => 'number',
'#required' => TRUE,
'#default_value' => $this->configuration['secret_expiration'],
];
if ($this->moduleHandler->moduleExists('jwt')) {
$form['jwt_error'] = [
'#weight' => -100,
'#theme' => 'status_messages',
'#message_list' => [
MessengerInterface::TYPE_ERROR => [
$this->t('The JSON Web Tokens module is not compatible with the Simple OAuth module. You should uninstall it when using the Simple OAuth plugin.'),
],
],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['secret_expiration'] = $form_state->getValue('secret_expiration');
}
/**
* {@inheritdoc}
*/
public function generate(NextSiteInterface $next_site, EntityInterface $entity, string $resource_version = NULL): ?Url {
$query = [];
$query['path'] = $path = $entity->toUrl()->toString();
if($resource_version) {
$query['path'] = $query['path'].'?resourceVersion='.$resource_version;
}
// Create a secret based on the timestamp, path, scope and resource version.
$query['timestamp'] = $timestamp = $this->time->getRequestTime();
$query['secret'] = $this->previewSecretGenerator->generate($timestamp . $path . $resource_version);
return Url::fromUri($next_site->getPreviewUrl(), [
'query' => $query,
]);
}
/**
* {@inheritdoc}
*/
public function validate(Request $request) {
$body = Json::decode($request->getContent());
// Validate the path.
// We do not check for existing path. We let the next.js site handle this.
if (empty($body['path'])) {
throw new InvalidPreviewUrlRequest("Field 'path' is missing");
}
// Validate the timestamp.
if (empty($body['timestamp'])) {
throw new InvalidPreviewUrlRequest("Field 'timestamp' is missing");
}
$timestamp = (int) $body['timestamp'];
if ($this->time->getRequestTime() > $timestamp + (int) $this->configuration['secret_expiration']) {
throw new InvalidPreviewUrlRequest("The provided secret has expired.");
}
// Validate the secret.
if (empty($body['secret'])) {
throw new InvalidPreviewUrlRequest("Field 'secret' is missing");
}
if ($body['secret'] !== $this->previewSecretGenerator->generate($body['timestamp'] . $body['path'] . $body['resourceVersion'])) {
throw new InvalidPreviewUrlRequest("The provided secret is invalid.");
}
return [
'path' => $body['path'],
'maxAge' => (int) $this->configuration['secret_expiration'],
];
}
}