@@ -30,59 +30,59 @@ |
||
| 30 | 30 | use OCP\Security\ISecureRandom; |
| 31 | 31 | |
| 32 | 32 | class OauthApiController extends Controller { |
| 33 | - /** @var AccessTokenMapper */ |
|
| 34 | - private $accessTokenMapper; |
|
| 35 | - /** @var ICrypto */ |
|
| 36 | - private $crypto; |
|
| 37 | - /** @var DefaultTokenMapper */ |
|
| 38 | - private $defaultTokenMapper; |
|
| 39 | - /** @var ISecureRandom */ |
|
| 40 | - private $secureRandom; |
|
| 33 | + /** @var AccessTokenMapper */ |
|
| 34 | + private $accessTokenMapper; |
|
| 35 | + /** @var ICrypto */ |
|
| 36 | + private $crypto; |
|
| 37 | + /** @var DefaultTokenMapper */ |
|
| 38 | + private $defaultTokenMapper; |
|
| 39 | + /** @var ISecureRandom */ |
|
| 40 | + private $secureRandom; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param string $appName |
|
| 44 | - * @param IRequest $request |
|
| 45 | - * @param ICrypto $crypto |
|
| 46 | - * @param AccessTokenMapper $accessTokenMapper |
|
| 47 | - * @param DefaultTokenMapper $defaultTokenMapper |
|
| 48 | - * @param ISecureRandom $secureRandom |
|
| 49 | - */ |
|
| 50 | - public function __construct($appName, |
|
| 51 | - IRequest $request, |
|
| 52 | - ICrypto $crypto, |
|
| 53 | - AccessTokenMapper $accessTokenMapper, |
|
| 54 | - DefaultTokenMapper $defaultTokenMapper, |
|
| 55 | - ISecureRandom $secureRandom) { |
|
| 56 | - parent::__construct($appName, $request); |
|
| 57 | - $this->crypto = $crypto; |
|
| 58 | - $this->accessTokenMapper = $accessTokenMapper; |
|
| 59 | - $this->defaultTokenMapper = $defaultTokenMapper; |
|
| 60 | - $this->secureRandom = $secureRandom; |
|
| 61 | - } |
|
| 42 | + /** |
|
| 43 | + * @param string $appName |
|
| 44 | + * @param IRequest $request |
|
| 45 | + * @param ICrypto $crypto |
|
| 46 | + * @param AccessTokenMapper $accessTokenMapper |
|
| 47 | + * @param DefaultTokenMapper $defaultTokenMapper |
|
| 48 | + * @param ISecureRandom $secureRandom |
|
| 49 | + */ |
|
| 50 | + public function __construct($appName, |
|
| 51 | + IRequest $request, |
|
| 52 | + ICrypto $crypto, |
|
| 53 | + AccessTokenMapper $accessTokenMapper, |
|
| 54 | + DefaultTokenMapper $defaultTokenMapper, |
|
| 55 | + ISecureRandom $secureRandom) { |
|
| 56 | + parent::__construct($appName, $request); |
|
| 57 | + $this->crypto = $crypto; |
|
| 58 | + $this->accessTokenMapper = $accessTokenMapper; |
|
| 59 | + $this->defaultTokenMapper = $defaultTokenMapper; |
|
| 60 | + $this->secureRandom = $secureRandom; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @PublicPage |
|
| 65 | - * @NoCSRFRequired |
|
| 66 | - * |
|
| 67 | - * @param string $code |
|
| 68 | - * @return JSONResponse |
|
| 69 | - */ |
|
| 70 | - public function getToken($code) { |
|
| 71 | - $accessToken = $this->accessTokenMapper->getByCode($code); |
|
| 72 | - $decryptedToken = $this->crypto->decrypt($accessToken->getEncryptedToken(), $code); |
|
| 73 | - $newCode = $this->secureRandom->generate(128); |
|
| 74 | - $accessToken->setHashedCode(hash('sha512', $newCode)); |
|
| 75 | - $accessToken->setEncryptedToken($this->crypto->encrypt($decryptedToken, $newCode)); |
|
| 76 | - $this->accessTokenMapper->update($accessToken); |
|
| 63 | + /** |
|
| 64 | + * @PublicPage |
|
| 65 | + * @NoCSRFRequired |
|
| 66 | + * |
|
| 67 | + * @param string $code |
|
| 68 | + * @return JSONResponse |
|
| 69 | + */ |
|
| 70 | + public function getToken($code) { |
|
| 71 | + $accessToken = $this->accessTokenMapper->getByCode($code); |
|
| 72 | + $decryptedToken = $this->crypto->decrypt($accessToken->getEncryptedToken(), $code); |
|
| 73 | + $newCode = $this->secureRandom->generate(128); |
|
| 74 | + $accessToken->setHashedCode(hash('sha512', $newCode)); |
|
| 75 | + $accessToken->setEncryptedToken($this->crypto->encrypt($decryptedToken, $newCode)); |
|
| 76 | + $this->accessTokenMapper->update($accessToken); |
|
| 77 | 77 | |
| 78 | - return new JSONResponse( |
|
| 79 | - [ |
|
| 80 | - 'access_token' => $decryptedToken, |
|
| 81 | - 'token_type' => 'Bearer', |
|
| 82 | - 'expires_in' => 3600, |
|
| 83 | - 'refresh_token' => $newCode, |
|
| 84 | - 'user_id' => $this->defaultTokenMapper->getTokenById($accessToken->getTokenId())->getUID(), |
|
| 85 | - ] |
|
| 86 | - ); |
|
| 87 | - } |
|
| 78 | + return new JSONResponse( |
|
| 79 | + [ |
|
| 80 | + 'access_token' => $decryptedToken, |
|
| 81 | + 'token_type' => 'Bearer', |
|
| 82 | + 'expires_in' => 3600, |
|
| 83 | + 'refresh_token' => $newCode, |
|
| 84 | + 'user_id' => $this->defaultTokenMapper->getTokenById($accessToken->getTokenId())->getUID(), |
|
| 85 | + ] |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -43,267 +43,267 @@ |
||
| 43 | 43 | use OCP\Session\Exceptions\SessionNotAvailableException; |
| 44 | 44 | |
| 45 | 45 | class ClientFlowLoginController extends Controller { |
| 46 | - /** @var IUserSession */ |
|
| 47 | - private $userSession; |
|
| 48 | - /** @var IL10N */ |
|
| 49 | - private $l10n; |
|
| 50 | - /** @var Defaults */ |
|
| 51 | - private $defaults; |
|
| 52 | - /** @var ISession */ |
|
| 53 | - private $session; |
|
| 54 | - /** @var IProvider */ |
|
| 55 | - private $tokenProvider; |
|
| 56 | - /** @var ISecureRandom */ |
|
| 57 | - private $random; |
|
| 58 | - /** @var IURLGenerator */ |
|
| 59 | - private $urlGenerator; |
|
| 60 | - /** @var ClientMapper */ |
|
| 61 | - private $clientMapper; |
|
| 62 | - /** @var AccessTokenMapper */ |
|
| 63 | - private $accessTokenMapper; |
|
| 64 | - /** @var ICrypto */ |
|
| 65 | - private $crypto; |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + private $userSession; |
|
| 48 | + /** @var IL10N */ |
|
| 49 | + private $l10n; |
|
| 50 | + /** @var Defaults */ |
|
| 51 | + private $defaults; |
|
| 52 | + /** @var ISession */ |
|
| 53 | + private $session; |
|
| 54 | + /** @var IProvider */ |
|
| 55 | + private $tokenProvider; |
|
| 56 | + /** @var ISecureRandom */ |
|
| 57 | + private $random; |
|
| 58 | + /** @var IURLGenerator */ |
|
| 59 | + private $urlGenerator; |
|
| 60 | + /** @var ClientMapper */ |
|
| 61 | + private $clientMapper; |
|
| 62 | + /** @var AccessTokenMapper */ |
|
| 63 | + private $accessTokenMapper; |
|
| 64 | + /** @var ICrypto */ |
|
| 65 | + private $crypto; |
|
| 66 | 66 | |
| 67 | - const stateName = 'client.flow.state.token'; |
|
| 67 | + const stateName = 'client.flow.state.token'; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @param string $appName |
|
| 71 | - * @param IRequest $request |
|
| 72 | - * @param IUserSession $userSession |
|
| 73 | - * @param IL10N $l10n |
|
| 74 | - * @param Defaults $defaults |
|
| 75 | - * @param ISession $session |
|
| 76 | - * @param IProvider $tokenProvider |
|
| 77 | - * @param ISecureRandom $random |
|
| 78 | - * @param IURLGenerator $urlGenerator |
|
| 79 | - * @param ClientMapper $clientMapper |
|
| 80 | - * @param AccessTokenMapper $accessTokenMapper |
|
| 81 | - * @param ICrypto $crypto |
|
| 82 | - */ |
|
| 83 | - public function __construct($appName, |
|
| 84 | - IRequest $request, |
|
| 85 | - IUserSession $userSession, |
|
| 86 | - IL10N $l10n, |
|
| 87 | - Defaults $defaults, |
|
| 88 | - ISession $session, |
|
| 89 | - IProvider $tokenProvider, |
|
| 90 | - ISecureRandom $random, |
|
| 91 | - IURLGenerator $urlGenerator, |
|
| 92 | - ClientMapper $clientMapper, |
|
| 93 | - AccessTokenMapper $accessTokenMapper, |
|
| 94 | - ICrypto $crypto) { |
|
| 95 | - parent::__construct($appName, $request); |
|
| 96 | - $this->userSession = $userSession; |
|
| 97 | - $this->l10n = $l10n; |
|
| 98 | - $this->defaults = $defaults; |
|
| 99 | - $this->session = $session; |
|
| 100 | - $this->tokenProvider = $tokenProvider; |
|
| 101 | - $this->random = $random; |
|
| 102 | - $this->urlGenerator = $urlGenerator; |
|
| 103 | - $this->clientMapper = $clientMapper; |
|
| 104 | - $this->accessTokenMapper = $accessTokenMapper; |
|
| 105 | - $this->crypto = $crypto; |
|
| 106 | - } |
|
| 69 | + /** |
|
| 70 | + * @param string $appName |
|
| 71 | + * @param IRequest $request |
|
| 72 | + * @param IUserSession $userSession |
|
| 73 | + * @param IL10N $l10n |
|
| 74 | + * @param Defaults $defaults |
|
| 75 | + * @param ISession $session |
|
| 76 | + * @param IProvider $tokenProvider |
|
| 77 | + * @param ISecureRandom $random |
|
| 78 | + * @param IURLGenerator $urlGenerator |
|
| 79 | + * @param ClientMapper $clientMapper |
|
| 80 | + * @param AccessTokenMapper $accessTokenMapper |
|
| 81 | + * @param ICrypto $crypto |
|
| 82 | + */ |
|
| 83 | + public function __construct($appName, |
|
| 84 | + IRequest $request, |
|
| 85 | + IUserSession $userSession, |
|
| 86 | + IL10N $l10n, |
|
| 87 | + Defaults $defaults, |
|
| 88 | + ISession $session, |
|
| 89 | + IProvider $tokenProvider, |
|
| 90 | + ISecureRandom $random, |
|
| 91 | + IURLGenerator $urlGenerator, |
|
| 92 | + ClientMapper $clientMapper, |
|
| 93 | + AccessTokenMapper $accessTokenMapper, |
|
| 94 | + ICrypto $crypto) { |
|
| 95 | + parent::__construct($appName, $request); |
|
| 96 | + $this->userSession = $userSession; |
|
| 97 | + $this->l10n = $l10n; |
|
| 98 | + $this->defaults = $defaults; |
|
| 99 | + $this->session = $session; |
|
| 100 | + $this->tokenProvider = $tokenProvider; |
|
| 101 | + $this->random = $random; |
|
| 102 | + $this->urlGenerator = $urlGenerator; |
|
| 103 | + $this->clientMapper = $clientMapper; |
|
| 104 | + $this->accessTokenMapper = $accessTokenMapper; |
|
| 105 | + $this->crypto = $crypto; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - private function getClientName() { |
|
| 112 | - $userAgent = $this->request->getHeader('USER_AGENT'); |
|
| 113 | - return $userAgent !== null ? $userAgent : 'unknown'; |
|
| 114 | - } |
|
| 108 | + /** |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + private function getClientName() { |
|
| 112 | + $userAgent = $this->request->getHeader('USER_AGENT'); |
|
| 113 | + return $userAgent !== null ? $userAgent : 'unknown'; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * @param string $stateToken |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - private function isValidToken($stateToken) { |
|
| 121 | - $currentToken = $this->session->get(self::stateName); |
|
| 122 | - if(!is_string($stateToken) || !is_string($currentToken)) { |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - return hash_equals($currentToken, $stateToken); |
|
| 126 | - } |
|
| 116 | + /** |
|
| 117 | + * @param string $stateToken |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + private function isValidToken($stateToken) { |
|
| 121 | + $currentToken = $this->session->get(self::stateName); |
|
| 122 | + if(!is_string($stateToken) || !is_string($currentToken)) { |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + return hash_equals($currentToken, $stateToken); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * @return TemplateResponse |
|
| 130 | - */ |
|
| 131 | - private function stateTokenForbiddenResponse() { |
|
| 132 | - $response = new TemplateResponse( |
|
| 133 | - $this->appName, |
|
| 134 | - '403', |
|
| 135 | - [ |
|
| 136 | - 'file' => $this->l10n->t('State token does not match'), |
|
| 137 | - ], |
|
| 138 | - 'guest' |
|
| 139 | - ); |
|
| 140 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 141 | - return $response; |
|
| 142 | - } |
|
| 128 | + /** |
|
| 129 | + * @return TemplateResponse |
|
| 130 | + */ |
|
| 131 | + private function stateTokenForbiddenResponse() { |
|
| 132 | + $response = new TemplateResponse( |
|
| 133 | + $this->appName, |
|
| 134 | + '403', |
|
| 135 | + [ |
|
| 136 | + 'file' => $this->l10n->t('State token does not match'), |
|
| 137 | + ], |
|
| 138 | + 'guest' |
|
| 139 | + ); |
|
| 140 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 141 | + return $response; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @PublicPage |
|
| 146 | - * @NoCSRFRequired |
|
| 147 | - * @UseSession |
|
| 148 | - * |
|
| 149 | - * @param string $clientIdentifier |
|
| 150 | - * |
|
| 151 | - * @return TemplateResponse |
|
| 152 | - */ |
|
| 153 | - public function showAuthPickerPage($clientIdentifier = '') { |
|
| 154 | - $clientName = $this->getClientName(); |
|
| 155 | - $client = null; |
|
| 156 | - if($clientIdentifier !== '') { |
|
| 157 | - $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
| 158 | - $clientName = $client->getName(); |
|
| 159 | - } |
|
| 144 | + /** |
|
| 145 | + * @PublicPage |
|
| 146 | + * @NoCSRFRequired |
|
| 147 | + * @UseSession |
|
| 148 | + * |
|
| 149 | + * @param string $clientIdentifier |
|
| 150 | + * |
|
| 151 | + * @return TemplateResponse |
|
| 152 | + */ |
|
| 153 | + public function showAuthPickerPage($clientIdentifier = '') { |
|
| 154 | + $clientName = $this->getClientName(); |
|
| 155 | + $client = null; |
|
| 156 | + if($clientIdentifier !== '') { |
|
| 157 | + $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
| 158 | + $clientName = $client->getName(); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - // No valid clientIdentifier given and no valid API Request (APIRequest header not set) |
|
| 162 | - $clientRequest = $this->request->getHeader('OCS-APIREQUEST'); |
|
| 163 | - if ($clientRequest !== 'true' && $client === null) { |
|
| 164 | - return new TemplateResponse( |
|
| 165 | - $this->appName, |
|
| 166 | - 'error', |
|
| 167 | - [ |
|
| 168 | - 'errors' => |
|
| 169 | - [ |
|
| 170 | - [ |
|
| 171 | - 'error' => 'Access Forbidden', |
|
| 172 | - 'hint' => 'Invalid request', |
|
| 173 | - ], |
|
| 174 | - ], |
|
| 175 | - ], |
|
| 176 | - 'guest' |
|
| 177 | - ); |
|
| 178 | - } |
|
| 161 | + // No valid clientIdentifier given and no valid API Request (APIRequest header not set) |
|
| 162 | + $clientRequest = $this->request->getHeader('OCS-APIREQUEST'); |
|
| 163 | + if ($clientRequest !== 'true' && $client === null) { |
|
| 164 | + return new TemplateResponse( |
|
| 165 | + $this->appName, |
|
| 166 | + 'error', |
|
| 167 | + [ |
|
| 168 | + 'errors' => |
|
| 169 | + [ |
|
| 170 | + [ |
|
| 171 | + 'error' => 'Access Forbidden', |
|
| 172 | + 'hint' => 'Invalid request', |
|
| 173 | + ], |
|
| 174 | + ], |
|
| 175 | + ], |
|
| 176 | + 'guest' |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - $stateToken = $this->random->generate( |
|
| 181 | - 64, |
|
| 182 | - ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS |
|
| 183 | - ); |
|
| 184 | - $this->session->set(self::stateName, $stateToken); |
|
| 180 | + $stateToken = $this->random->generate( |
|
| 181 | + 64, |
|
| 182 | + ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS |
|
| 183 | + ); |
|
| 184 | + $this->session->set(self::stateName, $stateToken); |
|
| 185 | 185 | |
| 186 | - return new TemplateResponse( |
|
| 187 | - $this->appName, |
|
| 188 | - 'loginflow/authpicker', |
|
| 189 | - [ |
|
| 190 | - 'client' => $clientName, |
|
| 191 | - 'clientIdentifier' => $clientIdentifier, |
|
| 192 | - 'instanceName' => $this->defaults->getName(), |
|
| 193 | - 'urlGenerator' => $this->urlGenerator, |
|
| 194 | - 'stateToken' => $stateToken, |
|
| 195 | - 'serverHost' => $this->request->getServerHost(), |
|
| 196 | - ], |
|
| 197 | - 'guest' |
|
| 198 | - ); |
|
| 199 | - } |
|
| 186 | + return new TemplateResponse( |
|
| 187 | + $this->appName, |
|
| 188 | + 'loginflow/authpicker', |
|
| 189 | + [ |
|
| 190 | + 'client' => $clientName, |
|
| 191 | + 'clientIdentifier' => $clientIdentifier, |
|
| 192 | + 'instanceName' => $this->defaults->getName(), |
|
| 193 | + 'urlGenerator' => $this->urlGenerator, |
|
| 194 | + 'stateToken' => $stateToken, |
|
| 195 | + 'serverHost' => $this->request->getServerHost(), |
|
| 196 | + ], |
|
| 197 | + 'guest' |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * @NoAdminRequired |
|
| 203 | - * @NoCSRFRequired |
|
| 204 | - * @UseSession |
|
| 205 | - * |
|
| 206 | - * @param string $stateToken |
|
| 207 | - * @param string $clientIdentifier |
|
| 208 | - * @return TemplateResponse |
|
| 209 | - */ |
|
| 210 | - public function redirectPage($stateToken = '', |
|
| 211 | - $clientIdentifier = '') { |
|
| 212 | - if(!$this->isValidToken($stateToken)) { |
|
| 213 | - return $this->stateTokenForbiddenResponse(); |
|
| 214 | - } |
|
| 201 | + /** |
|
| 202 | + * @NoAdminRequired |
|
| 203 | + * @NoCSRFRequired |
|
| 204 | + * @UseSession |
|
| 205 | + * |
|
| 206 | + * @param string $stateToken |
|
| 207 | + * @param string $clientIdentifier |
|
| 208 | + * @return TemplateResponse |
|
| 209 | + */ |
|
| 210 | + public function redirectPage($stateToken = '', |
|
| 211 | + $clientIdentifier = '') { |
|
| 212 | + if(!$this->isValidToken($stateToken)) { |
|
| 213 | + return $this->stateTokenForbiddenResponse(); |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - return new TemplateResponse( |
|
| 217 | - $this->appName, |
|
| 218 | - 'loginflow/redirect', |
|
| 219 | - [ |
|
| 220 | - 'urlGenerator' => $this->urlGenerator, |
|
| 221 | - 'stateToken' => $stateToken, |
|
| 222 | - 'clientIdentifier' => $clientIdentifier, |
|
| 223 | - 'oauthState' => $this->session->get('oauth.state'), |
|
| 224 | - ], |
|
| 225 | - 'empty' |
|
| 226 | - ); |
|
| 227 | - } |
|
| 216 | + return new TemplateResponse( |
|
| 217 | + $this->appName, |
|
| 218 | + 'loginflow/redirect', |
|
| 219 | + [ |
|
| 220 | + 'urlGenerator' => $this->urlGenerator, |
|
| 221 | + 'stateToken' => $stateToken, |
|
| 222 | + 'clientIdentifier' => $clientIdentifier, |
|
| 223 | + 'oauthState' => $this->session->get('oauth.state'), |
|
| 224 | + ], |
|
| 225 | + 'empty' |
|
| 226 | + ); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * @NoAdminRequired |
|
| 231 | - * @UseSession |
|
| 232 | - * |
|
| 233 | - * @param string $stateToken |
|
| 234 | - * @param string $clientIdentifier |
|
| 235 | - * @return Http\RedirectResponse|Response |
|
| 236 | - */ |
|
| 237 | - public function generateAppPassword($stateToken, |
|
| 238 | - $clientIdentifier = '') { |
|
| 239 | - if(!$this->isValidToken($stateToken)) { |
|
| 240 | - $this->session->remove(self::stateName); |
|
| 241 | - return $this->stateTokenForbiddenResponse(); |
|
| 242 | - } |
|
| 229 | + /** |
|
| 230 | + * @NoAdminRequired |
|
| 231 | + * @UseSession |
|
| 232 | + * |
|
| 233 | + * @param string $stateToken |
|
| 234 | + * @param string $clientIdentifier |
|
| 235 | + * @return Http\RedirectResponse|Response |
|
| 236 | + */ |
|
| 237 | + public function generateAppPassword($stateToken, |
|
| 238 | + $clientIdentifier = '') { |
|
| 239 | + if(!$this->isValidToken($stateToken)) { |
|
| 240 | + $this->session->remove(self::stateName); |
|
| 241 | + return $this->stateTokenForbiddenResponse(); |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - $this->session->remove(self::stateName); |
|
| 244 | + $this->session->remove(self::stateName); |
|
| 245 | 245 | |
| 246 | - try { |
|
| 247 | - $sessionId = $this->session->getId(); |
|
| 248 | - } catch (SessionNotAvailableException $ex) { |
|
| 249 | - $response = new Response(); |
|
| 250 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 251 | - return $response; |
|
| 252 | - } |
|
| 246 | + try { |
|
| 247 | + $sessionId = $this->session->getId(); |
|
| 248 | + } catch (SessionNotAvailableException $ex) { |
|
| 249 | + $response = new Response(); |
|
| 250 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 251 | + return $response; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - try { |
|
| 255 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
| 256 | - $loginName = $sessionToken->getLoginName(); |
|
| 257 | - try { |
|
| 258 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
| 259 | - } catch (PasswordlessTokenException $ex) { |
|
| 260 | - $password = null; |
|
| 261 | - } |
|
| 262 | - } catch (InvalidTokenException $ex) { |
|
| 263 | - $response = new Response(); |
|
| 264 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 265 | - return $response; |
|
| 266 | - } |
|
| 254 | + try { |
|
| 255 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
| 256 | + $loginName = $sessionToken->getLoginName(); |
|
| 257 | + try { |
|
| 258 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
| 259 | + } catch (PasswordlessTokenException $ex) { |
|
| 260 | + $password = null; |
|
| 261 | + } |
|
| 262 | + } catch (InvalidTokenException $ex) { |
|
| 263 | + $response = new Response(); |
|
| 264 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 265 | + return $response; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - $clientName = $this->getClientName(); |
|
| 269 | - $client = false; |
|
| 270 | - if($clientIdentifier !== '') { |
|
| 271 | - $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
| 272 | - $clientName = $client->getName(); |
|
| 273 | - } |
|
| 268 | + $clientName = $this->getClientName(); |
|
| 269 | + $client = false; |
|
| 270 | + if($clientIdentifier !== '') { |
|
| 271 | + $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
|
| 272 | + $clientName = $client->getName(); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
| 276 | - $uid = $this->userSession->getUser()->getUID(); |
|
| 277 | - $generatedToken = $this->tokenProvider->generateToken( |
|
| 278 | - $token, |
|
| 279 | - $uid, |
|
| 280 | - $loginName, |
|
| 281 | - $password, |
|
| 282 | - $clientName, |
|
| 283 | - IToken::PERMANENT_TOKEN, |
|
| 284 | - IToken::DO_NOT_REMEMBER |
|
| 285 | - ); |
|
| 275 | + $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
| 276 | + $uid = $this->userSession->getUser()->getUID(); |
|
| 277 | + $generatedToken = $this->tokenProvider->generateToken( |
|
| 278 | + $token, |
|
| 279 | + $uid, |
|
| 280 | + $loginName, |
|
| 281 | + $password, |
|
| 282 | + $clientName, |
|
| 283 | + IToken::PERMANENT_TOKEN, |
|
| 284 | + IToken::DO_NOT_REMEMBER |
|
| 285 | + ); |
|
| 286 | 286 | |
| 287 | - if($client) { |
|
| 288 | - $code = $this->random->generate(128); |
|
| 289 | - $accessToken = new AccessToken(); |
|
| 290 | - $accessToken->setClientId($client->getId()); |
|
| 291 | - $accessToken->setEncryptedToken($this->crypto->encrypt($token, $code)); |
|
| 292 | - $accessToken->setHashedCode(hash('sha512', $code)); |
|
| 293 | - $accessToken->setTokenId($generatedToken->getId()); |
|
| 294 | - $this->accessTokenMapper->insert($accessToken); |
|
| 287 | + if($client) { |
|
| 288 | + $code = $this->random->generate(128); |
|
| 289 | + $accessToken = new AccessToken(); |
|
| 290 | + $accessToken->setClientId($client->getId()); |
|
| 291 | + $accessToken->setEncryptedToken($this->crypto->encrypt($token, $code)); |
|
| 292 | + $accessToken->setHashedCode(hash('sha512', $code)); |
|
| 293 | + $accessToken->setTokenId($generatedToken->getId()); |
|
| 294 | + $this->accessTokenMapper->insert($accessToken); |
|
| 295 | 295 | |
| 296 | - $redirectUri = sprintf( |
|
| 297 | - '%s?state=%s&code=%s', |
|
| 298 | - $client->getRedirectUri(), |
|
| 299 | - urlencode($this->session->get('oauth.state')), |
|
| 300 | - urlencode($code) |
|
| 301 | - ); |
|
| 302 | - $this->session->remove('oauth.state'); |
|
| 303 | - } else { |
|
| 304 | - $redirectUri = 'nc://login/server:' . $this->request->getServerHost() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token); |
|
| 305 | - } |
|
| 296 | + $redirectUri = sprintf( |
|
| 297 | + '%s?state=%s&code=%s', |
|
| 298 | + $client->getRedirectUri(), |
|
| 299 | + urlencode($this->session->get('oauth.state')), |
|
| 300 | + urlencode($code) |
|
| 301 | + ); |
|
| 302 | + $this->session->remove('oauth.state'); |
|
| 303 | + } else { |
|
| 304 | + $redirectUri = 'nc://login/server:' . $this->request->getServerHost() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token); |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | - return new Http\RedirectResponse($redirectUri); |
|
| 308 | - } |
|
| 307 | + return new Http\RedirectResponse($redirectUri); |
|
| 308 | + } |
|
| 309 | 309 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | private function isValidToken($stateToken) { |
| 121 | 121 | $currentToken = $this->session->get(self::stateName); |
| 122 | - if(!is_string($stateToken) || !is_string($currentToken)) { |
|
| 122 | + if (!is_string($stateToken) || !is_string($currentToken)) { |
|
| 123 | 123 | return false; |
| 124 | 124 | } |
| 125 | 125 | return hash_equals($currentToken, $stateToken); |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | public function showAuthPickerPage($clientIdentifier = '') { |
| 154 | 154 | $clientName = $this->getClientName(); |
| 155 | 155 | $client = null; |
| 156 | - if($clientIdentifier !== '') { |
|
| 156 | + if ($clientIdentifier !== '') { |
|
| 157 | 157 | $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
| 158 | 158 | $clientName = $client->getName(); |
| 159 | 159 | } |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | public function redirectPage($stateToken = '', |
| 211 | 211 | $clientIdentifier = '') { |
| 212 | - if(!$this->isValidToken($stateToken)) { |
|
| 212 | + if (!$this->isValidToken($stateToken)) { |
|
| 213 | 213 | return $this->stateTokenForbiddenResponse(); |
| 214 | 214 | } |
| 215 | 215 | |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | public function generateAppPassword($stateToken, |
| 238 | 238 | $clientIdentifier = '') { |
| 239 | - if(!$this->isValidToken($stateToken)) { |
|
| 239 | + if (!$this->isValidToken($stateToken)) { |
|
| 240 | 240 | $this->session->remove(self::stateName); |
| 241 | 241 | return $this->stateTokenForbiddenResponse(); |
| 242 | 242 | } |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | |
| 268 | 268 | $clientName = $this->getClientName(); |
| 269 | 269 | $client = false; |
| 270 | - if($clientIdentifier !== '') { |
|
| 270 | + if ($clientIdentifier !== '') { |
|
| 271 | 271 | $client = $this->clientMapper->getByIdentifier($clientIdentifier); |
| 272 | 272 | $clientName = $client->getName(); |
| 273 | 273 | } |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | IToken::DO_NOT_REMEMBER |
| 285 | 285 | ); |
| 286 | 286 | |
| 287 | - if($client) { |
|
| 287 | + if ($client) { |
|
| 288 | 288 | $code = $this->random->generate(128); |
| 289 | 289 | $accessToken = new AccessToken(); |
| 290 | 290 | $accessToken->setClientId($client->getId()); |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | ); |
| 302 | 302 | $this->session->remove('oauth.state'); |
| 303 | 303 | } else { |
| 304 | - $redirectUri = 'nc://login/server:' . $this->request->getServerHost() . '&user:' . urlencode($loginName) . '&password:' . urlencode($token); |
|
| 304 | + $redirectUri = 'nc://login/server:'.$this->request->getServerHost().'&user:'.urlencode($loginName).'&password:'.urlencode($token); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | return new Http\RedirectResponse($redirectUri); |
@@ -30,135 +30,135 @@ |
||
| 30 | 30 | |
| 31 | 31 | class DefaultTokenMapper extends Mapper { |
| 32 | 32 | |
| 33 | - public function __construct(IDBConnection $db) { |
|
| 34 | - parent::__construct($db, 'authtoken'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Invalidate (delete) a given token |
|
| 39 | - * |
|
| 40 | - * @param string $token |
|
| 41 | - */ |
|
| 42 | - public function invalidate($token) { |
|
| 43 | - /* @var $qb IQueryBuilder */ |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $qb->delete('authtoken') |
|
| 46 | - ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 47 | - ->setParameter('token', $token) |
|
| 48 | - ->execute(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param int $olderThan |
|
| 53 | - * @param int $remember |
|
| 54 | - */ |
|
| 55 | - public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 56 | - /* @var $qb IQueryBuilder */ |
|
| 57 | - $qb = $this->db->getQueryBuilder(); |
|
| 58 | - $qb->delete('authtoken') |
|
| 59 | - ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 60 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
| 61 | - ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
| 62 | - ->execute(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the user UID for the given token |
|
| 67 | - * |
|
| 68 | - * @param string $token |
|
| 69 | - * @throws DoesNotExistException |
|
| 70 | - * @return DefaultToken |
|
| 71 | - */ |
|
| 72 | - public function getToken($token) { |
|
| 73 | - /* @var $qb IQueryBuilder */ |
|
| 74 | - $qb = $this->db->getQueryBuilder(); |
|
| 75 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 76 | - ->from('authtoken') |
|
| 77 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 78 | - ->execute(); |
|
| 79 | - |
|
| 80 | - $data = $result->fetch(); |
|
| 81 | - $result->closeCursor(); |
|
| 82 | - if ($data === false) { |
|
| 83 | - throw new DoesNotExistException('token does not exist'); |
|
| 84 | - } |
|
| 33 | + public function __construct(IDBConnection $db) { |
|
| 34 | + parent::__construct($db, 'authtoken'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Invalidate (delete) a given token |
|
| 39 | + * |
|
| 40 | + * @param string $token |
|
| 41 | + */ |
|
| 42 | + public function invalidate($token) { |
|
| 43 | + /* @var $qb IQueryBuilder */ |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $qb->delete('authtoken') |
|
| 46 | + ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 47 | + ->setParameter('token', $token) |
|
| 48 | + ->execute(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param int $olderThan |
|
| 53 | + * @param int $remember |
|
| 54 | + */ |
|
| 55 | + public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 56 | + /* @var $qb IQueryBuilder */ |
|
| 57 | + $qb = $this->db->getQueryBuilder(); |
|
| 58 | + $qb->delete('authtoken') |
|
| 59 | + ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 60 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
| 61 | + ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
| 62 | + ->execute(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the user UID for the given token |
|
| 67 | + * |
|
| 68 | + * @param string $token |
|
| 69 | + * @throws DoesNotExistException |
|
| 70 | + * @return DefaultToken |
|
| 71 | + */ |
|
| 72 | + public function getToken($token) { |
|
| 73 | + /* @var $qb IQueryBuilder */ |
|
| 74 | + $qb = $this->db->getQueryBuilder(); |
|
| 75 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 76 | + ->from('authtoken') |
|
| 77 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 78 | + ->execute(); |
|
| 79 | + |
|
| 80 | + $data = $result->fetch(); |
|
| 81 | + $result->closeCursor(); |
|
| 82 | + if ($data === false) { |
|
| 83 | + throw new DoesNotExistException('token does not exist'); |
|
| 84 | + } |
|
| 85 | 85 | ; |
| 86 | - return DefaultToken::fromRow($data); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get the token for $id |
|
| 91 | - * |
|
| 92 | - * @param string $id |
|
| 93 | - * @throws DoesNotExistException |
|
| 94 | - * @return DefaultToken |
|
| 95 | - */ |
|
| 96 | - public function getTokenById($id) { |
|
| 97 | - /* @var $qb IQueryBuilder */ |
|
| 98 | - $qb = $this->db->getQueryBuilder(); |
|
| 99 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope') |
|
| 100 | - ->from('authtoken') |
|
| 101 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 102 | - ->execute(); |
|
| 103 | - |
|
| 104 | - $data = $result->fetch(); |
|
| 105 | - $result->closeCursor(); |
|
| 106 | - if ($data === false) { |
|
| 107 | - throw new DoesNotExistException('token does not exist'); |
|
| 108 | - }; |
|
| 109 | - return DefaultToken::fromRow($data); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get all token of a user |
|
| 114 | - * |
|
| 115 | - * The provider may limit the number of result rows in case of an abuse |
|
| 116 | - * where a high number of (session) tokens is generated |
|
| 117 | - * |
|
| 118 | - * @param IUser $user |
|
| 119 | - * @return DefaultToken[] |
|
| 120 | - */ |
|
| 121 | - public function getTokenByUser(IUser $user) { |
|
| 122 | - /* @var $qb IQueryBuilder */ |
|
| 123 | - $qb = $this->db->getQueryBuilder(); |
|
| 124 | - $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 125 | - ->from('authtoken') |
|
| 126 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 127 | - ->setMaxResults(1000); |
|
| 128 | - $result = $qb->execute(); |
|
| 129 | - $data = $result->fetchAll(); |
|
| 130 | - $result->closeCursor(); |
|
| 131 | - |
|
| 132 | - $entities = array_map(function ($row) { |
|
| 133 | - return DefaultToken::fromRow($row); |
|
| 134 | - }, $data); |
|
| 135 | - |
|
| 136 | - return $entities; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param IUser $user |
|
| 141 | - * @param int $id |
|
| 142 | - */ |
|
| 143 | - public function deleteById(IUser $user, $id) { |
|
| 144 | - /* @var $qb IQueryBuilder */ |
|
| 145 | - $qb = $this->db->getQueryBuilder(); |
|
| 146 | - $qb->delete('authtoken') |
|
| 147 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 148 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 149 | - $qb->execute(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * delete all auth token which belong to a specific client if the client was deleted |
|
| 154 | - * |
|
| 155 | - * @param string $name |
|
| 156 | - */ |
|
| 157 | - public function deleteByName($name) { |
|
| 158 | - $qb = $this->db->getQueryBuilder(); |
|
| 159 | - $qb->delete('authtoken') |
|
| 160 | - ->where($qb->expr()->eq('name', $qb->createNamedParameter($name))); |
|
| 161 | - $qb->execute(); |
|
| 162 | - } |
|
| 86 | + return DefaultToken::fromRow($data); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get the token for $id |
|
| 91 | + * |
|
| 92 | + * @param string $id |
|
| 93 | + * @throws DoesNotExistException |
|
| 94 | + * @return DefaultToken |
|
| 95 | + */ |
|
| 96 | + public function getTokenById($id) { |
|
| 97 | + /* @var $qb IQueryBuilder */ |
|
| 98 | + $qb = $this->db->getQueryBuilder(); |
|
| 99 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope') |
|
| 100 | + ->from('authtoken') |
|
| 101 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 102 | + ->execute(); |
|
| 103 | + |
|
| 104 | + $data = $result->fetch(); |
|
| 105 | + $result->closeCursor(); |
|
| 106 | + if ($data === false) { |
|
| 107 | + throw new DoesNotExistException('token does not exist'); |
|
| 108 | + }; |
|
| 109 | + return DefaultToken::fromRow($data); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get all token of a user |
|
| 114 | + * |
|
| 115 | + * The provider may limit the number of result rows in case of an abuse |
|
| 116 | + * where a high number of (session) tokens is generated |
|
| 117 | + * |
|
| 118 | + * @param IUser $user |
|
| 119 | + * @return DefaultToken[] |
|
| 120 | + */ |
|
| 121 | + public function getTokenByUser(IUser $user) { |
|
| 122 | + /* @var $qb IQueryBuilder */ |
|
| 123 | + $qb = $this->db->getQueryBuilder(); |
|
| 124 | + $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 125 | + ->from('authtoken') |
|
| 126 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 127 | + ->setMaxResults(1000); |
|
| 128 | + $result = $qb->execute(); |
|
| 129 | + $data = $result->fetchAll(); |
|
| 130 | + $result->closeCursor(); |
|
| 131 | + |
|
| 132 | + $entities = array_map(function ($row) { |
|
| 133 | + return DefaultToken::fromRow($row); |
|
| 134 | + }, $data); |
|
| 135 | + |
|
| 136 | + return $entities; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param IUser $user |
|
| 141 | + * @param int $id |
|
| 142 | + */ |
|
| 143 | + public function deleteById(IUser $user, $id) { |
|
| 144 | + /* @var $qb IQueryBuilder */ |
|
| 145 | + $qb = $this->db->getQueryBuilder(); |
|
| 146 | + $qb->delete('authtoken') |
|
| 147 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 148 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 149 | + $qb->execute(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * delete all auth token which belong to a specific client if the client was deleted |
|
| 154 | + * |
|
| 155 | + * @param string $name |
|
| 156 | + */ |
|
| 157 | + public function deleteByName($name) { |
|
| 158 | + $qb = $this->db->getQueryBuilder(); |
|
| 159 | + $qb->delete('authtoken') |
|
| 160 | + ->where($qb->expr()->eq('name', $qb->createNamedParameter($name))); |
|
| 161 | + $qb->execute(); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | 164 | } |
@@ -28,62 +28,62 @@ |
||
| 28 | 28 | |
| 29 | 29 | class ClientMapper extends Mapper { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param IDBConnection $db |
|
| 33 | - */ |
|
| 34 | - public function __construct(IDBConnection $db) { |
|
| 35 | - parent::__construct($db, 'oauth2_clients'); |
|
| 36 | - } |
|
| 31 | + /** |
|
| 32 | + * @param IDBConnection $db |
|
| 33 | + */ |
|
| 34 | + public function __construct(IDBConnection $db) { |
|
| 35 | + parent::__construct($db, 'oauth2_clients'); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param string $clientIdentifier |
|
| 40 | - * @return Client |
|
| 41 | - * @throws ClientNotFoundException |
|
| 42 | - */ |
|
| 43 | - public function getByIdentifier($clientIdentifier) { |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $qb |
|
| 46 | - ->select('*') |
|
| 47 | - ->from($this->tableName) |
|
| 48 | - ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); |
|
| 49 | - $result = $qb->execute(); |
|
| 50 | - $row = $result->fetch(); |
|
| 51 | - $result->closeCursor(); |
|
| 52 | - if($row === false) { |
|
| 53 | - throw new ClientNotFoundException(); |
|
| 54 | - } |
|
| 55 | - return Client::fromRow($row); |
|
| 56 | - } |
|
| 38 | + /** |
|
| 39 | + * @param string $clientIdentifier |
|
| 40 | + * @return Client |
|
| 41 | + * @throws ClientNotFoundException |
|
| 42 | + */ |
|
| 43 | + public function getByIdentifier($clientIdentifier) { |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $qb |
|
| 46 | + ->select('*') |
|
| 47 | + ->from($this->tableName) |
|
| 48 | + ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); |
|
| 49 | + $result = $qb->execute(); |
|
| 50 | + $row = $result->fetch(); |
|
| 51 | + $result->closeCursor(); |
|
| 52 | + if($row === false) { |
|
| 53 | + throw new ClientNotFoundException(); |
|
| 54 | + } |
|
| 55 | + return Client::fromRow($row); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param string $uid internal uid of the client |
|
| 60 | - * @return Client |
|
| 61 | - * @throws ClientNotFoundException |
|
| 62 | - */ |
|
| 63 | - public function getByUid($uid) { |
|
| 64 | - $qb = $this->db->getQueryBuilder(); |
|
| 65 | - $qb |
|
| 66 | - ->select('*') |
|
| 67 | - ->from($this->tableName) |
|
| 68 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT))); |
|
| 69 | - $result = $qb->execute(); |
|
| 70 | - $row = $result->fetch(); |
|
| 71 | - $result->closeCursor(); |
|
| 72 | - if($row === false) { |
|
| 73 | - throw new ClientNotFoundException(); |
|
| 74 | - } |
|
| 75 | - return Client::fromRow($row); |
|
| 76 | - } |
|
| 58 | + /** |
|
| 59 | + * @param string $uid internal uid of the client |
|
| 60 | + * @return Client |
|
| 61 | + * @throws ClientNotFoundException |
|
| 62 | + */ |
|
| 63 | + public function getByUid($uid) { |
|
| 64 | + $qb = $this->db->getQueryBuilder(); |
|
| 65 | + $qb |
|
| 66 | + ->select('*') |
|
| 67 | + ->from($this->tableName) |
|
| 68 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT))); |
|
| 69 | + $result = $qb->execute(); |
|
| 70 | + $row = $result->fetch(); |
|
| 71 | + $result->closeCursor(); |
|
| 72 | + if($row === false) { |
|
| 73 | + throw new ClientNotFoundException(); |
|
| 74 | + } |
|
| 75 | + return Client::fromRow($row); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @return Client[] |
|
| 80 | - */ |
|
| 81 | - public function getClients() { |
|
| 82 | - $qb = $this->db->getQueryBuilder(); |
|
| 83 | - $qb |
|
| 84 | - ->select('*') |
|
| 85 | - ->from($this->tableName); |
|
| 78 | + /** |
|
| 79 | + * @return Client[] |
|
| 80 | + */ |
|
| 81 | + public function getClients() { |
|
| 82 | + $qb = $this->db->getQueryBuilder(); |
|
| 83 | + $qb |
|
| 84 | + ->select('*') |
|
| 85 | + ->from($this->tableName); |
|
| 86 | 86 | |
| 87 | - return $this->findEntities($qb->getSQL()); |
|
| 88 | - } |
|
| 87 | + return $this->findEntities($qb->getSQL()); |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | $result = $qb->execute(); |
| 50 | 50 | $row = $result->fetch(); |
| 51 | 51 | $result->closeCursor(); |
| 52 | - if($row === false) { |
|
| 52 | + if ($row === false) { |
|
| 53 | 53 | throw new ClientNotFoundException(); |
| 54 | 54 | } |
| 55 | 55 | return Client::fromRow($row); |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | $result = $qb->execute(); |
| 70 | 70 | $row = $result->fetch(); |
| 71 | 71 | $result->closeCursor(); |
| 72 | - if($row === false) { |
|
| 72 | + if ($row === false) { |
|
| 73 | 73 | throw new ClientNotFoundException(); |
| 74 | 74 | } |
| 75 | 75 | return Client::fromRow($row); |
@@ -28,43 +28,43 @@ |
||
| 28 | 28 | |
| 29 | 29 | class AccessTokenMapper extends Mapper { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param IDBConnection $db |
|
| 33 | - */ |
|
| 34 | - public function __construct(IDBConnection $db) { |
|
| 35 | - parent::__construct($db, 'oauth2_access_tokens'); |
|
| 36 | - } |
|
| 31 | + /** |
|
| 32 | + * @param IDBConnection $db |
|
| 33 | + */ |
|
| 34 | + public function __construct(IDBConnection $db) { |
|
| 35 | + parent::__construct($db, 'oauth2_access_tokens'); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param string $code |
|
| 40 | - * @return AccessToken |
|
| 41 | - * @throws AccessTokenNotFoundException |
|
| 42 | - */ |
|
| 43 | - public function getByCode($code) { |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $qb |
|
| 46 | - ->select('*') |
|
| 47 | - ->from($this->tableName) |
|
| 48 | - ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code)))); |
|
| 49 | - $result = $qb->execute(); |
|
| 50 | - $row = $result->fetch(); |
|
| 51 | - $result->closeCursor(); |
|
| 52 | - if($row === false) { |
|
| 53 | - throw new AccessTokenNotFoundException(); |
|
| 54 | - } |
|
| 55 | - return AccessToken::fromRow($row); |
|
| 56 | - } |
|
| 38 | + /** |
|
| 39 | + * @param string $code |
|
| 40 | + * @return AccessToken |
|
| 41 | + * @throws AccessTokenNotFoundException |
|
| 42 | + */ |
|
| 43 | + public function getByCode($code) { |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $qb |
|
| 46 | + ->select('*') |
|
| 47 | + ->from($this->tableName) |
|
| 48 | + ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code)))); |
|
| 49 | + $result = $qb->execute(); |
|
| 50 | + $row = $result->fetch(); |
|
| 51 | + $result->closeCursor(); |
|
| 52 | + if($row === false) { |
|
| 53 | + throw new AccessTokenNotFoundException(); |
|
| 54 | + } |
|
| 55 | + return AccessToken::fromRow($row); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * delete all access token from a given client |
|
| 60 | - * |
|
| 61 | - * @param int $id |
|
| 62 | - */ |
|
| 63 | - public function deleteByClientId($id) { |
|
| 64 | - $qb = $this->db->getQueryBuilder(); |
|
| 65 | - $qb |
|
| 66 | - ->delete($this->tableName) |
|
| 67 | - ->where($qb->expr()->eq('client_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 68 | - $qb->execute(); |
|
| 69 | - } |
|
| 58 | + /** |
|
| 59 | + * delete all access token from a given client |
|
| 60 | + * |
|
| 61 | + * @param int $id |
|
| 62 | + */ |
|
| 63 | + public function deleteByClientId($id) { |
|
| 64 | + $qb = $this->db->getQueryBuilder(); |
|
| 65 | + $qb |
|
| 66 | + ->delete($this->tableName) |
|
| 67 | + ->where($qb->expr()->eq('client_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 68 | + $qb->execute(); |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | $result = $qb->execute(); |
| 50 | 50 | $row = $result->fetch(); |
| 51 | 51 | $result->closeCursor(); |
| 52 | - if($row === false) { |
|
| 52 | + if ($row === false) { |
|
| 53 | 53 | throw new AccessTokenNotFoundException(); |
| 54 | 54 | } |
| 55 | 55 | return AccessToken::fromRow($row); |
@@ -20,26 +20,26 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | return [ |
| 23 | - 'routes' => [ |
|
| 24 | - [ |
|
| 25 | - 'name' => 'Settings#addClient', |
|
| 26 | - 'url' => '/settings', |
|
| 27 | - 'verb' => 'POST', |
|
| 28 | - ], |
|
| 29 | - [ |
|
| 30 | - 'name' => 'Settings#deleteClient', |
|
| 31 | - 'url' => '/clients/{id}/delete', |
|
| 32 | - 'verb' => 'POST' |
|
| 33 | - ], |
|
| 34 | - [ |
|
| 35 | - 'name' => 'LoginRedirector#authorize', |
|
| 36 | - 'url' => '/authorize', |
|
| 37 | - 'verb' => 'GET', |
|
| 38 | - ], |
|
| 39 | - [ |
|
| 40 | - 'name' => 'OauthApi#getToken', |
|
| 41 | - 'url' => '/api/v1/token', |
|
| 42 | - 'verb' => 'POST' |
|
| 43 | - ], |
|
| 44 | - ], |
|
| 23 | + 'routes' => [ |
|
| 24 | + [ |
|
| 25 | + 'name' => 'Settings#addClient', |
|
| 26 | + 'url' => '/settings', |
|
| 27 | + 'verb' => 'POST', |
|
| 28 | + ], |
|
| 29 | + [ |
|
| 30 | + 'name' => 'Settings#deleteClient', |
|
| 31 | + 'url' => '/clients/{id}/delete', |
|
| 32 | + 'verb' => 'POST' |
|
| 33 | + ], |
|
| 34 | + [ |
|
| 35 | + 'name' => 'LoginRedirector#authorize', |
|
| 36 | + 'url' => '/authorize', |
|
| 37 | + 'verb' => 'GET', |
|
| 38 | + ], |
|
| 39 | + [ |
|
| 40 | + 'name' => 'OauthApi#getToken', |
|
| 41 | + 'url' => '/api/v1/token', |
|
| 42 | + 'verb' => 'POST' |
|
| 43 | + ], |
|
| 44 | + ], |
|
| 45 | 45 | ]; |
@@ -48,213 +48,213 @@ |
||
| 48 | 48 | class Auth extends AbstractBasic { |
| 49 | 49 | |
| 50 | 50 | |
| 51 | - const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
| 51 | + const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
| 52 | 52 | |
| 53 | - /** @var ISession */ |
|
| 54 | - private $session; |
|
| 55 | - /** @var Session */ |
|
| 56 | - private $userSession; |
|
| 57 | - /** @var IRequest */ |
|
| 58 | - private $request; |
|
| 59 | - /** @var string */ |
|
| 60 | - private $currentUser; |
|
| 61 | - /** @var Manager */ |
|
| 62 | - private $twoFactorManager; |
|
| 63 | - /** @var Throttler */ |
|
| 64 | - private $throttler; |
|
| 53 | + /** @var ISession */ |
|
| 54 | + private $session; |
|
| 55 | + /** @var Session */ |
|
| 56 | + private $userSession; |
|
| 57 | + /** @var IRequest */ |
|
| 58 | + private $request; |
|
| 59 | + /** @var string */ |
|
| 60 | + private $currentUser; |
|
| 61 | + /** @var Manager */ |
|
| 62 | + private $twoFactorManager; |
|
| 63 | + /** @var Throttler */ |
|
| 64 | + private $throttler; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param ISession $session |
|
| 68 | - * @param Session $userSession |
|
| 69 | - * @param IRequest $request |
|
| 70 | - * @param Manager $twoFactorManager |
|
| 71 | - * @param Throttler $throttler |
|
| 72 | - * @param string $principalPrefix |
|
| 73 | - */ |
|
| 74 | - public function __construct(ISession $session, |
|
| 75 | - Session $userSession, |
|
| 76 | - IRequest $request, |
|
| 77 | - Manager $twoFactorManager, |
|
| 78 | - Throttler $throttler, |
|
| 79 | - $principalPrefix = 'principals/users/') { |
|
| 80 | - $this->session = $session; |
|
| 81 | - $this->userSession = $userSession; |
|
| 82 | - $this->twoFactorManager = $twoFactorManager; |
|
| 83 | - $this->request = $request; |
|
| 84 | - $this->throttler = $throttler; |
|
| 85 | - $this->principalPrefix = $principalPrefix; |
|
| 66 | + /** |
|
| 67 | + * @param ISession $session |
|
| 68 | + * @param Session $userSession |
|
| 69 | + * @param IRequest $request |
|
| 70 | + * @param Manager $twoFactorManager |
|
| 71 | + * @param Throttler $throttler |
|
| 72 | + * @param string $principalPrefix |
|
| 73 | + */ |
|
| 74 | + public function __construct(ISession $session, |
|
| 75 | + Session $userSession, |
|
| 76 | + IRequest $request, |
|
| 77 | + Manager $twoFactorManager, |
|
| 78 | + Throttler $throttler, |
|
| 79 | + $principalPrefix = 'principals/users/') { |
|
| 80 | + $this->session = $session; |
|
| 81 | + $this->userSession = $userSession; |
|
| 82 | + $this->twoFactorManager = $twoFactorManager; |
|
| 83 | + $this->request = $request; |
|
| 84 | + $this->throttler = $throttler; |
|
| 85 | + $this->principalPrefix = $principalPrefix; |
|
| 86 | 86 | |
| 87 | - // setup realm |
|
| 88 | - $defaults = new \OCP\Defaults(); |
|
| 89 | - $this->realm = $defaults->getName(); |
|
| 90 | - } |
|
| 87 | + // setup realm |
|
| 88 | + $defaults = new \OCP\Defaults(); |
|
| 89 | + $this->realm = $defaults->getName(); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Whether the user has initially authenticated via DAV |
|
| 94 | - * |
|
| 95 | - * This is required for WebDAV clients that resent the cookies even when the |
|
| 96 | - * account was changed. |
|
| 97 | - * |
|
| 98 | - * @see https://github.com/owncloud/core/issues/13245 |
|
| 99 | - * |
|
| 100 | - * @param string $username |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function isDavAuthenticated($username) { |
|
| 104 | - return !is_null($this->session->get(self::DAV_AUTHENTICATED)) && |
|
| 105 | - $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
| 106 | - } |
|
| 92 | + /** |
|
| 93 | + * Whether the user has initially authenticated via DAV |
|
| 94 | + * |
|
| 95 | + * This is required for WebDAV clients that resent the cookies even when the |
|
| 96 | + * account was changed. |
|
| 97 | + * |
|
| 98 | + * @see https://github.com/owncloud/core/issues/13245 |
|
| 99 | + * |
|
| 100 | + * @param string $username |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function isDavAuthenticated($username) { |
|
| 104 | + return !is_null($this->session->get(self::DAV_AUTHENTICATED)) && |
|
| 105 | + $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Validates a username and password |
|
| 110 | - * |
|
| 111 | - * This method should return true or false depending on if login |
|
| 112 | - * succeeded. |
|
| 113 | - * |
|
| 114 | - * @param string $username |
|
| 115 | - * @param string $password |
|
| 116 | - * @return bool |
|
| 117 | - * @throws PasswordLoginForbidden |
|
| 118 | - */ |
|
| 119 | - protected function validateUserPass($username, $password) { |
|
| 120 | - if ($this->userSession->isLoggedIn() && |
|
| 121 | - $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
| 122 | - ) { |
|
| 123 | - \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
| 124 | - $this->session->close(); |
|
| 125 | - return true; |
|
| 126 | - } else { |
|
| 127 | - \OC_Util::setupFS(); //login hooks may need early access to the filesystem |
|
| 128 | - try { |
|
| 129 | - if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
| 130 | - \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
| 131 | - $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
| 132 | - $this->session->close(); |
|
| 133 | - return true; |
|
| 134 | - } else { |
|
| 135 | - $this->session->close(); |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - } catch (PasswordLoginForbiddenException $ex) { |
|
| 139 | - $this->session->close(); |
|
| 140 | - throw new PasswordLoginForbidden(); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 108 | + /** |
|
| 109 | + * Validates a username and password |
|
| 110 | + * |
|
| 111 | + * This method should return true or false depending on if login |
|
| 112 | + * succeeded. |
|
| 113 | + * |
|
| 114 | + * @param string $username |
|
| 115 | + * @param string $password |
|
| 116 | + * @return bool |
|
| 117 | + * @throws PasswordLoginForbidden |
|
| 118 | + */ |
|
| 119 | + protected function validateUserPass($username, $password) { |
|
| 120 | + if ($this->userSession->isLoggedIn() && |
|
| 121 | + $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
| 122 | + ) { |
|
| 123 | + \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
| 124 | + $this->session->close(); |
|
| 125 | + return true; |
|
| 126 | + } else { |
|
| 127 | + \OC_Util::setupFS(); //login hooks may need early access to the filesystem |
|
| 128 | + try { |
|
| 129 | + if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
| 130 | + \OC_Util::setupFS($this->userSession->getUser()->getUID()); |
|
| 131 | + $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
| 132 | + $this->session->close(); |
|
| 133 | + return true; |
|
| 134 | + } else { |
|
| 135 | + $this->session->close(); |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + } catch (PasswordLoginForbiddenException $ex) { |
|
| 139 | + $this->session->close(); |
|
| 140 | + throw new PasswordLoginForbidden(); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @param RequestInterface $request |
|
| 147 | - * @param ResponseInterface $response |
|
| 148 | - * @return array |
|
| 149 | - * @throws NotAuthenticated |
|
| 150 | - * @throws ServiceUnavailable |
|
| 151 | - */ |
|
| 152 | - function check(RequestInterface $request, ResponseInterface $response) { |
|
| 153 | - try { |
|
| 154 | - $result = $this->auth($request, $response); |
|
| 155 | - return $result; |
|
| 156 | - } catch (NotAuthenticated $e) { |
|
| 157 | - throw $e; |
|
| 158 | - } catch (Exception $e) { |
|
| 159 | - $class = get_class($e); |
|
| 160 | - $msg = $e->getMessage(); |
|
| 161 | - \OC::$server->getLogger()->logException($e); |
|
| 162 | - throw new ServiceUnavailable("$class: $msg"); |
|
| 163 | - } |
|
| 164 | - } |
|
| 145 | + /** |
|
| 146 | + * @param RequestInterface $request |
|
| 147 | + * @param ResponseInterface $response |
|
| 148 | + * @return array |
|
| 149 | + * @throws NotAuthenticated |
|
| 150 | + * @throws ServiceUnavailable |
|
| 151 | + */ |
|
| 152 | + function check(RequestInterface $request, ResponseInterface $response) { |
|
| 153 | + try { |
|
| 154 | + $result = $this->auth($request, $response); |
|
| 155 | + return $result; |
|
| 156 | + } catch (NotAuthenticated $e) { |
|
| 157 | + throw $e; |
|
| 158 | + } catch (Exception $e) { |
|
| 159 | + $class = get_class($e); |
|
| 160 | + $msg = $e->getMessage(); |
|
| 161 | + \OC::$server->getLogger()->logException($e); |
|
| 162 | + throw new ServiceUnavailable("$class: $msg"); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Checks whether a CSRF check is required on the request |
|
| 168 | - * |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - private function requiresCSRFCheck() { |
|
| 172 | - // GET requires no check at all |
|
| 173 | - if($this->request->getMethod() === 'GET') { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 166 | + /** |
|
| 167 | + * Checks whether a CSRF check is required on the request |
|
| 168 | + * |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + private function requiresCSRFCheck() { |
|
| 172 | + // GET requires no check at all |
|
| 173 | + if($this->request->getMethod() === 'GET') { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - // Official Nextcloud clients require no checks |
|
| 178 | - if($this->request->isUserAgent([ |
|
| 179 | - IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
| 180 | - IRequest::USER_AGENT_CLIENT_ANDROID, |
|
| 181 | - IRequest::USER_AGENT_CLIENT_IOS, |
|
| 182 | - ])) { |
|
| 183 | - return false; |
|
| 184 | - } |
|
| 177 | + // Official Nextcloud clients require no checks |
|
| 178 | + if($this->request->isUserAgent([ |
|
| 179 | + IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
| 180 | + IRequest::USER_AGENT_CLIENT_ANDROID, |
|
| 181 | + IRequest::USER_AGENT_CLIENT_IOS, |
|
| 182 | + ])) { |
|
| 183 | + return false; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - // If not logged-in no check is required |
|
| 187 | - if(!$this->userSession->isLoggedIn()) { |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 186 | + // If not logged-in no check is required |
|
| 187 | + if(!$this->userSession->isLoggedIn()) { |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - // POST always requires a check |
|
| 192 | - if($this->request->getMethod() === 'POST') { |
|
| 193 | - return true; |
|
| 194 | - } |
|
| 191 | + // POST always requires a check |
|
| 192 | + if($this->request->getMethod() === 'POST') { |
|
| 193 | + return true; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - // If logged-in AND DAV authenticated no check is required |
|
| 197 | - if($this->userSession->isLoggedIn() && |
|
| 198 | - $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 196 | + // If logged-in AND DAV authenticated no check is required |
|
| 197 | + if($this->userSession->isLoggedIn() && |
|
| 198 | + $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - return true; |
|
| 203 | - } |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * @param RequestInterface $request |
|
| 207 | - * @param ResponseInterface $response |
|
| 208 | - * @return array |
|
| 209 | - * @throws NotAuthenticated |
|
| 210 | - */ |
|
| 211 | - private function auth(RequestInterface $request, ResponseInterface $response) { |
|
| 212 | - $forcedLogout = false; |
|
| 205 | + /** |
|
| 206 | + * @param RequestInterface $request |
|
| 207 | + * @param ResponseInterface $response |
|
| 208 | + * @return array |
|
| 209 | + * @throws NotAuthenticated |
|
| 210 | + */ |
|
| 211 | + private function auth(RequestInterface $request, ResponseInterface $response) { |
|
| 212 | + $forcedLogout = false; |
|
| 213 | 213 | |
| 214 | - if(!$this->request->passesCSRFCheck() && |
|
| 215 | - $this->requiresCSRFCheck()) { |
|
| 216 | - // In case of a fail with POST we need to recheck the credentials |
|
| 217 | - if($this->request->getMethod() === 'POST') { |
|
| 218 | - $forcedLogout = true; |
|
| 219 | - } else { |
|
| 220 | - $response->setStatus(401); |
|
| 221 | - throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
| 222 | - } |
|
| 223 | - } |
|
| 214 | + if(!$this->request->passesCSRFCheck() && |
|
| 215 | + $this->requiresCSRFCheck()) { |
|
| 216 | + // In case of a fail with POST we need to recheck the credentials |
|
| 217 | + if($this->request->getMethod() === 'POST') { |
|
| 218 | + $forcedLogout = true; |
|
| 219 | + } else { |
|
| 220 | + $response->setStatus(401); |
|
| 221 | + throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - if($forcedLogout) { |
|
| 226 | - $this->userSession->logout(); |
|
| 227 | - } else { |
|
| 228 | - if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
| 229 | - throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
| 230 | - } |
|
| 231 | - if (\OC_User::handleApacheAuth() || |
|
| 232 | - //Fix for broken webdav clients |
|
| 233 | - ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) || |
|
| 234 | - //Well behaved clients that only send the cookie are allowed |
|
| 235 | - ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) |
|
| 236 | - ) { |
|
| 237 | - $user = $this->userSession->getUser()->getUID(); |
|
| 238 | - \OC_Util::setupFS($user); |
|
| 239 | - $this->currentUser = $user; |
|
| 240 | - $this->session->close(); |
|
| 241 | - return [true, $this->principalPrefix . $user]; |
|
| 242 | - } |
|
| 243 | - } |
|
| 225 | + if($forcedLogout) { |
|
| 226 | + $this->userSession->logout(); |
|
| 227 | + } else { |
|
| 228 | + if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
| 229 | + throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
| 230 | + } |
|
| 231 | + if (\OC_User::handleApacheAuth() || |
|
| 232 | + //Fix for broken webdav clients |
|
| 233 | + ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) || |
|
| 234 | + //Well behaved clients that only send the cookie are allowed |
|
| 235 | + ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) |
|
| 236 | + ) { |
|
| 237 | + $user = $this->userSession->getUser()->getUID(); |
|
| 238 | + \OC_Util::setupFS($user); |
|
| 239 | + $this->currentUser = $user; |
|
| 240 | + $this->session->close(); |
|
| 241 | + return [true, $this->principalPrefix . $user]; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) { |
|
| 246 | - // do not re-authenticate over ajax, use dummy auth name to prevent browser popup |
|
| 247 | - $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"'); |
|
| 248 | - $response->setStatus(401); |
|
| 249 | - throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
| 250 | - } |
|
| 245 | + if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) { |
|
| 246 | + // do not re-authenticate over ajax, use dummy auth name to prevent browser popup |
|
| 247 | + $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"'); |
|
| 248 | + $response->setStatus(401); |
|
| 249 | + throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - $data = parent::check($request, $response); |
|
| 253 | - if($data[0] === true) { |
|
| 254 | - $startPos = strrpos($data[1], '/') + 1; |
|
| 255 | - $user = $this->userSession->getUser()->getUID(); |
|
| 256 | - $data[1] = substr_replace($data[1], $user, $startPos); |
|
| 257 | - } |
|
| 258 | - return $data; |
|
| 259 | - } |
|
| 252 | + $data = parent::check($request, $response); |
|
| 253 | + if($data[0] === true) { |
|
| 254 | + $startPos = strrpos($data[1], '/') + 1; |
|
| 255 | + $user = $this->userSession->getUser()->getUID(); |
|
| 256 | + $data[1] = substr_replace($data[1], $user, $startPos); |
|
| 257 | + } |
|
| 258 | + return $data; |
|
| 259 | + } |
|
| 260 | 260 | } |
@@ -170,12 +170,12 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | private function requiresCSRFCheck() { |
| 172 | 172 | // GET requires no check at all |
| 173 | - if($this->request->getMethod() === 'GET') { |
|
| 173 | + if ($this->request->getMethod() === 'GET') { |
|
| 174 | 174 | return false; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | // Official Nextcloud clients require no checks |
| 178 | - if($this->request->isUserAgent([ |
|
| 178 | + if ($this->request->isUserAgent([ |
|
| 179 | 179 | IRequest::USER_AGENT_CLIENT_DESKTOP, |
| 180 | 180 | IRequest::USER_AGENT_CLIENT_ANDROID, |
| 181 | 181 | IRequest::USER_AGENT_CLIENT_IOS, |
@@ -184,17 +184,17 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | // If not logged-in no check is required |
| 187 | - if(!$this->userSession->isLoggedIn()) { |
|
| 187 | + if (!$this->userSession->isLoggedIn()) { |
|
| 188 | 188 | return false; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | // POST always requires a check |
| 192 | - if($this->request->getMethod() === 'POST') { |
|
| 192 | + if ($this->request->getMethod() === 'POST') { |
|
| 193 | 193 | return true; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | // If logged-in AND DAV authenticated no check is required |
| 197 | - if($this->userSession->isLoggedIn() && |
|
| 197 | + if ($this->userSession->isLoggedIn() && |
|
| 198 | 198 | $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
| 199 | 199 | return false; |
| 200 | 200 | } |
@@ -211,10 +211,10 @@ discard block |
||
| 211 | 211 | private function auth(RequestInterface $request, ResponseInterface $response) { |
| 212 | 212 | $forcedLogout = false; |
| 213 | 213 | |
| 214 | - if(!$this->request->passesCSRFCheck() && |
|
| 214 | + if (!$this->request->passesCSRFCheck() && |
|
| 215 | 215 | $this->requiresCSRFCheck()) { |
| 216 | 216 | // In case of a fail with POST we need to recheck the credentials |
| 217 | - if($this->request->getMethod() === 'POST') { |
|
| 217 | + if ($this->request->getMethod() === 'POST') { |
|
| 218 | 218 | $forcedLogout = true; |
| 219 | 219 | } else { |
| 220 | 220 | $response->setStatus(401); |
@@ -222,10 +222,10 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - if($forcedLogout) { |
|
| 225 | + if ($forcedLogout) { |
|
| 226 | 226 | $this->userSession->logout(); |
| 227 | 227 | } else { |
| 228 | - if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
| 228 | + if ($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
| 229 | 229 | throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
| 230 | 230 | } |
| 231 | 231 | if (\OC_User::handleApacheAuth() || |
@@ -238,19 +238,19 @@ discard block |
||
| 238 | 238 | \OC_Util::setupFS($user); |
| 239 | 239 | $this->currentUser = $user; |
| 240 | 240 | $this->session->close(); |
| 241 | - return [true, $this->principalPrefix . $user]; |
|
| 241 | + return [true, $this->principalPrefix.$user]; |
|
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) { |
| 246 | 246 | // do not re-authenticate over ajax, use dummy auth name to prevent browser popup |
| 247 | - $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"'); |
|
| 247 | + $response->addHeader('WWW-Authenticate', 'DummyBasic realm="'.$this->realm.'"'); |
|
| 248 | 248 | $response->setStatus(401); |
| 249 | 249 | throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | $data = parent::check($request, $response); |
| 253 | - if($data[0] === true) { |
|
| 253 | + if ($data[0] === true) { |
|
| 254 | 254 | $startPos = strrpos($data[1], '/') + 1; |
| 255 | 255 | $user = $this->userSession->getUser()->getUID(); |
| 256 | 256 | $data[1] = substr_replace($data[1], $user, $startPos); |
@@ -27,50 +27,50 @@ |
||
| 27 | 27 | use Sabre\DAV\Auth\Backend\AbstractBearer; |
| 28 | 28 | |
| 29 | 29 | class BearerAuth extends AbstractBearer { |
| 30 | - /** @var IUserSession */ |
|
| 31 | - private $userSession; |
|
| 32 | - /** @var ISession */ |
|
| 33 | - private $session; |
|
| 34 | - /** @var IRequest */ |
|
| 35 | - private $request; |
|
| 36 | - /** @var string */ |
|
| 37 | - private $principalPrefix; |
|
| 30 | + /** @var IUserSession */ |
|
| 31 | + private $userSession; |
|
| 32 | + /** @var ISession */ |
|
| 33 | + private $session; |
|
| 34 | + /** @var IRequest */ |
|
| 35 | + private $request; |
|
| 36 | + /** @var string */ |
|
| 37 | + private $principalPrefix; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param IUserSession $userSession |
|
| 41 | - * @param ISession $session |
|
| 42 | - * @param string $principalPrefix |
|
| 43 | - * @param IRequest $request |
|
| 44 | - */ |
|
| 45 | - public function __construct(IUserSession $userSession, |
|
| 46 | - ISession $session, |
|
| 47 | - IRequest $request, |
|
| 48 | - $principalPrefix = 'principals/users/') { |
|
| 49 | - $this->userSession = $userSession; |
|
| 50 | - $this->session = $session; |
|
| 51 | - $this->request = $request; |
|
| 52 | - $this->principalPrefix = $principalPrefix; |
|
| 53 | - } |
|
| 39 | + /** |
|
| 40 | + * @param IUserSession $userSession |
|
| 41 | + * @param ISession $session |
|
| 42 | + * @param string $principalPrefix |
|
| 43 | + * @param IRequest $request |
|
| 44 | + */ |
|
| 45 | + public function __construct(IUserSession $userSession, |
|
| 46 | + ISession $session, |
|
| 47 | + IRequest $request, |
|
| 48 | + $principalPrefix = 'principals/users/') { |
|
| 49 | + $this->userSession = $userSession; |
|
| 50 | + $this->session = $session; |
|
| 51 | + $this->request = $request; |
|
| 52 | + $this->principalPrefix = $principalPrefix; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - private function setupUserFs($userId) { |
|
| 56 | - \OC_Util::setupFS($userId); |
|
| 57 | - $this->session->close(); |
|
| 58 | - return $this->principalPrefix . $userId; |
|
| 59 | - } |
|
| 55 | + private function setupUserFs($userId) { |
|
| 56 | + \OC_Util::setupFS($userId); |
|
| 57 | + $this->session->close(); |
|
| 58 | + return $this->principalPrefix . $userId; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * {@inheritdoc} |
|
| 63 | - */ |
|
| 64 | - public function validateBearerToken($bearerToken) { |
|
| 65 | - \OC_Util::setupFS(); |
|
| 61 | + /** |
|
| 62 | + * {@inheritdoc} |
|
| 63 | + */ |
|
| 64 | + public function validateBearerToken($bearerToken) { |
|
| 65 | + \OC_Util::setupFS(); |
|
| 66 | 66 | |
| 67 | - if(!$this->userSession->isLoggedIn()) { |
|
| 68 | - $this->userSession->tryTokenLogin($this->request); |
|
| 69 | - } |
|
| 70 | - if($this->userSession->isLoggedIn()) { |
|
| 71 | - return $this->setupUserFs($this->userSession->getUser()->getUID()); |
|
| 72 | - } |
|
| 67 | + if(!$this->userSession->isLoggedIn()) { |
|
| 68 | + $this->userSession->tryTokenLogin($this->request); |
|
| 69 | + } |
|
| 70 | + if($this->userSession->isLoggedIn()) { |
|
| 71 | + return $this->setupUserFs($this->userSession->getUser()->getUID()); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return false; |
|
| 75 | - } |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | private function setupUserFs($userId) { |
| 56 | 56 | \OC_Util::setupFS($userId); |
| 57 | 57 | $this->session->close(); |
| 58 | - return $this->principalPrefix . $userId; |
|
| 58 | + return $this->principalPrefix.$userId; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -64,10 +64,10 @@ discard block |
||
| 64 | 64 | public function validateBearerToken($bearerToken) { |
| 65 | 65 | \OC_Util::setupFS(); |
| 66 | 66 | |
| 67 | - if(!$this->userSession->isLoggedIn()) { |
|
| 67 | + if (!$this->userSession->isLoggedIn()) { |
|
| 68 | 68 | $this->userSession->tryTokenLogin($this->request); |
| 69 | 69 | } |
| 70 | - if($this->userSession->isLoggedIn()) { |
|
| 70 | + if ($this->userSession->isLoggedIn()) { |
|
| 71 | 71 | return $this->setupUserFs($this->userSession->getUser()->getUID()); |
| 72 | 72 | } |
| 73 | 73 | |
@@ -58,198 +58,198 @@ |
||
| 58 | 58 | |
| 59 | 59 | class Server { |
| 60 | 60 | |
| 61 | - /** @var IRequest */ |
|
| 62 | - private $request; |
|
| 63 | - |
|
| 64 | - /** @var string */ |
|
| 65 | - private $baseUri; |
|
| 66 | - |
|
| 67 | - /** @var Connector\Sabre\Server */ |
|
| 68 | - private $server; |
|
| 69 | - |
|
| 70 | - public function __construct(IRequest $request, $baseUri) { |
|
| 71 | - $this->request = $request; |
|
| 72 | - $this->baseUri = $baseUri; |
|
| 73 | - $logger = \OC::$server->getLogger(); |
|
| 74 | - $mailer = \OC::$server->getMailer(); |
|
| 75 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 76 | - |
|
| 77 | - $root = new RootCollection(); |
|
| 78 | - $this->server = new \OCA\DAV\Connector\Sabre\Server($root); |
|
| 79 | - |
|
| 80 | - // Add maintenance plugin |
|
| 81 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
| 82 | - |
|
| 83 | - // Backends |
|
| 84 | - $authBackend = new Auth( |
|
| 85 | - \OC::$server->getSession(), |
|
| 86 | - \OC::$server->getUserSession(), |
|
| 87 | - \OC::$server->getRequest(), |
|
| 88 | - \OC::$server->getTwoFactorAuthManager(), |
|
| 89 | - \OC::$server->getBruteForceThrottler() |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - // Set URL explicitly due to reverse-proxy situations |
|
| 93 | - $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
| 94 | - $this->server->setBaseUri($this->baseUri); |
|
| 95 | - |
|
| 96 | - $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
| 97 | - $authPlugin = new Plugin(); |
|
| 98 | - $authPlugin->addBackend(new PublicAuth()); |
|
| 99 | - $this->server->addPlugin($authPlugin); |
|
| 100 | - |
|
| 101 | - // allow setup of additional auth backends |
|
| 102 | - $event = new SabrePluginEvent($this->server); |
|
| 103 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
| 104 | - |
|
| 105 | - $bearerAuthBackend = new BearerAuth( |
|
| 106 | - \OC::$server->getUserSession(), |
|
| 107 | - \OC::$server->getSession(), |
|
| 108 | - \OC::$server->getRequest() |
|
| 109 | - ); |
|
| 110 | - $authPlugin->addBackend($bearerAuthBackend); |
|
| 111 | - // because we are throwing exceptions this plugin has to be the last one |
|
| 112 | - $authPlugin->addBackend($authBackend); |
|
| 113 | - |
|
| 114 | - // debugging |
|
| 115 | - if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
| 116 | - $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
| 117 | - } else { |
|
| 118 | - $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
| 122 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 123 | - $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
| 124 | - |
|
| 125 | - // acl |
|
| 126 | - $acl = new DavAclPlugin(); |
|
| 127 | - $acl->principalCollectionSet = [ |
|
| 128 | - 'principals/users', 'principals/groups' |
|
| 129 | - ]; |
|
| 130 | - $acl->defaultUsernamePath = 'principals/users'; |
|
| 131 | - $this->server->addPlugin($acl); |
|
| 132 | - |
|
| 133 | - // calendar plugins |
|
| 134 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
| 135 | - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
| 136 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
| 137 | - $this->server->addPlugin(new IMipPlugin($mailer, $logger)); |
|
| 138 | - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
| 139 | - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
| 140 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 141 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
| 142 | - \OC::$server->getConfig(), |
|
| 143 | - \OC::$server->getURLGenerator() |
|
| 144 | - )); |
|
| 145 | - |
|
| 146 | - // addressbook plugins |
|
| 147 | - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
| 148 | - $this->server->addPlugin(new VCFExportPlugin()); |
|
| 149 | - $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
| 150 | - |
|
| 151 | - // system tags plugins |
|
| 152 | - $this->server->addPlugin(new SystemTagPlugin( |
|
| 153 | - \OC::$server->getSystemTagManager(), |
|
| 154 | - \OC::$server->getGroupManager(), |
|
| 155 | - \OC::$server->getUserSession() |
|
| 156 | - )); |
|
| 157 | - |
|
| 158 | - // comments plugin |
|
| 159 | - $this->server->addPlugin(new CommentsPlugin( |
|
| 160 | - \OC::$server->getCommentsManager(), |
|
| 161 | - \OC::$server->getUserSession() |
|
| 162 | - )); |
|
| 163 | - |
|
| 164 | - $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
| 165 | - |
|
| 166 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 167 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
| 168 | - if($request->isUserAgent([ |
|
| 169 | - '/WebDAVFS/', |
|
| 170 | - '/Microsoft Office OneNote 2013/', |
|
| 171 | - '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
| 172 | - ])) { |
|
| 173 | - $this->server->addPlugin(new FakeLockerPlugin()); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
| 177 | - $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // wait with registering these until auth is handled and the filesystem is setup |
|
| 181 | - $this->server->on('beforeMethod', function () { |
|
| 182 | - // custom properties plugin must be the last one |
|
| 183 | - $userSession = \OC::$server->getUserSession(); |
|
| 184 | - $user = $userSession->getUser(); |
|
| 185 | - if ($user !== null) { |
|
| 186 | - $view = \OC\Files\Filesystem::getView(); |
|
| 187 | - $this->server->addPlugin( |
|
| 188 | - new FilesPlugin( |
|
| 189 | - $this->server->tree, |
|
| 190 | - \OC::$server->getConfig(), |
|
| 191 | - $this->request, |
|
| 192 | - \OC::$server->getPreviewManager(), |
|
| 193 | - false, |
|
| 194 | - !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
| 195 | - ) |
|
| 196 | - ); |
|
| 197 | - |
|
| 198 | - $this->server->addPlugin( |
|
| 199 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 200 | - new CustomPropertiesBackend( |
|
| 201 | - $this->server->tree, |
|
| 202 | - \OC::$server->getDatabaseConnection(), |
|
| 203 | - \OC::$server->getUserSession()->getUser() |
|
| 204 | - ) |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - if ($view !== null) { |
|
| 208 | - $this->server->addPlugin( |
|
| 209 | - new QuotaPlugin($view)); |
|
| 210 | - } |
|
| 211 | - $this->server->addPlugin( |
|
| 212 | - new TagsPlugin( |
|
| 213 | - $this->server->tree, \OC::$server->getTagManager() |
|
| 214 | - ) |
|
| 215 | - ); |
|
| 216 | - // TODO: switch to LazyUserFolder |
|
| 217 | - $userFolder = \OC::$server->getUserFolder(); |
|
| 218 | - $this->server->addPlugin(new SharesPlugin( |
|
| 219 | - $this->server->tree, |
|
| 220 | - $userSession, |
|
| 221 | - $userFolder, |
|
| 222 | - \OC::$server->getShareManager() |
|
| 223 | - )); |
|
| 224 | - $this->server->addPlugin(new CommentPropertiesPlugin( |
|
| 225 | - \OC::$server->getCommentsManager(), |
|
| 226 | - $userSession |
|
| 227 | - )); |
|
| 228 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
| 229 | - if ($view !== null) { |
|
| 230 | - $this->server->addPlugin(new FilesReportPlugin( |
|
| 231 | - $this->server->tree, |
|
| 232 | - $view, |
|
| 233 | - \OC::$server->getSystemTagManager(), |
|
| 234 | - \OC::$server->getSystemTagObjectMapper(), |
|
| 235 | - \OC::$server->getTagManager(), |
|
| 236 | - $userSession, |
|
| 237 | - \OC::$server->getGroupManager(), |
|
| 238 | - $userFolder |
|
| 239 | - )); |
|
| 240 | - $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( |
|
| 241 | - $this->server->tree, |
|
| 242 | - $user, |
|
| 243 | - \OC::$server->getRootFolder(), |
|
| 244 | - \OC::$server->getShareManager(), |
|
| 245 | - $view |
|
| 246 | - ))); |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - }); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - public function exec() { |
|
| 253 | - $this->server->exec(); |
|
| 254 | - } |
|
| 61 | + /** @var IRequest */ |
|
| 62 | + private $request; |
|
| 63 | + |
|
| 64 | + /** @var string */ |
|
| 65 | + private $baseUri; |
|
| 66 | + |
|
| 67 | + /** @var Connector\Sabre\Server */ |
|
| 68 | + private $server; |
|
| 69 | + |
|
| 70 | + public function __construct(IRequest $request, $baseUri) { |
|
| 71 | + $this->request = $request; |
|
| 72 | + $this->baseUri = $baseUri; |
|
| 73 | + $logger = \OC::$server->getLogger(); |
|
| 74 | + $mailer = \OC::$server->getMailer(); |
|
| 75 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 76 | + |
|
| 77 | + $root = new RootCollection(); |
|
| 78 | + $this->server = new \OCA\DAV\Connector\Sabre\Server($root); |
|
| 79 | + |
|
| 80 | + // Add maintenance plugin |
|
| 81 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
| 82 | + |
|
| 83 | + // Backends |
|
| 84 | + $authBackend = new Auth( |
|
| 85 | + \OC::$server->getSession(), |
|
| 86 | + \OC::$server->getUserSession(), |
|
| 87 | + \OC::$server->getRequest(), |
|
| 88 | + \OC::$server->getTwoFactorAuthManager(), |
|
| 89 | + \OC::$server->getBruteForceThrottler() |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + // Set URL explicitly due to reverse-proxy situations |
|
| 93 | + $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
| 94 | + $this->server->setBaseUri($this->baseUri); |
|
| 95 | + |
|
| 96 | + $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
| 97 | + $authPlugin = new Plugin(); |
|
| 98 | + $authPlugin->addBackend(new PublicAuth()); |
|
| 99 | + $this->server->addPlugin($authPlugin); |
|
| 100 | + |
|
| 101 | + // allow setup of additional auth backends |
|
| 102 | + $event = new SabrePluginEvent($this->server); |
|
| 103 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
| 104 | + |
|
| 105 | + $bearerAuthBackend = new BearerAuth( |
|
| 106 | + \OC::$server->getUserSession(), |
|
| 107 | + \OC::$server->getSession(), |
|
| 108 | + \OC::$server->getRequest() |
|
| 109 | + ); |
|
| 110 | + $authPlugin->addBackend($bearerAuthBackend); |
|
| 111 | + // because we are throwing exceptions this plugin has to be the last one |
|
| 112 | + $authPlugin->addBackend($authBackend); |
|
| 113 | + |
|
| 114 | + // debugging |
|
| 115 | + if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
| 116 | + $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
| 117 | + } else { |
|
| 118 | + $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
| 122 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 123 | + $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
| 124 | + |
|
| 125 | + // acl |
|
| 126 | + $acl = new DavAclPlugin(); |
|
| 127 | + $acl->principalCollectionSet = [ |
|
| 128 | + 'principals/users', 'principals/groups' |
|
| 129 | + ]; |
|
| 130 | + $acl->defaultUsernamePath = 'principals/users'; |
|
| 131 | + $this->server->addPlugin($acl); |
|
| 132 | + |
|
| 133 | + // calendar plugins |
|
| 134 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
| 135 | + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
| 136 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
| 137 | + $this->server->addPlugin(new IMipPlugin($mailer, $logger)); |
|
| 138 | + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
| 139 | + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
| 140 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 141 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
| 142 | + \OC::$server->getConfig(), |
|
| 143 | + \OC::$server->getURLGenerator() |
|
| 144 | + )); |
|
| 145 | + |
|
| 146 | + // addressbook plugins |
|
| 147 | + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
| 148 | + $this->server->addPlugin(new VCFExportPlugin()); |
|
| 149 | + $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
| 150 | + |
|
| 151 | + // system tags plugins |
|
| 152 | + $this->server->addPlugin(new SystemTagPlugin( |
|
| 153 | + \OC::$server->getSystemTagManager(), |
|
| 154 | + \OC::$server->getGroupManager(), |
|
| 155 | + \OC::$server->getUserSession() |
|
| 156 | + )); |
|
| 157 | + |
|
| 158 | + // comments plugin |
|
| 159 | + $this->server->addPlugin(new CommentsPlugin( |
|
| 160 | + \OC::$server->getCommentsManager(), |
|
| 161 | + \OC::$server->getUserSession() |
|
| 162 | + )); |
|
| 163 | + |
|
| 164 | + $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
| 165 | + |
|
| 166 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 167 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
| 168 | + if($request->isUserAgent([ |
|
| 169 | + '/WebDAVFS/', |
|
| 170 | + '/Microsoft Office OneNote 2013/', |
|
| 171 | + '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
| 172 | + ])) { |
|
| 173 | + $this->server->addPlugin(new FakeLockerPlugin()); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
| 177 | + $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // wait with registering these until auth is handled and the filesystem is setup |
|
| 181 | + $this->server->on('beforeMethod', function () { |
|
| 182 | + // custom properties plugin must be the last one |
|
| 183 | + $userSession = \OC::$server->getUserSession(); |
|
| 184 | + $user = $userSession->getUser(); |
|
| 185 | + if ($user !== null) { |
|
| 186 | + $view = \OC\Files\Filesystem::getView(); |
|
| 187 | + $this->server->addPlugin( |
|
| 188 | + new FilesPlugin( |
|
| 189 | + $this->server->tree, |
|
| 190 | + \OC::$server->getConfig(), |
|
| 191 | + $this->request, |
|
| 192 | + \OC::$server->getPreviewManager(), |
|
| 193 | + false, |
|
| 194 | + !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
| 195 | + ) |
|
| 196 | + ); |
|
| 197 | + |
|
| 198 | + $this->server->addPlugin( |
|
| 199 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 200 | + new CustomPropertiesBackend( |
|
| 201 | + $this->server->tree, |
|
| 202 | + \OC::$server->getDatabaseConnection(), |
|
| 203 | + \OC::$server->getUserSession()->getUser() |
|
| 204 | + ) |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + if ($view !== null) { |
|
| 208 | + $this->server->addPlugin( |
|
| 209 | + new QuotaPlugin($view)); |
|
| 210 | + } |
|
| 211 | + $this->server->addPlugin( |
|
| 212 | + new TagsPlugin( |
|
| 213 | + $this->server->tree, \OC::$server->getTagManager() |
|
| 214 | + ) |
|
| 215 | + ); |
|
| 216 | + // TODO: switch to LazyUserFolder |
|
| 217 | + $userFolder = \OC::$server->getUserFolder(); |
|
| 218 | + $this->server->addPlugin(new SharesPlugin( |
|
| 219 | + $this->server->tree, |
|
| 220 | + $userSession, |
|
| 221 | + $userFolder, |
|
| 222 | + \OC::$server->getShareManager() |
|
| 223 | + )); |
|
| 224 | + $this->server->addPlugin(new CommentPropertiesPlugin( |
|
| 225 | + \OC::$server->getCommentsManager(), |
|
| 226 | + $userSession |
|
| 227 | + )); |
|
| 228 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
| 229 | + if ($view !== null) { |
|
| 230 | + $this->server->addPlugin(new FilesReportPlugin( |
|
| 231 | + $this->server->tree, |
|
| 232 | + $view, |
|
| 233 | + \OC::$server->getSystemTagManager(), |
|
| 234 | + \OC::$server->getSystemTagObjectMapper(), |
|
| 235 | + \OC::$server->getTagManager(), |
|
| 236 | + $userSession, |
|
| 237 | + \OC::$server->getGroupManager(), |
|
| 238 | + $userFolder |
|
| 239 | + )); |
|
| 240 | + $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( |
|
| 241 | + $this->server->tree, |
|
| 242 | + $user, |
|
| 243 | + \OC::$server->getRootFolder(), |
|
| 244 | + \OC::$server->getShareManager(), |
|
| 245 | + $view |
|
| 246 | + ))); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + }); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + public function exec() { |
|
| 253 | + $this->server->exec(); |
|
| 254 | + } |
|
| 255 | 255 | } |