Passed
Push — master ( 6846a8...5b6112 )
by Julius
15:13
created
apps/federation/lib/BackgroundJob/GetSharedSecret.php 2 patches
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -52,191 +52,191 @@
 block discarded – undo
52 52
  */
53 53
 class GetSharedSecret extends Job {
54 54
 
55
-	/** @var IClient */
56
-	private $httpClient;
57
-
58
-	/** @var IJobList */
59
-	private $jobList;
60
-
61
-	/** @var IURLGenerator */
62
-	private $urlGenerator;
63
-
64
-	/** @var TrustedServers  */
65
-	private $trustedServers;
66
-
67
-	/** @var IDiscoveryService  */
68
-	private $ocsDiscoveryService;
69
-
70
-	/** @var ILogger */
71
-	private $logger;
72
-
73
-	/** @var bool */
74
-	protected $retainJob = false;
75
-
76
-	private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
77
-
78
-	/** @var  int  30 day = 2592000sec */
79
-	private $maxLifespan = 2592000;
80
-
81
-	/**
82
-	 * RequestSharedSecret constructor.
83
-	 *
84
-	 * @param IClientService $httpClientService
85
-	 * @param IURLGenerator $urlGenerator
86
-	 * @param IJobList $jobList
87
-	 * @param TrustedServers $trustedServers
88
-	 * @param ILogger $logger
89
-	 * @param IDiscoveryService $ocsDiscoveryService
90
-	 * @param ITimeFactory $timeFactory
91
-	 */
92
-	public function __construct(
93
-		IClientService $httpClientService,
94
-		IURLGenerator $urlGenerator,
95
-		IJobList $jobList,
96
-		TrustedServers $trustedServers,
97
-		ILogger $logger,
98
-		IDiscoveryService $ocsDiscoveryService,
99
-		ITimeFactory $timeFactory
100
-	) {
101
-		parent::__construct($timeFactory);
102
-		$this->logger = $logger;
103
-		$this->httpClient = $httpClientService->newClient();
104
-		$this->jobList = $jobList;
105
-		$this->urlGenerator = $urlGenerator;
106
-		$this->ocsDiscoveryService = $ocsDiscoveryService;
107
-		$this->trustedServers = $trustedServers;
108
-	}
109
-
110
-	/**
111
-	 * run the job, then remove it from the joblist
112
-	 *
113
-	 * @param IJobList $jobList
114
-	 * @param ILogger|null $logger
115
-	 */
116
-	public function execute(IJobList $jobList, ILogger $logger = null) {
117
-		$target = $this->argument['url'];
118
-		// only execute if target is still in the list of trusted domains
119
-		if ($this->trustedServers->isTrustedServer($target)) {
120
-			$this->parentExecute($jobList, $logger);
121
-		}
122
-
123
-		$jobList->remove($this, $this->argument);
124
-
125
-		if ($this->retainJob) {
126
-			$this->reAddJob($this->argument);
127
-		}
128
-	}
129
-
130
-	/**
131
-	 * call execute() method of parent
132
-	 *
133
-	 * @param IJobList $jobList
134
-	 * @param ILogger $logger
135
-	 */
136
-	protected function parentExecute($jobList, $logger = null) {
137
-		parent::execute($jobList, $logger);
138
-	}
139
-
140
-	protected function run($argument) {
141
-		$target = $argument['url'];
142
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
143
-		$currentTime = $this->time->getTime();
144
-		$source = $this->urlGenerator->getAbsoluteURL('/');
145
-		$source = rtrim($source, '/');
146
-		$token = $argument['token'];
147
-
148
-		// kill job after 30 days of trying
149
-		$deadline = $currentTime - $this->maxLifespan;
150
-		if ($created < $deadline) {
151
-			$this->retainJob = false;
152
-			$this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE);
153
-			return;
154
-		}
155
-
156
-		$endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
157
-		$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
158
-
159
-		// make sure that we have a well formatted url
160
-		$url = rtrim($target, '/') . '/' . trim($endPoint, '/');
161
-
162
-		$result = null;
163
-		try {
164
-			$result = $this->httpClient->get(
165
-				$url,
166
-				[
167
-					'query' =>
168
-						[
169
-							'url' => $source,
170
-							'token' => $token,
171
-							'format' => 'json',
172
-						],
173
-					'timeout' => 3,
174
-					'connect_timeout' => 3,
175
-				]
176
-			);
177
-
178
-			$status = $result->getStatusCode();
179
-		} catch (ClientException $e) {
180
-			$status = $e->getCode();
181
-			if ($status === Http::STATUS_FORBIDDEN) {
182
-				$this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']);
183
-			} else {
184
-				$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
185
-			}
186
-		} catch (RequestException $e) {
187
-			$status = -1; // There is no status code if we could not connect
188
-			$this->logger->logException($e, [
189
-				'message' => 'Could not connect to ' . $target,
190
-				'level' => ILogger::INFO,
191
-				'app' => 'federation',
192
-			]);
193
-		} catch (\Throwable $e) {
194
-			$status = Http::STATUS_INTERNAL_SERVER_ERROR;
195
-			$this->logger->logException($e, ['app' => 'federation']);
196
-		}
197
-
198
-		// if we received a unexpected response we try again later
199
-		if (
200
-			$status !== Http::STATUS_OK
201
-			&& $status !== Http::STATUS_FORBIDDEN
202
-		) {
203
-			$this->retainJob = true;
204
-		}
205
-
206
-		if ($status === Http::STATUS_OK && $result instanceof IResponse) {
207
-			$body = $result->getBody();
208
-			$result = json_decode($body, true);
209
-			if (isset($result['ocs']['data']['sharedSecret'])) {
210
-				$this->trustedServers->addSharedSecret(
211
-						$target,
212
-						$result['ocs']['data']['sharedSecret']
213
-				);
214
-			} else {
215
-				$this->logger->error(
216
-						'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body,
217
-						['app' => 'federation']
218
-				);
219
-				$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
220
-			}
221
-		}
222
-	}
223
-
224
-	/**
225
-	 * re-add background job
226
-	 *
227
-	 * @param array $argument
228
-	 */
229
-	protected function reAddJob(array $argument) {
230
-		$url = $argument['url'];
231
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
232
-		$token = $argument['token'];
233
-		$this->jobList->add(
234
-			GetSharedSecret::class,
235
-			[
236
-				'url' => $url,
237
-				'token' => $token,
238
-				'created' => $created
239
-			]
240
-		);
241
-	}
55
+    /** @var IClient */
56
+    private $httpClient;
57
+
58
+    /** @var IJobList */
59
+    private $jobList;
60
+
61
+    /** @var IURLGenerator */
62
+    private $urlGenerator;
63
+
64
+    /** @var TrustedServers  */
65
+    private $trustedServers;
66
+
67
+    /** @var IDiscoveryService  */
68
+    private $ocsDiscoveryService;
69
+
70
+    /** @var ILogger */
71
+    private $logger;
72
+
73
+    /** @var bool */
74
+    protected $retainJob = false;
75
+
76
+    private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
77
+
78
+    /** @var  int  30 day = 2592000sec */
79
+    private $maxLifespan = 2592000;
80
+
81
+    /**
82
+     * RequestSharedSecret constructor.
83
+     *
84
+     * @param IClientService $httpClientService
85
+     * @param IURLGenerator $urlGenerator
86
+     * @param IJobList $jobList
87
+     * @param TrustedServers $trustedServers
88
+     * @param ILogger $logger
89
+     * @param IDiscoveryService $ocsDiscoveryService
90
+     * @param ITimeFactory $timeFactory
91
+     */
92
+    public function __construct(
93
+        IClientService $httpClientService,
94
+        IURLGenerator $urlGenerator,
95
+        IJobList $jobList,
96
+        TrustedServers $trustedServers,
97
+        ILogger $logger,
98
+        IDiscoveryService $ocsDiscoveryService,
99
+        ITimeFactory $timeFactory
100
+    ) {
101
+        parent::__construct($timeFactory);
102
+        $this->logger = $logger;
103
+        $this->httpClient = $httpClientService->newClient();
104
+        $this->jobList = $jobList;
105
+        $this->urlGenerator = $urlGenerator;
106
+        $this->ocsDiscoveryService = $ocsDiscoveryService;
107
+        $this->trustedServers = $trustedServers;
108
+    }
109
+
110
+    /**
111
+     * run the job, then remove it from the joblist
112
+     *
113
+     * @param IJobList $jobList
114
+     * @param ILogger|null $logger
115
+     */
116
+    public function execute(IJobList $jobList, ILogger $logger = null) {
117
+        $target = $this->argument['url'];
118
+        // only execute if target is still in the list of trusted domains
119
+        if ($this->trustedServers->isTrustedServer($target)) {
120
+            $this->parentExecute($jobList, $logger);
121
+        }
122
+
123
+        $jobList->remove($this, $this->argument);
124
+
125
+        if ($this->retainJob) {
126
+            $this->reAddJob($this->argument);
127
+        }
128
+    }
129
+
130
+    /**
131
+     * call execute() method of parent
132
+     *
133
+     * @param IJobList $jobList
134
+     * @param ILogger $logger
135
+     */
136
+    protected function parentExecute($jobList, $logger = null) {
137
+        parent::execute($jobList, $logger);
138
+    }
139
+
140
+    protected function run($argument) {
141
+        $target = $argument['url'];
142
+        $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
143
+        $currentTime = $this->time->getTime();
144
+        $source = $this->urlGenerator->getAbsoluteURL('/');
145
+        $source = rtrim($source, '/');
146
+        $token = $argument['token'];
147
+
148
+        // kill job after 30 days of trying
149
+        $deadline = $currentTime - $this->maxLifespan;
150
+        if ($created < $deadline) {
151
+            $this->retainJob = false;
152
+            $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE);
153
+            return;
154
+        }
155
+
156
+        $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
157
+        $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
158
+
159
+        // make sure that we have a well formatted url
160
+        $url = rtrim($target, '/') . '/' . trim($endPoint, '/');
161
+
162
+        $result = null;
163
+        try {
164
+            $result = $this->httpClient->get(
165
+                $url,
166
+                [
167
+                    'query' =>
168
+                        [
169
+                            'url' => $source,
170
+                            'token' => $token,
171
+                            'format' => 'json',
172
+                        ],
173
+                    'timeout' => 3,
174
+                    'connect_timeout' => 3,
175
+                ]
176
+            );
177
+
178
+            $status = $result->getStatusCode();
179
+        } catch (ClientException $e) {
180
+            $status = $e->getCode();
181
+            if ($status === Http::STATUS_FORBIDDEN) {
182
+                $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']);
183
+            } else {
184
+                $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
185
+            }
186
+        } catch (RequestException $e) {
187
+            $status = -1; // There is no status code if we could not connect
188
+            $this->logger->logException($e, [
189
+                'message' => 'Could not connect to ' . $target,
190
+                'level' => ILogger::INFO,
191
+                'app' => 'federation',
192
+            ]);
193
+        } catch (\Throwable $e) {
194
+            $status = Http::STATUS_INTERNAL_SERVER_ERROR;
195
+            $this->logger->logException($e, ['app' => 'federation']);
196
+        }
197
+
198
+        // if we received a unexpected response we try again later
199
+        if (
200
+            $status !== Http::STATUS_OK
201
+            && $status !== Http::STATUS_FORBIDDEN
202
+        ) {
203
+            $this->retainJob = true;
204
+        }
205
+
206
+        if ($status === Http::STATUS_OK && $result instanceof IResponse) {
207
+            $body = $result->getBody();
208
+            $result = json_decode($body, true);
209
+            if (isset($result['ocs']['data']['sharedSecret'])) {
210
+                $this->trustedServers->addSharedSecret(
211
+                        $target,
212
+                        $result['ocs']['data']['sharedSecret']
213
+                );
214
+            } else {
215
+                $this->logger->error(
216
+                        'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body,
217
+                        ['app' => 'federation']
218
+                );
219
+                $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
220
+            }
221
+        }
222
+    }
223
+
224
+    /**
225
+     * re-add background job
226
+     *
227
+     * @param array $argument
228
+     */
229
+    protected function reAddJob(array $argument) {
230
+        $url = $argument['url'];
231
+        $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
232
+        $token = $argument['token'];
233
+        $this->jobList->add(
234
+            GetSharedSecret::class,
235
+            [
236
+                'url' => $url,
237
+                'token' => $token,
238
+                'created' => $created
239
+            ]
240
+        );
241
+    }
242 242
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 
140 140
 	protected function run($argument) {
141 141
 		$target = $argument['url'];
142
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
142
+		$created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime();
143 143
 		$currentTime = $this->time->getTime();
144 144
 		$source = $this->urlGenerator->getAbsoluteURL('/');
145 145
 		$source = rtrim($source, '/');
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
 		$deadline = $currentTime - $this->maxLifespan;
150 150
 		if ($created < $deadline) {
151 151
 			$this->retainJob = false;
152
-			$this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE);
152
+			$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
153 153
 			return;
154 154
 		}
