Files
spa-api/laravel/security/auth.php

189 lines
4.8 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Security;
2011-06-08 23:45:08 -05:00
2011-10-05 18:32:48 -05:00
use Laravel\IoC;
2011-10-10 21:34:15 -05:00
use Laravel\Str;
use Laravel\Config;
use Laravel\Cookie;
2011-10-10 21:34:15 -05:00
use Laravel\Session\Manager as Session;
class Auth {
2011-06-08 23:45:08 -05:00
/**
* The current user of the application.
*
* @var object
*/
2011-10-05 18:32:48 -05:00
protected static $user;
2011-06-08 23:45:08 -05:00
2011-09-21 21:46:16 -05:00
/**
* The key used when storing the user ID in the session.
*
* @var string
*/
const user_key = 'laravel_user_id';
/**
* The key used when setting the "remember me" cookie.
*
* @var string
*/
const remember_key = 'laravel_remember';
2011-06-08 23:45:08 -05:00
/**
* Determine if the current user of the application is authenticated.
*
* @return bool
*/
2011-10-05 18:32:48 -05:00
public static function check()
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
return ! is_null(static::user());
2011-06-08 23:45:08 -05:00
}
/**
* Get the current user of the application.
*
2011-10-10 21:34:15 -05:00
* This method will call the "user" closure in the authentication configuration file.
* If the user is not authenticated, null will be returned.
*
* If no user exists in the session, the method will check for a "remember me"
* cookie and attempt to login the user based on the value of that cookie.
2011-10-04 21:43:39 -05:00
*
* <code>
* // Get the current user of the application
* $user = Auth::user();
*
* // Access a property on the current user of the application
* $email = Auth::user()->email;
* </code>
*
2011-06-08 23:45:08 -05:00
* @return object
*/
2011-10-05 18:32:48 -05:00
public static function user()
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
if ( ! is_null(static::$user)) return static::$user;
2011-10-10 21:34:15 -05:00
static::$user = call_user_func(Config::get('auth.user'), Session::$payload->get(Auth::user_key));
2011-10-09 12:49:41 -04:00
if (is_null(static::$user) and ! is_null($cookie = Cookie::get(Auth::remember_key)))
{
static::$user = static::recall($cookie);
}
2011-10-09 12:49:41 -04:00
return static::$user;
2011-06-08 23:45:08 -05:00
}
/**
* Attempt to login a user based on a long-lived "remember me" cookie.
*
* @param string $cookie
* @return mixed
*/
protected static function recall($cookie)
{
2011-10-10 21:34:15 -05:00
$cookie = explode('|', Crypter::decrypt($cookie));
2011-10-10 21:34:15 -05:00
// If there are not at least two elements in the array, the decrypted value
// is not valid and we wil just bail out of the method since the cookie may
// have been tampered with and should not be considered trustworthy.
if (count($cookie) < 2) return;
list($id, $username, $config) = array($cookie[0], $cookie[1], Config::get('auth'));
if ( ! is_null($user = call_user_func($config['user'], $id)) and $user->{$config['username']} === $username)
{
static::login($user);
2011-10-10 21:34:15 -05:00
return $user;
}
}
2011-06-08 23:45:08 -05:00
/**
* Attempt to log a user into the application.
2011-07-07 23:01:44 -05:00
*
* If the given credentials are valid, the user will be logged into the application
2011-10-10 21:34:15 -05:00
* and their user ID will be stored in the session via the "login" method.
*
* The user may also be "remembered". When this option is set, the user will be
* automatically logged into the application for one year via an encrypted cookie
* containing their ID. Of course, if the user logs out of the application,
* they will no longer be remembered.
*
* @param string $username
* @param string $password
2011-10-09 11:57:00 -04:00
* @param bool $remember
* @return bool
2011-06-08 23:45:08 -05:00
*/
public static function attempt($username, $password = null, $remember = false)
2011-06-08 23:45:08 -05:00
{
$config = Config::get('auth');
if ( ! is_null($user = call_user_func($config['attempt'], $username, $password, $config)))
2011-06-08 23:45:08 -05:00
{
2011-10-10 21:34:15 -05:00
static::login($user, $config, $remember);
return true;
2011-06-08 23:45:08 -05:00
}
return false;
}
/**
* Log a user into the application.
*
* The user ID will be stored in the session so it is available on subsequent requests.
*
* @param object $user
* @param bool $remember
* @return void
*/
public static function login($user, $remember = false)
{
2011-10-05 18:32:48 -05:00
static::$user = $user;
2011-10-10 21:34:15 -05:00
if ($remember) static::remember($user->id, $user->{Config::get('auth.username')});
2011-10-10 21:34:15 -05:00
Session::$payload->put(Auth::user_key, $user->id);
}
/**
* Set a cookie so that users are "remembered" and don't need to login.
*
* @param string $id
2011-10-10 21:34:15 -05:00
* @param string $username
* @return void
*/
2011-10-10 21:34:15 -05:00
protected static function remember($id, $username)
{
2011-10-10 21:34:15 -05:00
$cookie = Crypter::encrypt($id.'|'.$username.'|'.Str::random(40));
// This method assumes the "remember me" cookie should have the same configuration
// as the session cookie. Since this cookie, like the session cookie, should be
// kept very secure, it's probably safe to assume the settings are the same.
$config = Config::get('session');
Cookie::forever(Auth::remember_key, $cookie, $config['path'], $config['domain'], $config['secure']);
}
2011-06-08 23:45:08 -05:00
/**
* Log the current user out of the application.
2011-06-08 23:45:08 -05:00
*
2011-10-04 21:43:39 -05:00
* The "logout" closure in the authenciation configuration file will be called.
2011-10-10 21:34:15 -05:00
* All authentication cookies will be deleted and the user ID will be removed
* from the session.
2011-10-04 21:43:39 -05:00
*
2011-06-08 23:45:08 -05:00
* @return void
*/
2011-10-05 18:32:48 -05:00
public static function logout()
2011-06-08 23:45:08 -05:00
{
2011-10-05 18:32:48 -05:00
call_user_func(Config::get('auth.logout'), static::user());
2011-10-05 18:32:48 -05:00
static::$user = null;
2011-08-19 20:12:39 -05:00
Cookie::forget(Auth::user_key);
Cookie::forget(Auth::remember_key);
2011-10-10 21:34:15 -05:00
Session::$payload->forget(Auth::user_key);
2011-08-19 20:12:39 -05:00
}
2011-08-11 13:45:17 -05:00
2011-06-08 23:45:08 -05:00
}