| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // +---------------------------------------------------------------------------+ |
|---|
| 4 | // | This file is part of the Agavi package. | |
|---|
| 5 | // | Copyright (c) 2005-2008 the Agavi Project. | |
|---|
| 6 | // | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. | |
|---|
| 7 | // | | |
|---|
| 8 | // | For the full copyright and license information, please view the LICENSE | |
|---|
| 9 | // | file that was distributed with this source code. You can also view the | |
|---|
| 10 | // | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|---|
| 11 | // | vi: set noexpandtab: | |
|---|
| 12 | // | Local Variables: | |
|---|
| 13 | // | indent-tabs-mode: t | |
|---|
| 14 | // | End: | |
|---|
| 15 | // +---------------------------------------------------------------------------+ |
|---|
| 16 | |
|---|
| 17 | class AgaviSampleAppUser extends AgaviRbacSecurityUser |
|---|
| 18 | { |
|---|
| 19 | /** |
|---|
| 20 | * Let's pretend this is our database. For the sake of example ;) |
|---|
| 21 | */ |
|---|
| 22 | static $users = array( |
|---|
| 23 | 'Chuck Norris' => array( |
|---|
| 24 | 'salt' => 'bb6cb0a1ea7b94d9a1ffdfe74a3e141a', |
|---|
| 25 | 'password' => 'd436130cf2f5024cfdb3aa7325322d530336b95f', // that's "kick" plus the salt |
|---|
| 26 | 'roles' => array( |
|---|
| 27 | 'photographer', |
|---|
| 28 | ) |
|---|
| 29 | ), |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | public function startup() |
|---|
| 33 | { |
|---|
| 34 | parent::startup(); |
|---|
| 35 | |
|---|
| 36 | $reqData = $this->getContext()->getRequest()->getRequestData(); |
|---|
| 37 | |
|---|
| 38 | if(!$this->isAuthenticated() && $reqData->hasCookie('autologon')) { |
|---|
| 39 | $login = $reqData->getCookie('autologon'); |
|---|
| 40 | try { |
|---|
| 41 | $this->login($login['username'], $login['password'], true); |
|---|
| 42 | } catch(AgaviSecurityException $e) { |
|---|
| 43 | $response = $this->getContext()->getController()->getGlobalResponse(); |
|---|
| 44 | // login didn't work. that cookie sucks, delete it. |
|---|
| 45 | $response->setCookie('autologon[username]', false); |
|---|
| 46 | $response->setCookie('autologon[password]', false); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public function login($username, $password, $isPasswordHashed = false) |
|---|
| 52 | { |
|---|
| 53 | if(!isset(self::$users[$username])) { |
|---|
| 54 | throw new AgaviSecurityException('username'); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | if(!$isPasswordHashed) { |
|---|
| 58 | $password = self::computeSaltedHash($password, self::$users[$username]['salt']); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if($password != self::$users[$username]['password']) { |
|---|
| 62 | throw new AgaviSecurityException('password'); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | $this->setAuthenticated(true); |
|---|
| 66 | $this->clearCredentials(); |
|---|
| 67 | $this->grantRoles(self::$users[$username]['roles']); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | public static function computeSaltedHash($secret, $salt) |
|---|
| 71 | { |
|---|
| 72 | // sha1 is flawed. you know the drill. this is just an example. |
|---|
| 73 | return sha1($secret . $salt); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | public static function getPassword($username) |
|---|
| 77 | { |
|---|
| 78 | if(self::$users[$username]) { |
|---|
| 79 | return self::$users[$username]['password']; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public function logout() |
|---|
| 84 | { |
|---|
| 85 | $this->clearCredentials(); |
|---|
| 86 | $this->setAuthenticated(false); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | ?> |
|---|