155 155
 
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 		$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
158 158
 
159 159
 		// make sure that we have a well formatted url
160
-		$url = rtrim($target, '/') . '/' . trim($endPoint, '/');
160
+		$url = rtrim($target, '/').'/'.trim($endPoint, '/');
161 161
 
162 162
 		$result = null;
163 163
 		try {
@@ -179,14 +179,14 @@  discard block
 block discarded – undo
179 179
 		} catch (ClientException $e) {
180 180
 			$status = $e->getCode();
181 181
 			if ($status === Http::STATUS_FORBIDDEN) {
182
-				$this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']);
182
+				$this->logger->info($target.' refused to exchange a shared secret with you.', ['app' => 'federation']);
183 183
 			} else {
184
-				$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
184
+				$this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']);
185 185
 			}
186 186
 		} catch (RequestException $e) {
187 187
 			$status = -1; // There is no status code if we could not connect
188 188
 			$this->logger->logException($e, [
189
-				'message' => 'Could not connect to ' . $target,
189
+				'message' => 'Could not connect to '.$target,
190 190
 				'level' => ILogger::INFO,
191 191
 				'app' => 'federation',
192 192
 			]);
@@ -213,7 +213,7 @@  discard block
 block discarded – undo
213 213
 				);
214 214
 			} else {
215 215
 				$this->logger->error(
216
-						'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body,
216
+						'remote server "'.$target.'"" does not return a valid shared secret. Received data: '.$body,
217 217
 						['app' => 'federation']
218 218
 				);
219 219
 				$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 	 */
229 229
 	protected function reAddJob(array $argument) {
230 230
 		$url = $argument['url'];
231
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
231
+		$created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime();
232 232
 		$token = $argument['token'];
233 233
 		$this->jobList->add(
234 234
 			GetSharedSecret::class,
Please login to merge, or discard this patch.
apps/federation/lib/BackgroundJob/RequestSharedSecret.php 2 patches
Indentation   +166 added lines, -166 removed lines patch added patch discarded remove patch
@@ -50,170 +50,170 @@
 block discarded – undo
50 50
  */
51 51
 class RequestSharedSecret extends Job {
52 52
 
53
-	/** @var IClient */
54
-	private $httpClient;
55
-
56
-	/** @var IJobList */
57
-	private $jobList;
58
-
59
-	/** @var IURLGenerator */
60
-	private $urlGenerator;
61
-
62
-	/** @var TrustedServers */
63
-	private $trustedServers;
64
-
65
-	/** @var IDiscoveryService  */
66
-	private $ocsDiscoveryService;
67
-
68
-	/** @var ILogger */
69
-	private $logger;
70
-
71
-	/** @var bool */
72
-	protected $retainJob = false;
73
-
74
-	private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret';
75
-
76
-	/** @var  int  30 day = 2592000sec */
77
-	private $maxLifespan = 2592000;
78
-
79
-	/**
80
-	 * RequestSharedSecret constructor.
81
-	 *
82
-	 * @param IClientService $httpClientService
83
-	 * @param IURLGenerator $urlGenerator
84
-	 * @param IJobList $jobList
85
-	 * @param TrustedServers $trustedServers
86
-	 * @param IDiscoveryService $ocsDiscoveryService
87
-	 * @param ILogger $logger
88
-	 * @param ITimeFactory $timeFactory
89
-	 */
90
-	public function __construct(
91
-		IClientService $httpClientService,
92
-		IURLGenerator $urlGenerator,
93
-		IJobList $jobList,
94
-		TrustedServers $trustedServers,
95
-		IDiscoveryService $ocsDiscoveryService,
96
-		ILogger $logger,
97
-		ITimeFactory $timeFactory
98
-	) {
99
-		parent::__construct($timeFactory);
100
-		$this->httpClient = $httpClientService->newClient();
101
-		$this->jobList = $jobList;
102
-		$this->urlGenerator = $urlGenerator;
103
-		$this->logger = $logger;
104
-		$this->ocsDiscoveryService = $ocsDiscoveryService;
105
-		$this->trustedServers = $trustedServers;
106
-	}
107
-
108
-
109
-	/**
110
-	 * run the job, then remove it from the joblist
111
-	 *
112
-	 * @param IJobList $jobList
113
-	 * @param ILogger|null $logger
114
-	 */
115
-	public function execute(IJobList $jobList, ILogger $logger = null) {
116
-		$target = $this->argument['url'];
117
-		// only execute if target is still in the list of trusted domains
118
-		if ($this->trustedServers->isTrustedServer($target)) {
119
-			$this->parentExecute($jobList, $logger);
120
-		}
121
-
122
-		$jobList->remove($this, $this->argument);
123
-
124
-		if ($this->retainJob) {
125
-			$this->reAddJob($this->argument);
126
-		}
127
-	}
128
-
129
-	/**
130
-	 * call execute() method of parent
131
-	 *
132
-	 * @param IJobList $jobList
133
-	 * @param ILogger $logger
134
-	 */
135
-	protected function parentExecute($jobList, $logger) {
136
-		parent::execute($jobList, $logger);
137
-	}
138
-
139
-	protected function run($argument) {
140
-		$target = $argument['url'];
141
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
142
-		$currentTime = $this->time->getTime();
143
-		$source = $this->urlGenerator->getAbsoluteURL('/');
144
-		$source = rtrim($source, '/');
145
-		$token = $argument['token'];
146
-
147
-		// kill job after 30 days of trying
148
-		$deadline = $currentTime - $this->maxLifespan;
149
-		if ($created < $deadline) {
150
-			$this->retainJob = false;
151
-			$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
152
-			return;
153
-		}
154
-
155
-		$endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
156
-		$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
157
-
158
-		// make sure that we have a well formated url
159
-		$url = rtrim($target, '/') . '/' . trim($endPoint, '/');
160
-
161
-		try {
162
-			$result = $this->httpClient->post(
163
-				$url,
164
-				[
165
-					'body' => [
166
-						'url' => $source,
167
-						'token' => $token,
168
-						'format' => 'json',
169
-					],
170
-					'timeout' => 3,
171
-					'connect_timeout' => 3,
172
-				]
173
-			);
174
-
175
-			$status = $result->getStatusCode();
176
-		} catch (ClientException $e) {
177
-			$status = $e->getCode();
178
-			if ($status === Http::STATUS_FORBIDDEN) {
179
-				$this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']);
180
-			} else {
181
-				$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
182
-			}
183
-		} catch (RequestException $e) {
184
-			$status = -1; // There is no status code if we could not connect
185
-			$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
186
-		} catch (\Throwable $e) {
187
-			$status = Http::STATUS_INTERNAL_SERVER_ERROR;
188
-			$this->logger->logException($e, ['app' => 'federation']);
189
-		}
190
-
191
-		// if we received a unexpected response we try again later
192
-		if (
193
-			$status !== Http::STATUS_OK
194
-			&& $status !== Http::STATUS_FORBIDDEN
195
-		) {
196
-			$this->retainJob = true;
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * re-add background job
202
-	 *
203
-	 * @param array $argument
204
-	 */
205
-	protected function reAddJob(array $argument) {
206
-		$url = $argument['url'];
207
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
208
-		$token = $argument['token'];
209
-
210
-		$this->jobList->add(
211
-			RequestSharedSecret::class,
212
-			[
213
-				'url' => $url,
214
-				'token' => $token,
215
-				'created' => $created
216
-			]
217
-		);
218
-	}
53
+    /** @var IClient */
54
+    private $httpClient;
55
+
56
+    /** @var IJobList */
57
+    private $jobList;
58
+
59
+    /** @var IURLGenerator */
60
+    private $urlGenerator;
61
+
62
+    /** @var TrustedServers */
63
+    private $trustedServers;
64
+
65
+    /** @var IDiscoveryService  */
66
+    private $ocsDiscoveryService;
67
+
68
+    /** @var ILogger */
69
+    private $logger;
70
+
71
+    /** @var bool */
72
+    protected $retainJob = false;
73
+
74
+    private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret';
75
+
76
+    /** @var  int  30 day = 2592000sec */
77
+    private $maxLifespan = 2592000;
78
+
79
+    /**
80
+     * RequestSharedSecret constructor.
81
+     *
82
+     * @param IClientService $httpClientService
83
+     * @param IURLGenerator $urlGenerator
84
+     * @param IJobList $jobList
85
+     * @param TrustedServers $trustedServers
86
+     * @param IDiscoveryService $ocsDiscoveryService
87
+     * @param ILogger $logger
88
+     * @param ITimeFactory $timeFactory
89
+     */
90
+    public function __construct(
91
+        IClientService $httpClientService,
92
+        IURLGenerator $urlGenerator,
93
+        IJobList $jobList,
94
+        TrustedServers $trustedServers,
95
+        IDiscoveryService $ocsDiscoveryService,
96
+        ILogger $logger,
97
+        ITimeFactory $timeFactory
98
+    ) {
99
+        parent::__construct($timeFactory);
100
+        $this->httpClient = $httpClientService->newClient();
101
+        $this->jobList = $jobList;
102
+        $this->urlGenerator = $urlGenerator;
103
+        $this->logger = $logger;
104
+        $this->ocsDiscoveryService = $ocsDiscoveryService;
105
+        $this->trustedServers = $trustedServers;
106
+    }
107
+
108
+
109
+    /**
110
+     * run the job, then remove it from the joblist
111
+     *
112
+     * @param IJobList $jobList
113
+     * @param ILogger|null $logger
114
+     */
115
+    public function execute(IJobList $jobList, ILogger $logger = null) {
116
+        $target = $this->argument['url'];
117
+        // only execute if target is still in the list of trusted domains
118
+        if ($this->trustedServers->isTrustedServer($target)) {
119
+            $this->parentExecute($jobList, $logger);
120
+        }
121
+
122
+        $jobList->remove($this, $this->argument);
123
+
124
+        if ($this->retainJob) {
125
+            $this->reAddJob($this->argument);
126
+        }
127
+    }
128
+
129
+    /**
130
+     * call execute() method of parent
131
+     *
132
+     * @param IJobList $jobList
133
+     * @param ILogger $logger
134
+     */
135
+    protected function parentExecute($jobList, $logger) {
136
+        parent::execute($jobList, $logger);
137
+    }
138
+
139
+    protected function run($argument) {
140
+        $target = $argument['url'];
141
+        $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
142
+        $currentTime = $this->time->getTime();
143
+        $source = $this->urlGenerator->getAbsoluteURL('/');
144
+        $source = rtrim($source, '/');
145
+        $token = $argument['token'];
146
+
147
+        // kill job after 30 days of trying
148
+        $deadline = $currentTime - $this->maxLifespan;
149
+        if ($created < $deadline) {
150
+            $this->retainJob = false;
151
+            $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
152
+            return;
153
+        }
154
+
155
+        $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
156
+        $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
157
+
158
+        // make sure that we have a well formated url
159
+        $url = rtrim($target, '/') . '/' . trim($endPoint, '/');
160
+
161
+        try {
162
+            $result = $this->httpClient->post(
163
+                $url,
164
+                [
165
+                    'body' => [
166
+                        'url' => $source,
167
+                        'token' => $token,
168
+                        'format' => 'json',
169
+                    ],
170
+                    'timeout' => 3,
171
+                    'connect_timeout' => 3,
172
+                ]
173
+            );
174
+
175
+            $status = $result->getStatusCode();
176
+        } catch (ClientException $e) {
177
+            $status = $e->getCode();
178
+            if ($status === Http::STATUS_FORBIDDEN) {
179
+                $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']);
180
+            } else {
181
+                $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
182
+            }
183
+        } catch (RequestException $e) {
184
+            $status = -1; // There is no status code if we could not connect
185
+            $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
186
+        } catch (\Throwable $e) {
187
+            $status = Http::STATUS_INTERNAL_SERVER_ERROR;
188
+            $this->logger->logException($e, ['app' => 'federation']);
189
+        }
190
+
191
+        // if we received a unexpected response we try again later
192
+        if (
193
+            $status !== Http::STATUS_OK
194
+            && $status !== Http::STATUS_FORBIDDEN
195
+        ) {
196
+            $this->retainJob = true;
197
+        }
198
+    }
199
+
200
+    /**
201
+     * re-add background job
202
+     *
203
+     * @param array $argument
204
+     */
205
+    protected function reAddJob(array $argument) {
206
+        $url = $argument['url'];
207
+        $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
208
+        $token = $argument['token'];
209
+
210
+        $this->jobList->add(
211
+            RequestSharedSecret::class,
212
+            [
213
+                'url' => $url,
214
+                'token' => $token,
215
+                'created' => $created
216
+            ]
217
+        );
218
+    }
219 219
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 
139 139
 	protected function run($argument) {
140 140
 		$target = $argument['url'];
141
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
141
+		$created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime();
142 142
 		$currentTime = $this->time->getTime();
143 143
 		$source = $this->urlGenerator->getAbsoluteURL('/');
144 144
 		$source = rtrim($source, '/');
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 		$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
157 157
 
158 158
 		// make sure that we have a well formated url
159
-		$url = rtrim($target, '/') . '/' . trim($endPoint, '/');
159
+		$url = rtrim($target, '/').'/'.trim($endPoint, '/');
160 160
 
161 161
 		try {
162 162
 			$result = $this->httpClient->post(
@@ -176,13 +176,13 @@  discard block
 block discarded – undo
176 176
 		} catch (ClientException $e) {
177 177
 			$status = $e->getCode();
178 178
 			if ($status === Http::STATUS_FORBIDDEN) {
179
-				$this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']);
179
+				$this->logger->info($target.' refused to ask for a shared secret.', ['app' => 'federation']);
180 180
 			} else {
181
-				$this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']);
181
+				$this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']);
182 182
 			}
183 183
 		} catch (RequestException $e) {
184 184
 			$status = -1; // There is no status code if we could not connect
185
-			$this->logger->info('Could not connect to ' . $target, ['app' => 'federation']);
185
+			$this->logger->info('Could not connect to '.$target, ['app' => 'federation']);
186 186
 		} catch (\Throwable $e) {
187 187
 			$status = Http::STATUS_INTERNAL_SERVER_ERROR;
188 188
 			$this->logger->logException($e, ['app' => 'federation']);
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
 	 */
205 205
 	protected function reAddJob(array $argument) {
206 206
 		$url = $argument['url'];
207
-		$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
207
+		$created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime();
208 208
 		$token = $argument['token'];
209 209
 
210 210
 		$this->jobList->add(
Please login to merge, or discard this patch.