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