| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the tiqr project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * The tiqr project aims to provide an open implementation for  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * authentication using mobile devices. It was initiated by  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * SURFnet and developed by Egeniq. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * More information: http://www.tiqr.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Patrick Honing <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package tiqr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @license New BSD License - See LICENSE file for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @copyright (C) 2010-2012 SURFnet BV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Create SQL table (MySQL): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * CREATE TABLE `tiqrusersecret` (`userid` varchar(10) PRIMARY KEY, `secret` varchar(100)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | require_once 'Tiqr/UserSecretStorage/UserSecretStorageTrait.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * This user storage implementation implements a user secret storage using PDO. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * It is usable for any database with a PDO driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @author Patrick Honing <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class Tiqr_UserSecretStorage_Pdo implements Tiqr_UserSecretStorage_Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     use UserSecretStorageTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $tableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $handle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param Tiqr_UserSecretStorage_Encryption_Interface $encryption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param PDO $handle | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         Tiqr_UserSecretStorage_Encryption_Interface $encryption, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         LoggerInterface $logger, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         PDO $handle, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         string $tableName | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->encryption = $encryption; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->logger = $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->handle = $handle; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->tableName = $tableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     private function userExists($userId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $sth = $this->handle->prepare("SELECT userid FROM ".$this->tableName." WHERE userid = ?"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $sth->execute(array($userId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $result = $sth->fetchColumn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($result !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->logger->debug('Unable fot find user in user secret storage (PDO)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Get the user's secret | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param String $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return mixed: null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private function getUserSecret($userId) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $sth = $this->handle->prepare("SELECT secret FROM ".$this->tableName." WHERE userid = ?"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if($sth->execute(array($userId))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $secret = $sth->fetchColumn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             if ($secret !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 return $secret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->logger->notice('Unable to retrieve user secret from user secret storage (PDO)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Store a secret for a user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param String $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param String $secret | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     private function setUserSecret($userId, $secret) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if ($this->userExists($userId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $sth = $this->handle->prepare("UPDATE ".$this->tableName." SET secret = ? WHERE userid = ?"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $sth = $this->handle->prepare("INSERT INTO ".$this->tableName." (secret,userid) VALUES (?,?)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $result = $sth->execute(array($secret,$userId)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (!$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             throw new ReadWriteException('Unable to persist user secret in user secret storage (PDO)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                        
This check looks for private methods that have been defined, but are not used inside the class.