refactoring container for speed.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<?php namespace Laravel\Security;
|
||||
|
||||
use Laravel\IoC;
|
||||
use Laravel\Config;
|
||||
use Laravel\Session\Payload;
|
||||
|
||||
@@ -10,14 +11,7 @@ class Auth {
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* The session payload instance.
|
||||
*
|
||||
* @var Session\Payload
|
||||
*/
|
||||
protected $session;
|
||||
protected static $user;
|
||||
|
||||
/**
|
||||
* The key used when storing the user ID in the session.
|
||||
@@ -26,25 +20,14 @@ class Auth {
|
||||
*/
|
||||
const user_key = 'laravel_user_id';
|
||||
|
||||
/**
|
||||
* Create a new authenticator instance.
|
||||
*
|
||||
* @param Session\Payload $session
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Payload $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the current user of the application is authenticated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check()
|
||||
public static function check()
|
||||
{
|
||||
return ! is_null($this->user());
|
||||
return ! is_null(static::user());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,11 +46,13 @@ class Auth {
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function user()
|
||||
public static function user()
|
||||
{
|
||||
if ( ! is_null($this->user)) return $this->user;
|
||||
if ( ! is_null(static::$user)) return static::$user;
|
||||
|
||||
return $this->user = call_user_func(Config::get('auth.user'), $this->session->get(Auth::user_key));
|
||||
$id = IoC::container()->core('session')->get(Auth::user_key);
|
||||
|
||||
return static::$user = call_user_func(Config::get('auth.user'), $id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,11 +65,11 @@ class Auth {
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
public function attempt($username, $password = null)
|
||||
public static function attempt($username, $password = null)
|
||||
{
|
||||
if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password)))
|
||||
{
|
||||
$this->remember($user);
|
||||
static::remember($user);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -100,11 +85,11 @@ class Auth {
|
||||
* @param object $user
|
||||
* @return void
|
||||
*/
|
||||
public function remember($user)
|
||||
public static function remember($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
static::$user = $user;
|
||||
|
||||
$this->session->put(Auth::user_key, $user->id);
|
||||
IoC::container()->core('session')->put(Auth::user_key, $user->id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,13 +99,13 @@ class Auth {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function logout()
|
||||
public static function logout()
|
||||
{
|
||||
call_user_func(Config::get('auth.logout'), $this->user());
|
||||
call_user_func(Config::get('auth.logout'), static::user());
|
||||
|
||||
$this->user = null;
|
||||
static::$user = null;
|
||||
|
||||
$this->session->forget(Auth::user_key);
|
||||
IoC::container()->core('session')->forget(Auth::user_key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php namespace Laravel\Security;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
if (trim(Config::get('application.key')) === '')
|
||||
{
|
||||
throw new \Exception('The encryption class may not be used without an encryption key.');
|
||||
}
|
||||
|
||||
class Crypter {
|
||||
|
||||
/**
|
||||
@@ -7,44 +14,14 @@ class Crypter {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $cipher;
|
||||
protected static $cipher = MCRYPT_RIJNDAEL_256;
|
||||
|
||||
/**
|
||||
* The encryption mode.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $mode;
|
||||
|
||||
/**
|
||||
* The encryption key.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $key;
|
||||
|
||||
/**
|
||||
* Create a new Crypter instance.
|
||||
*
|
||||
* A valid cipher and mode supported by the Mcrypt extension must be given to the constructor.
|
||||
* Also, an encryption key (typically from the application configuration) must be specified.
|
||||
*
|
||||
* @param string $cipher
|
||||
* @param string $mode
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($cipher, $mode, $key)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->mode = $mode;
|
||||
$this->cipher = $cipher;
|
||||
|
||||
if (trim((string) $this->key) === '')
|
||||
{
|
||||
throw new \Exception('The encryption class may not be used without an encryption key.');
|
||||
}
|
||||
}
|
||||
protected static $mode = 'cbc';
|
||||
|
||||
/**
|
||||
* Encrypt a string using Mcrypt.
|
||||
@@ -60,10 +37,10 @@ class Crypter {
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function encrypt($value)
|
||||
public static function encrypt($value)
|
||||
{
|
||||
// Determine the most appropriate random number generator for the operating
|
||||
// system and environment the application is running on.
|
||||
// Determine the most appropriate random number generator for the
|
||||
// OS and system and environment the application is running on.
|
||||
if (defined('MCRYPT_DEV_URANDOM'))
|
||||
{
|
||||
$randomizer = MCRYPT_DEV_URANDOM;
|
||||
@@ -77,16 +54,14 @@ class Crypter {
|
||||
$randomizer = MCRYPT_RAND;
|
||||
}
|
||||
|
||||
$iv = mcrypt_create_iv($this->iv_size(), $randomizer);
|
||||
$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
|
||||
|
||||
return base64_encode($iv.mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv));
|
||||
return base64_encode($iv.mcrypt_encrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt a string using Mcrypt.
|
||||
*
|
||||
* The string will be decrypted using the cipher and mode specified when the crypter was created.
|
||||
*
|
||||
* <code>
|
||||
* // Decrypt a string using the Mcrypt PHP extension
|
||||
* $decrypted = Crypter::decrypt($secret);
|
||||
@@ -95,7 +70,7 @@ class Crypter {
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function decrypt($value)
|
||||
public static function decrypt($value)
|
||||
{
|
||||
// Since all encrypted strings generated by this class are base64 encoded, we will
|
||||
// first attempt to base64 decode the string. If we can't do it, we'll bail out.
|
||||
@@ -104,10 +79,13 @@ class Crypter {
|
||||
throw new \Exception('Decryption error. Input value is not valid base64 data.');
|
||||
}
|
||||
|
||||
// Extract the input vector and the encrypted string from the value
|
||||
list($iv, $value) = array(substr($value, 0, $this->iv_size()), substr($value, $this->iv_size()));
|
||||
// Extract the input vector and the encrypted string from the value.
|
||||
// These will be used by Mcrypt to properly decrypt the value.
|
||||
$iv = substr($value, 0, static::iv_size());
|
||||
|
||||
return rtrim(mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv), "\0");
|
||||
$value = substr($value, static::iv_size());
|
||||
|
||||
return rtrim(mcrypt_decrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv), "\0");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,9 +95,9 @@ class Crypter {
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function iv_size()
|
||||
protected static function iv_size()
|
||||
{
|
||||
return mcrypt_get_iv_size($this->cipher, $this->mode);
|
||||
return mcrypt_get_iv_size(static::$cipher, static::$mode);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user