Completed
Pull Request — master (#3614)
by Björn
22:45
created
apps/federation/lib/Controller/OCSAuthAPIController.php 1 patch
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -46,169 +46,169 @@
 block discarded – undo
46 46
  */
47 47
 class OCSAuthAPIController extends OCSController{
48 48
 
49
-	/** @var ISecureRandom  */
50
-	private $secureRandom;
51
-
52
-	/** @var IJobList */
53
-	private $jobList;
54
-
55
-	/** @var TrustedServers */
56
-	private $trustedServers;
57
-
58
-	/** @var DbHandler */
59
-	private $dbHandler;
60
-
61
-	/** @var ILogger */
62
-	private $logger;
63
-
64
-	/**
65
-	 * OCSAuthAPI constructor.
66
-	 *
67
-	 * @param string $appName
68
-	 * @param IRequest $request
69
-	 * @param ISecureRandom $secureRandom
70
-	 * @param IJobList $jobList
71
-	 * @param TrustedServers $trustedServers
72
-	 * @param DbHandler $dbHandler
73
-	 * @param ILogger $logger
74
-	 */
75
-	public function __construct(
76
-		$appName,
77
-		IRequest $request,
78
-		ISecureRandom $secureRandom,
79
-		IJobList $jobList,
80
-		TrustedServers $trustedServers,
81
-		DbHandler $dbHandler,
82
-		ILogger $logger
83
-	) {
84
-		parent::__construct($appName, $request);
85
-
86
-		$this->secureRandom = $secureRandom;
87
-		$this->jobList = $jobList;
88
-		$this->trustedServers = $trustedServers;
89
-		$this->dbHandler = $dbHandler;
90
-		$this->logger = $logger;
91
-	}
92
-
93
-	/**
94
-	 * @NoCSRFRequired
95
-	 * @PublicPage
96
-	 *
97
-	 * request received to ask remote server for a shared secret, for legacy end-points
98
-	 *
99
-	 * @param string $url
100
-	 * @param string $token
101
-	 * @return Http\DataResponse
102
-	 * @throws OCSForbiddenException
103
-	 */
104
-	public function requestSharedSecretLegacy($url, $token) {
105
-		return $this->requestSharedSecret($url, $token);
106
-	}
107
-
108
-
109
-	/**
110
-	 * @NoCSRFRequired
111
-	 * @PublicPage
112
-	 *
113
-	 * create shared secret and return it, for legacy end-points
114
-	 *
115
-	 * @param string $url
116
-	 * @param string $token
117
-	 * @return Http\DataResponse
118
-	 * @throws OCSForbiddenException
119
-	 */
120
-	public function getSharedSecretLegacy($url, $token) {
121
-		return $this->getSharedSecret($url, $token);
122
-	}
123
-
124
-	/**
125
-	 * @NoCSRFRequired
126
-	 * @PublicPage
127
-	 *
128
-	 * request received to ask remote server for a shared secret
129
-	 *
130
-	 * @param string $url
131
-	 * @param string $token
132
-	 * @return Http\DataResponse
133
-	 * @throws OCSForbiddenException
134
-	 */
135
-	public function requestSharedSecret($url, $token) {
136
-		if ($this->trustedServers->isTrustedServer($url) === false) {
137
-			$this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
138
-			throw new OCSForbiddenException();
139
-		}
140
-
141
-		// if both server initiated the exchange of the shared secret the greater
142
-		// token wins
143
-		$localToken = $this->dbHandler->getToken($url);
144
-		if (strcmp($localToken, $token) > 0) {
145
-			$this->logger->info(
146
-				'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.',
147
-				['app' => 'federation']
148
-			);
149
-			throw new OCSForbiddenException();
150
-		}
151
-
152
-		// we ask for the shared secret so we no longer have to ask the other server
153
-		// to request the shared secret
154
-		$this->jobList->remove('OCA\Federation\BackgroundJob\RequestSharedSecret',
155
-			[
156
-				'url' => $url,
157
-				'token' => $localToken
158
-			]
159
-		);
160
-
161
-		$this->jobList->add(
162
-			'OCA\Federation\BackgroundJob\GetSharedSecret',
163
-			[
164
-				'url' => $url,
165
-				'token' => $token,
166
-			]
167
-		);
168
-
169
-		return new Http\DataResponse();
170
-	}
171
-
172
-	/**
173
-	 * @NoCSRFRequired
174
-	 * @PublicPage
175
-	 *
176
-	 * create shared secret and return it
177
-	 *
178
-	 * @param string $url
179
-	 * @param string $token
180
-	 * @return Http\DataResponse
181
-	 * @throws OCSForbiddenException
182
-	 */
183
-	public function getSharedSecret($url, $token) {
184
-		if ($this->trustedServers->isTrustedServer($url) === false) {
185
-			$this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
186
-			throw new OCSForbiddenException();
187
-		}
188
-
189
-		if ($this->isValidToken($url, $token) === false) {
190
-			$expectedToken = $this->dbHandler->getToken($url);
191
-			$this->logger->error(
192
-				'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
193
-				['app' => 'federation']
194
-			);
195
-			throw new OCSForbiddenException();
196
-		}
197
-
198
-		$sharedSecret = $this->secureRandom->generate(32);
199
-
200
-		$this->trustedServers->addSharedSecret($url, $sharedSecret);
201
-		// reset token after the exchange of the shared secret was successful
202
-		$this->dbHandler->addToken($url, '');
203
-
204
-		return new Http\DataResponse([
205
-			'sharedSecret' => $sharedSecret
206
-		]);
207
-	}
208
-
209
-	protected function isValidToken($url, $token) {
210
-		$storedToken = $this->dbHandler->getToken($url);
211
-		return hash_equals($storedToken, $token);
212
-	}
49
+    /** @var ISecureRandom  */
50
+    private $secureRandom;
51
+
52
+    /** @var IJobList */
53
+    private $jobList;
54
+
55
+    /** @var TrustedServers */
56
+    private $trustedServers;
57
+
58
+    /** @var DbHandler */
59
+    private $dbHandler;
60
+
61
+    /** @var ILogger */
62
+    private $logger;
63
+
64
+    /**
65
+     * OCSAuthAPI constructor.
66
+     *
67
+     * @param string $appName
68
+     * @param IRequest $request
69
+     * @param ISecureRandom $secureRandom
70
+     * @param IJobList $jobList
71
+     * @param TrustedServers $trustedServers
72
+     * @param DbHandler $dbHandler
73
+     * @param ILogger $logger
74
+     */
75
+    public function __construct(
76
+        $appName,
77
+        IRequest $request,
78
+        ISecureRandom $secureRandom,
79
+        IJobList $jobList,
80
+        TrustedServers $trustedServers,
81
+        DbHandler $dbHandler,
82
+        ILogger $logger
83
+    ) {
84
+        parent::__construct($appName, $request);
85
+
86
+        $this->secureRandom = $secureRandom;
87
+        $this->jobList = $jobList;
88
+        $this->trustedServers = $trustedServers;
89
+        $this->dbHandler = $dbHandler;
90
+        $this->logger = $logger;
91
+    }
92
+
93
+    /**
94
+     * @NoCSRFRequired
95
+     * @PublicPage
96
+     *
97
+     * request received to ask remote server for a shared secret, for legacy end-points
98
+     *
99
+     * @param string $url
100
+     * @param string $token
101
+     * @return Http\DataResponse
102
+     * @throws OCSForbiddenException
103
+     */
104
+    public function requestSharedSecretLegacy($url, $token) {
105
+        return $this->requestSharedSecret($url, $token);
106
+    }
107
+
108
+
109
+    /**
110
+     * @NoCSRFRequired
111
+     * @PublicPage
112
+     *
113
+     * create shared secret and return it, for legacy end-points
114
+     *
115
+     * @param string $url
116
+     * @param string $token
117
+     * @return Http\DataResponse
118
+     * @throws OCSForbiddenException
119
+     */
120
+    public function getSharedSecretLegacy($url, $token) {
121
+        return $this->getSharedSecret($url, $token);
122
+    }
123
+
124
+    /**
125
+     * @NoCSRFRequired
126
+     * @PublicPage
127
+     *
128
+     * request received to ask remote server for a shared secret
129
+     *
130
+     * @param string $url
131
+     * @param string $token
132
+     * @return Http\DataResponse
133
+     * @throws OCSForbiddenException
134
+     */
135
+    public function requestSharedSecret($url, $token) {
136
+        if ($this->trustedServers->isTrustedServer($url) === false) {
137
+            $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
138
+            throw new OCSForbiddenException();
139
+        }
140
+
141
+        // if both server initiated the exchange of the shared secret the greater
142
+        // token wins
143
+        $localToken = $this->dbHandler->getToken($url);
144
+        if (strcmp($localToken, $token) > 0) {
145
+            $this->logger->info(
146
+                'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.',
147
+                ['app' => 'federation']
148
+            );
149
+            throw new OCSForbiddenException();
150
+        }
151
+
152
+        // we ask for the shared secret so we no longer have to ask the other server
153
+        // to request the shared secret
154
+        $this->jobList->remove('OCA\Federation\BackgroundJob\RequestSharedSecret',
155
+            [
156
+                'url' => $url,
157
+                'token' => $localToken
158
+            ]
159
+        );
160
+
161
+        $this->jobList->add(
162
+            'OCA\Federation\BackgroundJob\GetSharedSecret',
163
+            [
164
+                'url' => $url,
165
+                'token' => $token,
166
+            ]
167
+        );
168
+
169
+        return new Http\DataResponse();
170
+    }
171
+
172
+    /**
173
+     * @NoCSRFRequired
174
+     * @PublicPage
175
+     *
176
+     * create shared secret and return it
177
+     *
178
+     * @param string $url
179
+     * @param string $token
180
+     * @return Http\DataResponse
181
+     * @throws OCSForbiddenException
182
+     */
183
+    public function getSharedSecret($url, $token) {
184
+        if ($this->trustedServers->isTrustedServer($url) === false) {
185
+            $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
186
+            throw new OCSForbiddenException();
187
+        }
188
+
189
+        if ($this->isValidToken($url, $token) === false) {
190
+            $expectedToken = $this->dbHandler->getToken($url);
191
+            $this->logger->error(
192
+                'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
193
+                ['app' => 'federation']
194
+            );
195
+            throw new OCSForbiddenException();
196
+        }
197
+
198
+        $sharedSecret = $this->secureRandom->generate(32);
199
+
200
+        $this->trustedServers->addSharedSecret($url, $sharedSecret);
201
+        // reset token after the exchange of the shared secret was successful
202
+        $this->dbHandler->addToken($url, '');
203
+
204
+        return new Http\DataResponse([
205
+            'sharedSecret' => $sharedSecret
206
+        ]);
207
+    }
208
+
209
+    protected function isValidToken($url, $token) {
210
+        $storedToken = $this->dbHandler->getToken($url);
211
+        return hash_equals($storedToken, $token);
212
+    }
213 213
 
214 214
 }
Please login to merge, or discard this patch.
apps/federation/appinfo/routes.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -23,50 +23,50 @@
 block discarded – undo
23 23
 $application = new \OCA\Federation\AppInfo\Application();
24 24
 
25 25
 $application->registerRoutes(
26
-	$this,
27
-	[
28
-		'routes' => [
29
-			[
30
-				'name' => 'Settings#addServer',
31
-				'url' => '/trusted-servers',
32
-				'verb' => 'POST'
33
-			],
34
-			[
35
-				'name' => 'Settings#removeServer',
36
-				'url' => '/trusted-servers/{id}',
37
-				'verb' => 'DELETE'
38
-			],
39
-			[
40
-				'name' => 'Settings#autoAddServers',
41
-				'url' => '/auto-add-servers',
42
-				'verb' => 'POST'
43
-			],
44
-		],
45
-		'ocs' => [
46
-			// old endpoints, only used by Nextcloud and ownCloud
47
-			[
48
-				'name' => 'OCSAuthAPI#getSharedSecretLegacy',
49
-				'url' => '/api/v1/shared-secret',
50
-				'verb' => 'GET',
51
-			],
52
-			[
53
-				'name' => 'OCSAuthAPI#requestSharedSecretLegacy',
54
-				'url' => '/api/v1/request-shared-secret',
55
-				'verb' => 'POST',
56
-			],
57
-			// new endpoints, published as public api
58
-			[
59
-				'name' => 'OCSAuthAPI#getSharedSecret',
60
-				'root' => '/cloud',
61
-				'url' => '/shared-secret',
62
-				'verb' => 'GET',
63
-			],
64
-			[
65
-				'name' => 'OCSAuthAPI#requestSharedSecret',
66
-				'root' => '/cloud',
67
-				'url' => '/shared-secret',
68
-				'verb' => 'POST',
69
-			],
70
-		],
71
-	]
26
+    $this,
27
+    [
28
+        'routes' => [
29
+            [
30
+                'name' => 'Settings#addServer',
31
+                'url' => '/trusted-servers',
32
+                'verb' => 'POST'
33
+            ],
34
+            [
35
+                'name' => 'Settings#removeServer',
36
+                'url' => '/trusted-servers/{id}',
37
+                'verb' => 'DELETE'
38
+            ],
39
+            [
40
+                'name' => 'Settings#autoAddServers',
41
+                'url' => '/auto-add-servers',
42
+                'verb' => 'POST'
43
+            ],
44
+        ],
45
+        'ocs' => [
46
+            // old endpoints, only used by Nextcloud and ownCloud
47
+            [
48
+                'name' => 'OCSAuthAPI#getSharedSecretLegacy',
49
+                'url' => '/api/v1/shared-secret',
50
+                'verb' => 'GET',
51
+            ],
52
+            [
53
+                'name' => 'OCSAuthAPI#requestSharedSecretLegacy',
54
+                'url' => '/api/v1/request-shared-secret',
55
+                'verb' => 'POST',
56
+            ],
57
+            // new endpoints, published as public api
58
+            [
59
+                'name' => 'OCSAuthAPI#getSharedSecret',
60
+                'root' => '/cloud',
61
+                'url' => '/shared-secret',
62
+                'verb' => 'GET',
63
+            ],
64
+            [
65
+                'name' => 'OCSAuthAPI#requestSharedSecret',
66
+                'root' => '/cloud',
67
+                'url' => '/shared-secret',
68
+                'verb' => 'POST',
69
+            ],
70
+        ],
71
+    ]
72 72
 );
Please login to merge, or discard this patch.