Completed
Push — stable13 ( 14c841...de761e )
by Roeland
09:51
created
core/templates/loginflow/authpicker.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
 	<h2><?php p($l->t('Account access')) ?></h2>
32 32
 	<p class="info">
33 33
 		<?php print_unescaped($l->t('You are about to grant %s access to your %s account.', [
34
-								'<strong>' . \OCP\Util::sanitizeHTML($_['client']) . '</strong>',
34
+								'<strong>'.\OCP\Util::sanitizeHTML($_['client']).'</strong>',
35 35
 								\OCP\Util::sanitizeHTML($_['instanceName'])
36 36
 							])) ?>
37 37
 	</p>
@@ -59,6 +59,6 @@  discard block
 block discarded – undo
59 59
 	</form>
60 60
 </div>
61 61
 
62
-<?php if(empty($_['oauthState'])): ?>
62
+<?php if (empty($_['oauthState'])): ?>
63 63
 <a id="app-token-login" class="warning" href="#"><?php p($l->t('Alternative login using app token')) ?></a>
64 64
 <?php endif; ?>
Please login to merge, or discard this patch.
core/routes.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -36,43 +36,43 @@  discard block
 block discarded – undo
36 36
 
37 37
 $application = new Application();
38 38
 $application->registerRoutes($this, [
39
-	'routes' => [
40
-		['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'],
41
-		['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'],
42
-		['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'],
43
-		['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'],
44
-		['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'],
45
-		['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'],
46
-		['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
47
-		['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
48
-		['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
49
-		['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],
50
-		['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'],
51
-		['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'],
52
-		['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'],
53
-		['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'],
54
-		['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'],
55
-		['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'],
56
-		['name' => 'ClientFlowLogin#apptokenRedirect', 'url' => '/login/flow/apptoken', 'verb' => 'POST'],
57
-		['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'],
58
-		['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'],
59
-		['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'],
60
-		['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'],
61
-		['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'],
62
-		['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'],
63
-		['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
64
-		['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
65
-		['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
66
-		['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
67
-		['name' => 'AutoComplete#get', 'url' => 'autocomplete/get', 'verb' => 'GET'],
68
-		['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'],
69
-	],
70
-	'ocs' => [
71
-		['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
72
-		['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
73
-		['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
74
-		['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],
75
-	],
39
+    'routes' => [
40
+        ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'],
41
+        ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'],
42
+        ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'],
43
+        ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'],
44
+        ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'],
45
+        ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'],
46
+        ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
47
+        ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
48
+        ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
49
+        ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],
50
+        ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'],
51
+        ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'],
52
+        ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'],
53
+        ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'],
54
+        ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'],
55
+        ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'],
56
+        ['name' => 'ClientFlowLogin#apptokenRedirect', 'url' => '/login/flow/apptoken', 'verb' => 'POST'],
57
+        ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'],
58
+        ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'],
59
+        ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'],
60
+        ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'],
61
+        ['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'],
62
+        ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'],
63
+        ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
64
+        ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
65
+        ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
66
+        ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
67
+        ['name' => 'AutoComplete#get', 'url' => 'autocomplete/get', 'verb' => 'GET'],
68
+        ['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'],
69
+    ],
70
+    'ocs' => [
71
+        ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
72
+        ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'],
73
+        ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'],
74
+        ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'],
75
+    ],
76 76
 ]);
77 77
 
78 78
 // Post installation check
@@ -81,15 +81,15 @@  discard block
 block discarded – undo
81 81
 // Core ajax actions
82 82
 // Search
83 83
 $this->create('search_ajax_search', '/core/search')
84
-	->actionInclude('core/search/ajax/search.php');
84
+    ->actionInclude('core/search/ajax/search.php');
85 85
 // Routing
86 86
 $this->create('core_ajax_update', '/core/ajax/update.php')
87
-	->actionInclude('core/ajax/update.php');
87
+    ->actionInclude('core/ajax/update.php');
88 88
 
89 89
 // File routes
90 90
 $this->create('files.viewcontroller.showFile', '/f/{fileid}')->action(function($urlParams) {
91
-	$app = new \OCA\Files\AppInfo\Application($urlParams);
92
-	$app->dispatch('ViewController', 'index');
91
+    $app = new \OCA\Files\AppInfo\Application($urlParams);
92
+    $app->dispatch('ViewController', 'index');
93 93
 });
94 94
 
95 95
 // Call routes
@@ -98,57 +98,57 @@  discard block
 block discarded – undo
98 98
  * @suppress PhanUndeclaredClassMethod
99 99
  */
100 100
 $this->create('spreed.pagecontroller.showCall', '/call/{token}')->action(function($urlParams) {
101
-	if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) {
102
-		$app = new \OCA\Spreed\AppInfo\Application($urlParams);
103
-		$app->dispatch('PageController', 'index');
104
-	} else {
105
-		throw new \OC\HintException('App spreed is not enabled');
106
-	}
101
+    if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) {
102
+        $app = new \OCA\Spreed\AppInfo\Application($urlParams);
103
+        $app->dispatch('PageController', 'index');
104
+    } else {
105
+        throw new \OC\HintException('App spreed is not enabled');
106
+    }
107 107
 });
108 108
 
109 109
 // Sharing routes
110 110
 $this->create('files_sharing.sharecontroller.showShare', '/s/{token}')->action(function($urlParams) {
111
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
112
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
113
-		$app->dispatch('ShareController', 'showShare');
114
-	} else {
115
-		throw new \OC\HintException('App file sharing is not enabled');
116
-	}
111
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
112
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
113
+        $app->dispatch('ShareController', 'showShare');
114
+    } else {
115
+        throw new \OC\HintException('App file sharing is not enabled');
116
+    }
117 117
 });
118 118
 $this->create('files_sharing.sharecontroller.authenticate', '/s/{token}/authenticate')->post()->action(function($urlParams) {
119
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
120
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
121
-		$app->dispatch('ShareController', 'authenticate');
122
-	} else {
123
-		throw new \OC\HintException('App file sharing is not enabled');
124
-	}
119
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
120
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
121
+        $app->dispatch('ShareController', 'authenticate');
122
+    } else {
123
+        throw new \OC\HintException('App file sharing is not enabled');
124
+    }
125 125
 });
126 126
 $this->create('files_sharing.sharecontroller.showAuthenticate', '/s/{token}/authenticate')->get()->action(function($urlParams) {
127
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
128
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
129
-		$app->dispatch('ShareController', 'showAuthenticate');
130
-	} else {
131
-		throw new \OC\HintException('App file sharing is not enabled');
132
-	}
127
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
128
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
129
+        $app->dispatch('ShareController', 'showAuthenticate');
130
+    } else {
131
+        throw new \OC\HintException('App file sharing is not enabled');
132
+    }
133 133
 });
134 134
 $this->create('files_sharing.sharecontroller.downloadShare', '/s/{token}/download')->get()->action(function($urlParams) {
135
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
136
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
137
-		$app->dispatch('ShareController', 'downloadShare');
138
-	} else {
139
-		throw new \OC\HintException('App file sharing is not enabled');
140
-	}
135
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
136
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
137
+        $app->dispatch('ShareController', 'downloadShare');
138
+    } else {
139
+        throw new \OC\HintException('App file sharing is not enabled');
140
+    }
141 141
 });
142 142
 $this->create('files_sharing.publicpreview.directLink', '/s/{token}/preview')->get()->action(function($urlParams) {
143
-	if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
144
-		$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
145
-		$app->dispatch('PublicPreviewController', 'directLink');
146
-	} else {
147
-		throw new \OC\HintException('App file sharing is not enabled');
148
-	}
143
+    if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
144
+        $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
145
+        $app->dispatch('PublicPreviewController', 'directLink');
146
+    } else {
147
+        throw new \OC\HintException('App file sharing is not enabled');
148
+    }
149 149
 });
