6e75555102d4de6ebf755c5b5186676723704e49
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / Classes / RsaAuthService.php
1 <?php
2 namespace TYPO3\CMS\Rsaauth;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Dmitry Dulepov <dmitry@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 // Include backends
28 /**
29 * Service "RSA authentication" for the "rsaauth" extension. This service will
30 * authenticate a user using hos password encoded with one time public key. It
31 * uses the standard TYPO3 service to do all dirty work. Firsts, it will decode
32 * the password and then pass it to the parent service ('sv'). This ensures that it
33 * always works, even if other TYPO3 internals change.
34 *
35 * @author Dmitry Dulepov <dmitry@typo3.org>
36 */
37 class RsaAuthService extends \TYPO3\CMS\Sv\AuthenticationService {
38
39 /**
40 * An RSA backend.
41 *
42 * @var \TYPO3\CMS\Rsaauth\Backend\AbstractBackend
43 */
44 protected $backend = NULL;
45
46 /**
47 * Standard extension key for the service
48 * The extension key.
49 *
50 * @var string
51 */
52 public $extKey = 'rsaauth';
53
54 /**
55 * Standard prefix id for the service
56 * Same as class name
57 *
58 * @var string
59 */
60 public $prefixId = 'tx_rsaauth_sv1';
61
62 /**
63 * Standard relative path for the service
64 * Path to this script relative to the extension dir.
65 *
66 * @var string
67 */
68 public $scriptRelPath = 'sv1/class.tx_rsaauth_sv1.php';
69
70 /**
71 * Process the submitted credentials.
72 * In this case decrypt the password if it is RSA encrypted.
73 *
74 * @param array $loginData Credentials that are submitted and potentially modified by other services
75 * @param string $passwordTransmissionStrategy Keyword of how the password has been hashed or encrypted before submission
76 * @return boolean
77 */
78 public function processLoginData(array &$loginData, $passwordTransmissionStrategy) {
79 $isProcessed = FALSE;
80 if ($passwordTransmissionStrategy === 'rsa') {
81 $storage = \TYPO3\CMS\Rsaauth\Storage\StorageFactory::getStorage();
82 /** @var $storage \TYPO3\CMS\Rsaauth\Storage\AbstractStorage */
83 // Decrypt the password
84 $password = $loginData['uident'];
85 $key = $storage->get();
86 if ($key != NULL && substr($password, 0, 4) === 'rsa:') {
87 // Decode password and store it in loginData
88 $decryptedPassword = $this->backend->decrypt($key, substr($password, 4));
89 if ($decryptedPassword !== NULL) {
90 $loginData['uident_text'] = $decryptedPassword;
91 $isProcessed = TRUE;
92 } else {
93 if ($this->pObj->writeDevLog) {
94 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Process login data: Failed to RSA decrypt password', 'TYPO3\\CMS\\Rsaauth\\RsaAuthService');
95 }
96 }
97 // Remove the key
98 $storage->put(NULL);
99 } else {
100 if ($this->pObj->writeDevLog) {
101 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Process login data: passwordTransmissionStrategy has been set to "rsa" but no rsa encrypted password has been found.', 'TYPO3\\CMS\\Rsaauth\\RsaAuthService');
102 }
103 }
104 }
105 return $isProcessed;
106 }
107
108 /**
109 * Initializes the service.
110 *
111 * @return boolean
112 */
113 public function init() {
114 $available = parent::init();
115 if ($available) {
116 // Get the backend
117 $this->backend = \TYPO3\CMS\Rsaauth\Backend\BackendFactory::getBackend();
118 if (is_null($this->backend)) {
119 $available = FALSE;
120 }
121 }
122 return $available;
123 }
124
125 }