150 150
 
151 151
 // used for heartbeat
152 152
 $this->create('heartbeat', '/heartbeat')->action(function(){
153
-	// do nothing
153
+    // do nothing
154 154
 });
Please login to merge, or discard this patch.
core/Controller/ClientFlowLoginController.php 2 patches
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -48,322 +48,322 @@
 block discarded – undo
48 48
 use OCP\Session\Exceptions\SessionNotAvailableException;
49 49
 
50 50
 class ClientFlowLoginController extends Controller {
51
-	/** @var IUserSession */
52
-	private $userSession;
53
-	/** @var IL10N */
54
-	private $l10n;
55
-	/** @var Defaults */
56
-	private $defaults;
57
-	/** @var ISession */
58
-	private $session;
59
-	/** @var IProvider */
60
-	private $tokenProvider;
61
-	/** @var ISecureRandom */
62
-	private $random;
63
-	/** @var IURLGenerator */
64
-	private $urlGenerator;
65
-	/** @var ClientMapper */
66
-	private $clientMapper;
67
-	/** @var AccessTokenMapper */
68
-	private $accessTokenMapper;
69
-	/** @var ICrypto */
70
-	private $crypto;
71
-
72
-	const stateName = 'client.flow.state.token';
73
-
74
-	/**
75
-	 * @param string $appName
76
-	 * @param IRequest $request
77
-	 * @param IUserSession $userSession
78
-	 * @param IL10N $l10n
79
-	 * @param Defaults $defaults
80
-	 * @param ISession $session
81
-	 * @param IProvider $tokenProvider
82
-	 * @param ISecureRandom $random
83
-	 * @param IURLGenerator $urlGenerator
84
-	 * @param ClientMapper $clientMapper
85
-	 * @param AccessTokenMapper $accessTokenMapper
86
-	 * @param ICrypto $crypto
87
-	 */
88
-	public function __construct($appName,
89
-								IRequest $request,
90
-								IUserSession $userSession,
91
-								IL10N $l10n,
92
-								Defaults $defaults,
93
-								ISession $session,
94
-								IProvider $tokenProvider,
95
-								ISecureRandom $random,
96
-								IURLGenerator $urlGenerator,
97
-								ClientMapper $clientMapper,
98
-								AccessTokenMapper $accessTokenMapper,
99
-								ICrypto $crypto) {
100
-		parent::__construct($appName, $request);
101
-		$this->userSession = $userSession;
102
-		$this->l10n = $l10n;
103
-		$this->defaults = $defaults;
104
-		$this->session = $session;
105
-		$this->tokenProvider = $tokenProvider;
106
-		$this->random = $random;
107
-		$this->urlGenerator = $urlGenerator;
108
-		$this->clientMapper = $clientMapper;
109
-		$this->accessTokenMapper = $accessTokenMapper;
110
-		$this->crypto = $crypto;
111
-	}
112
-
113
-	/**
114
-	 * @return string
115
-	 */
116
-	private function getClientName() {
117
-		$userAgent = $this->request->getHeader('USER_AGENT');
118
-		return $userAgent !== null ? $userAgent : 'unknown';
119
-	}
120
-
121
-	/**
122
-	 * @param string $stateToken
123
-	 * @return bool
124
-	 */
125
-	private function isValidToken($stateToken) {
126
-		$currentToken = $this->session->get(self::stateName);
127
-		if(!is_string($stateToken) || !is_string($currentToken)) {
128
-			return false;
129
-		}
130
-		return hash_equals($currentToken, $stateToken);
131
-	}
132
-
133
-	/**
134
-	 * @return TemplateResponse
135
-	 */
136
-	private function stateTokenForbiddenResponse() {
137
-		$response = new TemplateResponse(
138
-			$this->appName,
139
-			'403',
140
-			[
141
-				'file' => $this->l10n->t('State token does not match'),
142
-			],
143
-			'guest'
144
-		);
145
-		$response->setStatus(Http::STATUS_FORBIDDEN);
146
-		return $response;
147
-	}
148
-
149
-	/**
150
-	 * @PublicPage
151
-	 * @NoCSRFRequired
152
-	 * @UseSession
153
-	 *
154
-	 * @param string $clientIdentifier
155
-	 *
156
-	 * @return TemplateResponse
157
-	 */
158
-	public function showAuthPickerPage($clientIdentifier = '') {
159
-		$clientName = $this->getClientName();
160
-		$client = null;
161
-		if($clientIdentifier !== '') {
162
-			$client = $this->clientMapper->getByIdentifier($clientIdentifier);
163
-			$clientName = $client->getName();
164
-		}
165
-
166
-		// No valid clientIdentifier given and no valid API Request (APIRequest header not set)
167
-		$clientRequest = $this->request->getHeader('OCS-APIREQUEST');
168
-		if ($clientRequest !== 'true' && $client === null) {
169
-			return new TemplateResponse(
170
-				$this->appName,
171
-				'error',
172
-				[
173
-					'errors' =>
174
-					[
175
-						[
176
-							'error' => 'Access Forbidden',
177
-							'hint' => 'Invalid request',
178
-						],
179
-					],
180
-				],
181
-				'guest'
182
-			);
183
-		}
184
-
185
-		$stateToken = $this->random->generate(
186
-			64,
187
-			ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
188
-		);
189
-		$this->session->set(self::stateName, $stateToken);
190
-
191
-		return new TemplateResponse(
192
-			$this->appName,
193
-			'loginflow/authpicker',
194
-			[
195
-				'client' => $clientName,
196
-				'clientIdentifier' => $clientIdentifier,
197
-				'instanceName' => $this->defaults->getName(),
198
-				'urlGenerator' => $this->urlGenerator,
199
-				'stateToken' => $stateToken,
200
-				'serverHost' => $this->request->getServerHost(),
201
-				'oauthState' => $this->session->get('oauth.state'),
202
-			],
203
-			'guest'
204
-		);
205
-	}
206
-
207
-	/**
208
-	 * @NoAdminRequired
209
-	 * @NoCSRFRequired
210
-	 * @NoSameSiteCookieRequired
211
-	 * @UseSession
212
-	 *
213
-	 * @param string $stateToken
214
-	 * @param string $clientIdentifier
215
-	 * @return TemplateResponse
216
-	 */
217
-	public function redirectPage($stateToken = '',
218
-								 $clientIdentifier = '') {
219
-		if(!$this->isValidToken($stateToken)) {
220
-			return $this->stateTokenForbiddenResponse();
221
-		}
222
-
223
-		return new TemplateResponse(
224
-			$this->appName,
225
-			'loginflow/redirect',
226
-			[
227
-				'urlGenerator' => $this->urlGenerator,
228
-				'stateToken' => $stateToken,
229
-				'clientIdentifier' => $clientIdentifier,
230
-				'oauthState' => $this->session->get('oauth.state'),
231
-			],
232
-			'guest'
233
-		);
234
-	}
235
-
236
-	/**
237
-	 * @NoAdminRequired
238
-	 * @UseSession
239
-	 *
240
-	 * @param string $stateToken
241
-	 * @param string $clientIdentifier
242
-	 * @return Http\RedirectResponse|Response
243
-	 */
244
-	public function generateAppPassword($stateToken,
245
-										$clientIdentifier = '') {
246
-		if(!$this->isValidToken($stateToken)) {
247
-			$this->session->remove(self::stateName);
248
-			return $this->stateTokenForbiddenResponse();
249
-		}
250
-
251
-		$this->session->remove(self::stateName);
252
-
253
-		try {
254
-			$sessionId = $this->session->getId();
255
-		} catch (SessionNotAvailableException $ex) {
256
-			$response = new Response();
257
-			$response->setStatus(Http::STATUS_FORBIDDEN);
258
-			return $response;
259
-		}
260
-
261
-		try {
262
-			$sessionToken = $this->tokenProvider->getToken($sessionId);
263
-			$loginName = $sessionToken->getLoginName();
264
-			try {
265
-				$password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
266
-			} catch (PasswordlessTokenException $ex) {
267
-				$password = null;
268
-			}
269
-		} catch (InvalidTokenException $ex) {
270
-			$response = new Response();
271
-			$response->setStatus(Http::STATUS_FORBIDDEN);
272
-			return $response;
273
-		}
274
-
275
-		$clientName = $this->getClientName();
276
-		$client = false;
277
-		if($clientIdentifier !== '') {
278
-			$client = $this->clientMapper->getByIdentifier($clientIdentifier);
279
-			$clientName = $client->getName();
280
-		}
281
-
282
-		$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
283
-		$uid = $this->userSession->getUser()->getUID();
284
-		$generatedToken = $this->tokenProvider->generateToken(
285
-			$token,
286
-			$uid,
287
-			$loginName,
288
-			$password,
289
-			$clientName,
290
-			IToken::PERMANENT_TOKEN,
291
-			IToken::DO_NOT_REMEMBER
292
-		);
293
-
294
-		if($client) {
295
-			$code = $this->random->generate(128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
296
-			$accessToken = new AccessToken();
297
-			$accessToken->setClientId($client->getId());
298
-			$accessToken->setEncryptedToken($this->crypto->encrypt($token, $code));
299
-			$accessToken->setHashedCode(hash('sha512', $code));
300
-			$accessToken->setTokenId($generatedToken->getId());
301
-			$this->accessTokenMapper->insert($accessToken);
302
-
303
-			$redirectUri = sprintf(
304
-				'%s?state=%s&code=%s',
305
-				$client->getRedirectUri(),
306
-				urlencode($this->session->get('oauth.state')),
307
-				urlencode($code)
308
-			);
309
-			$this->session->remove('oauth.state');
310
-		} else {
311
-			$serverPostfix = '';
312
-
313
-			if (strpos($this->request->getRequestUri(), '/index.php') !== false) {
314
-				$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/index.php'));
315
-			} else if (strpos($this->request->getRequestUri(), '/login/flow') !== false) {
316
-				$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/login/flow'));
317
-			}
318
-
319
-			$protocol = $this->request->getServerProtocol();
320
-
321
-			if ($protocol !== "https") {
322
-				$xForwardedProto = $this->request->getHeader('X-Forwarded-Proto');
323
-				$xForwardedSSL = $this->request->getHeader('X-Forwarded-Ssl');
324
-				if ($xForwardedProto === 'https' || $xForwardedSSL === 'on') {
325
-					$protocol = 'https';
326
-				}
327
-			}
328
-
329
-
330
-			$serverPath = $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
331
-			$redirectUri = 'nc://login/server:' . $serverPath . '&user:' . urlencode($loginName) . '&password:' . urlencode($token);
332
-		}
333
-
334
-		// Clear the token from the login here
335
-		$this->tokenProvider->invalidateToken($sessionId);
336
-
337
-		return new Http\RedirectResponse($redirectUri);
338
-	}
339
-
340
-	/**
341
-	 * @PublicPage
342
-	 */
343
-	public function apptokenRedirect(string $stateToken, string $user, string $password) {
344
-		if (!$this->isValidToken($stateToken)) {
345
-			return $this->stateTokenForbiddenResponse();
346
-		}
347
-
348
-		$redirectUri = 'nc://login/server:' . $this->getServerPath() . '&user:' . urlencode($user) . '&password:' . urlencode($password);
349
-		return new Http\RedirectResponse($redirectUri);
350
-	}
351
-
352
-	private function getServerPath() {
353
-		$serverPostfix = '';
354
-		if (strpos($this->request->getRequestUri(), '/index.php') !== false) {
355
-			$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/index.php'));
356
-		} else if (strpos($this->request->getRequestUri(), '/login/flow') !== false) {
357
-			$serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/login/flow'));
358
-		}
359
-		$protocol = $this->request->getServerProtocol();
360
-		if ($protocol !== "https") {
361
-			$xForwardedProto = $this->request->getHeader('X-Forwarded-Proto');
362
-			$xForwardedSSL = $this->request->getHeader('X-Forwarded-Ssl');
363
-			if ($xForwardedProto === 'https' || $xForwardedSSL === 'on') {
364
-				$protocol = 'https';
365
-			}
366
-		}
367
-		return $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
368
-	}
51
+    /** @var IUserSession */
52
+    private $userSession;
53
+    /** @var IL10N */
54
+    private $l10n;
55
+    /** @var Defaults */
56
+    private $defaults;
57
+    /** @var ISession */
58
+    private $session;
59
+    /** @var IProvider */
60
+    private $tokenProvider;
61
+    /** @var ISecureRandom */
62
+    private $random;
63
+    /** @var IURLGenerator */
64
+    private $urlGenerator;
65
+    /** @var ClientMapper */
66
+    private $clientMapper;
67
+    /** @var AccessTokenMapper */
68
+    private $accessTokenMapper;
69
+    /** @var ICrypto */
70
+    private $crypto;
71
+
72
+    const stateName = 'client.flow.state.token';
73
+
74
+    /**
75
+     * @param string $appName
76
+     * @param IRequest $request
77
+     * @param IUserSession $userSession
78
+     * @param IL10N $l10n
79
+     * @param Defaults $defaults
80
+     * @param ISession $session
81
+     * @param IProvider $tokenProvider
82
+     * @param ISecureRandom $random
83
+     * @param IURLGenerator $urlGenerator
84
+     * @param ClientMapper $clientMapper
85
+     * @param AccessTokenMapper $accessTokenMapper
86
+     * @param ICrypto $crypto
87
+     */
88
+    public function __construct($appName,
89
+                                IRequest $request,
90
+                                IUserSession $userSession,
91
+                                IL10N $l10n,
92
+                                Defaults $defaults,
93
+                                ISession $session,
94
+                                IProvider $tokenProvider,
95
+                                ISecureRandom $random,
96
+                                IURLGenerator $urlGenerator,
97
+                                ClientMapper $clientMapper,
98
+                                AccessTokenMapper $accessTokenMapper,
99
+                                ICrypto $crypto) {
100
+        parent::__construct($appName, $request);
101
+        $this->userSession = $userSession;
102
+        $this->l10n = $l10n;
103
+        $this->defaults = $defaults;
104
+        $this->session = $session;
105
+        $this->tokenProvider = $tokenProvider;
106
+        $this->random = $random;
107
+        $this->urlGenerator = $urlGenerator;
108
+        $this->clientMapper = $clientMapper;
109
+        $this->accessTokenMapper = $accessTokenMapper;
110
+        $this->crypto = $crypto;
111
+    }
112
+
113
+    /**
114
+     * @return string
115
+     */
116
+    private function getClientName() {
117
+        $userAgent = $this->request->getHeader('USER_AGENT');
118
+        return $userAgent !== null ? $userAgent : 'unknown';
119
+    }
120
+
121
+    /**
122
+     * @param string $stateToken
123
+     * @return bool
124
+     */
125
+    private function isValidToken($stateToken) {
126
+        $currentToken = $this->session->get(self::stateName);
127
+        if(!is_string($stateToken) || !is_string($currentToken)) {
128
+            return false;
129
+        }
130
+        return hash_equals($currentToken, $stateToken);
131
+    }
132
+
133
+    /**
134
+     * @return TemplateResponse
135
+     */
136
+    private function stateTokenForbiddenResponse() {
137
+        $response = new TemplateResponse(
138
+            $this->appName,
139
+            '403',
140
+            [
141
+                'file' => $this->l10n->t('State token does not match'),
142
+            ],
143
+            'guest'
144
+        );
145
+        $response->setStatus(Http::STATUS_FORBIDDEN);
146
+        return $response;
147
+    }
148
+
149
+    /**
150
+     * @PublicPage
151
+     * @NoCSRFRequired
152
+     * @UseSession
153
+     *
154
+     * @param string $clientIdentifier
155
+     *
156
+     * @return TemplateResponse
157
+     */
158
+    public function showAuthPickerPage($clientIdentifier = '') {
159
+        $clientName = $this->getClientName();
160
+        $client = null;
161
+        if($clientIdentifier !== '') {
162
+            $client = $this->clientMapper->getByIdentifier($clientIdentifier);
163
+            $clientName = $client->getName();
164
+        }
165
+
166
+        // No valid clientIdentifier given and no valid API Request (APIRequest header not set)
167
+        $clientRequest = $this->request->getHeader('OCS-APIREQUEST');
168
+        if ($clientRequest !== 'true' && $client === null) {
169
+            return new TemplateResponse(
170
+                $this->appName,
171
+                'error',
172
+                [
173
+                    'errors' =>
174
+                    [
175
+                        [
176
+                            'error' => 'Access Forbidden',
177
+                            'hint' => 'Invalid request',
178
+                        ],
179
+                    ],
180
+                ],
181
+                'guest'
182
+            );
183
+        }
184
+
185
+        $stateToken = $this->random->generate(
186
+            64,
187
+            ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS
188
+        );
189
+        $this->session->set(self::stateName, $stateToken);
190
+
191
+        return new TemplateResponse(
192
+            $this->appName,
193
+            'loginflow/authpicker',
194
+            [
195
+                'client' => $clientName,
196
+                'clientIdentifier' => $clientIdentifier,
197
+                'instanceName' => $this->defaults->getName(),
198
+                'urlGenerator' => $this->urlGenerator,
199
+                'stateToken' => $stateToken,
200
+                'serverHost' => $this->request->getServerHost(),
201
+                'oauthState' => $this->session->get('oauth.state'),
202
+            ],
203
+            'guest'
204
+        );
205
+    }
206
+
207
+    /**
208
+     * @NoAdminRequired
209
+     * @NoCSRFRequired
210
+     * @NoSameSiteCookieRequired
211
+     * @UseSession
212
+     *
213
+     * @param string $stateToken
214
+     * @param string $clientIdentifier
215
+     * @return TemplateResponse
216
+     */
217
+    public function redirectPage($stateToken = '',
218
+                                    $clientIdentifier = '') {
219
+        if(!$this->isValidToken($stateToken)) {
220
+            return $this->stateTokenForbiddenResponse();
221
+        }
222
+
223
+        return new TemplateResponse(
224
+            $this->appName,
225
+            'loginflow/redirect',
226
+            [
227
+                'urlGenerator' => $this->urlGenerator,
228
+                'stateToken' => $stateToken,
229
+                'clientIdentifier' => $clientIdentifier,
230
+                'oauthState' => $this->session->get('oauth.state'),
231
+            ],
232
+            'guest'
233
+        );
234
+    }
235
+
236
+    /**
237
+     * @NoAdminRequired
238
+     * @UseSession
239
+     *
240
+     * @param string $stateToken
241
+     * @param string $clientIdentifier
242
+     * @return Http\RedirectResponse|Response
243
+     */
244
+    public function generateAppPassword($stateToken,
245
+                                        $clientIdentifier = '') {
246
+        if(!$this->isValidToken($stateToken)) {
247
+            $this->session->remove(self::stateName);
248
+            return $this->stateTokenForbiddenResponse();
249
+        }
250
+
251
+        $this->session->remove(self::stateName);
252
+
253
+        try {
254
+            $sessionId = $this->session->getId();
255
+        } catch (SessionNotAvailableException $ex) {
256
+            $response = new Response();
257
+            $response->setStatus(Http::STATUS_FORBIDDEN);
258
+            return $response;
259
+        }
260
+
261
+        try {
262
+            $sessionToken = $this->tokenProvider->getToken($sessionId);
263
+            $loginName = $sessionToken->getLoginName();
264
+            try {
265
+                $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
266
+            } catch (PasswordlessTokenException $ex) {
267
+                $password = null;
268
+            }
269
+        } catch (InvalidTokenException $ex) {
270
+            $response = new Response();
271
+            $response->setStatus(Http::STATUS_FORBIDDEN);
272
+            return $response;
273
+        }
274
+
275
+        $clientName = $this->getClientName();
276
+        $client = false;
277
+        if($clientIdentifier !== '') {
278
+            $client = $this->clientMapper->getByIdentifier($clientIdentifier);
279
+            $clientName = $client->getName();
280
+        }
281
+
282
+        $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
283
+        $uid = $this->userSession->getUser()->getUID();
284
+        $generatedToken = $this->tokenProvider->generateToken(
285
+            $token,
286
+            $uid,
287
+            $loginName,
288
+            $password,
289
+            $clientName,
290
+            IToken::PERMANENT_TOKEN,
291
+            IToken::DO_NOT_REMEMBER
292
+        );
293
+
294
+        if($client) {
295
+            $code = $this->random->generate(128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
296
+            $accessToken = new AccessToken();
297
+            $accessToken->setClientId($client->getId());
298
+            $accessToken->setEncryptedToken($this->crypto->encrypt($token, $code));
299
+            $accessToken->setHashedCode(hash('sha512', $code));
300
+            $accessToken->setTokenId($generatedToken->getId());
301
+            $this->accessTokenMapper->insert($accessToken);
302
+
303
+            $redirectUri = sprintf(
304
+                '%s?state=%s&code=%s',
305
+                $client->getRedirectUri(),
306
+                urlencode($this->session->get('oauth.state')),
307
+                urlencode($code)
308
+            );
309
+            $this->session->remove('oauth.state');
310
+        } else {
311
+            $serverPostfix = '';
312
+
313
+            if (strpos($this->request->getRequestUri(), '/index.php') !== false) {
314
+                $serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/index.php'));
315
+            } else if (strpos($this->request->getRequestUri(), '/login/flow') !== false) {
316
+                $serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/login/flow'));
317
+            }
318
+
319
+            $protocol = $this->request->getServerProtocol();
320
+
321
+            if ($protocol !== "https") {
322
+                $xForwardedProto = $this->request->getHeader('X-Forwarded-Proto');
323
+                $xForwardedSSL = $this->request->getHeader('X-Forwarded-Ssl');
324
+                if ($xForwardedProto === 'https' || $xForwardedSSL === 'on') {
325
+                    $protocol = 'https';
326
+                }
327
+            }
328
+
329
+
330
+            $serverPath = $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
331
+            $redirectUri = 'nc://login/server:' . $serverPath . '&user:' . urlencode($loginName) . '&password:' . urlencode($token);
332
+        }
333
+
334
+        // Clear the token from the login here
335
+        $this->tokenProvider->invalidateToken($sessionId);
336
+
337
+        return new Http\RedirectResponse($redirectUri);
338
+    }
339
+
340
+    /**
341
+     * @PublicPage
342
+     */
343
+    public function apptokenRedirect(string $stateToken, string $user, string $password) {
344
+        if (!$this->isValidToken($stateToken)) {
345
+            return $this->stateTokenForbiddenResponse();
346
+        }
347
+
348
+        $redirectUri = 'nc://login/server:' . $this->getServerPath() . '&user:' . urlencode($user) . '&password:' . urlencode($password);
349
+        return new Http\RedirectResponse($redirectUri);
350
+    }
351
+
352
+    private function getServerPath() {
353
+        $serverPostfix = '';
354
+        if (strpos($this->request->getRequestUri(), '/index.php') !== false) {
355
+            $serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/index.php'));
356
+        } else if (strpos($this->request->getRequestUri(), '/login/flow') !== false) {
357
+            $serverPostfix = substr($this->request->getRequestUri(), 0, strpos($this->request->getRequestUri(), '/login/flow'));
358
+        }
359
+        $protocol = $this->request->getServerProtocol();
360
+        if ($protocol !== "https") {
361
+            $xForwardedProto = $this->request->getHeader('X-Forwarded-Proto');
362
+            $xForwardedSSL = $this->request->getHeader('X-Forwarded-Ssl');
363
+            if ($xForwardedProto === 'https' || $xForwardedSSL === 'on') {
364
+                $protocol = 'https';
365
+            }
366
+        }
367
+        return $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
368
+    }
369 369
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 	 */
125 125
 	private function isValidToken($stateToken) {
126 126
 		$currentToken = $this->session->get(self::stateName);
127
-		if(!is_string($stateToken) || !is_string($currentToken)) {
127
+		if (!is_string($stateToken) || !is_string($currentToken)) {
128 128
 			return false;
129 129
 		}
130 130
 		return hash_equals($currentToken, $stateToken);
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
 	public function showAuthPickerPage($clientIdentifier = '') {
159 159
 		$clientName = $this->getClientName();
160 160
 		$client = null;
161
-		if($clientIdentifier !== '') {
161
+		if ($clientIdentifier !== '') {
162 162
 			$client = $this->clientMapper->getByIdentifier($clientIdentifier);
163 163
 			$clientName = $client->getName();
164 164
 		}
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
 	 */
217 217
 	public function redirectPage($stateToken = '',
218 218
 								 $clientIdentifier = '') {
219
-		if(!$this->isValidToken($stateToken)) {
219
+		if (!$this->isValidToken($stateToken)) {
220 220
 			return $this->stateTokenForbiddenResponse();
221 221
 		}
222 222
 
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
 	 */
244 244
 	public function generateAppPassword($stateToken,
245 245
 										$clientIdentifier = '') {
246
-		if(!$this->isValidToken($stateToken)) {
246
+		if (!$this->isValidToken($stateToken)) {
247 247
 			$this->session->remove(self::stateName);
248 248
 			return $this->stateTokenForbiddenResponse();
249 249
 		}
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 
275 275
 		$clientName = $this->getClientName();
276 276
 		$client = false;
277
-		if($clientIdentifier !== '') {
277
+		if ($clientIdentifier !== '') {
278 278
 			$client = $this->clientMapper->getByIdentifier($clientIdentifier);
279 279
 			$clientName = $client->getName();
280 280
 		}
@@ -291,7 +291,7 @@  discard block
 block discarded – undo
291 291
 			IToken::DO_NOT_REMEMBER
292 292
 		);
293 293
 
294
-		if($client) {
294
+		if ($client) {
295 295
 			$code = $this->random->generate(128, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
296 296
 			$accessToken = new AccessToken();
297 297
 			$accessToken->setClientId($client->getId());
@@ -327,8 +327,8 @@  discard block
 block discarded – undo
327 327
 			}
328 328
 
329 329
 
330
-			$serverPath = $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
331
-			$redirectUri = 'nc://login/server:' . $serverPath . '&user:' . urlencode($loginName) . '&password:' . urlencode($token);
330
+			$serverPath = $protocol."://".$this->request->getServerHost().$serverPostfix;
331
+			$redirectUri = 'nc://login/server:'.$serverPath.'&user:'.urlencode($loginName).'&password:'.urlencode($token);
332 332
 		}
333 333
 
334 334
 		// Clear the token from the login here
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 			return $this->stateTokenForbiddenResponse();
346 346
 		}
347 347
 
348
-		$redirectUri = 'nc://login/server:' . $this->getServerPath() . '&user:' . urlencode($user) . '&password:' . urlencode($password);
348
+		$redirectUri = 'nc://login/server:'.$this->getServerPath().'&user:'.urlencode($user).'&password:'.urlencode($password);
349 349
 		return new Http\RedirectResponse($redirectUri);
350 350
 	}
351 351
 
@@ -364,6 +364,6 @@  discard block
 block discarded – undo
364 364
 				$protocol = 'https';
365 365
 			}
366 366
 		}
367
-		return $protocol . "://" . $this->request->getServerHost() . $serverPostfix;
367
+		return $protocol."://".$this->request->getServerHost().$serverPostfix;
368 368
 	}
369 369
 }
Please login to merge, or discard this patch.