Skip to content

Commit

Permalink
Extract a few things. Added bearerToken method to request.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 15, 2015
1 parent 1ac7be1 commit 8ed72ef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 101 deletions.
68 changes: 68 additions & 0 deletions GuardHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Illuminate\Auth;

use Illuminate\Contracts\Auth\Authenticatable;

/**
* These methods are typically the same across all guards.
*/
trait GuardHelpers
{
/**
* The currently authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;

/**
* The user provider implementation.
*
* @var \Illuminate\Contracts\Auth\UserProvider
*/
protected $provider;

/**
* Determine if the current user is authenticated.
*
* @return bool
*/
public function check()
{
return ! is_null($this->user());
}

/**
* Determine if the current user is a guest.
*
* @return bool
*/
public function guest()
{
return ! $this->check();
}

/**
* Get the ID for the currently authenticated user.
*
* @return int|null
*/
public function id()
{
if ($this->user()) {
return $this->user()->getAuthIdentifier();
}
}

/**
* Set the current user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function setUser(Authenticatable $user)
{
$this->user = $user;
}
}
48 changes: 8 additions & 40 deletions SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Illuminate\Contracts\Auth\StatefulGuard;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\SupportsBasicAuth;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionGuard implements StatefulGuard, SupportsBasicAuth
{
use GuardHelpers;

/**
* The name of the Guard. Typically "session".
*
Expand All @@ -25,13 +27,6 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
*/
protected $name;

/**
* The currently authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;

/**
* The user we last attempted to retrieve.
*
Expand All @@ -46,13 +41,6 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
*/
protected $viaRemember = false;

/**
* The user provider implementation.
*
* @var \Illuminate\Contracts\Auth\UserProvider
*/
protected $provider;

/**
* The session used by the guard.
*
Expand Down Expand Up @@ -115,26 +103,6 @@ public function __construct($name,
$this->provider = $provider;
}

/**
* Determine if the current user is authenticated.
*
* @return bool
*/
public function check()
{
return ! is_null($this->user());
}

/**
* Determine if the current user is a guest.
*
* @return bool
*/
public function guest()
{
return ! $this->check();
}

/**
* Get the currently authenticated user.
*
Expand Down Expand Up @@ -440,7 +408,7 @@ public function attempting($callback)
* @param bool $remember
* @return void
*/
public function login(UserContract $user, $remember = false)
public function login(Authenticatable $user, $remember = false)
{
$this->updateSession($user->getAuthIdentifier());

Expand Down Expand Up @@ -527,7 +495,7 @@ public function onceUsingId($id)
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
protected function queueRecallerCookie(UserContract $user)
protected function queueRecallerCookie(Authenticatable $user)
{
$value = $user->getAuthIdentifier().'|'.$user->getRememberToken();

Expand Down Expand Up @@ -597,7 +565,7 @@ protected function clearUserDataFromStorage()
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
protected function refreshRememberToken(UserContract $user)
protected function refreshRememberToken(Authenticatable $user)
{
$user->setRememberToken($token = Str::random(60));

Expand All @@ -610,7 +578,7 @@ protected function refreshRememberToken(UserContract $user)
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
protected function createRememberTokenIfDoesntExist(UserContract $user)
protected function createRememberTokenIfDoesntExist(Authenticatable $user)
{
if (empty($user->getRememberToken())) {
$this->refreshRememberToken($user);
Expand Down Expand Up @@ -712,7 +680,7 @@ public function getUser()
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function setUser(UserContract $user)
public function setUser(Authenticatable $user)
{
$this->user = $user;

Expand Down
63 changes: 2 additions & 61 deletions TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,7 @@

class TokenGuard implements Guard
{
/**
* The currently authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;

/**
* The user provider implementation.
*
* @var \Illuminate\Contracts\Auth\UserProvider
*/
protected $provider;
use GuardHelpers;

/**
* The request instance.
Expand Down Expand Up @@ -65,26 +53,6 @@ public function __construct(UserProvider $provider,
$this->storageKey = $storageKey;
}

/**
* Determine if the current user is authenticated.
*
* @return bool
*/
public function check()
{
return ! is_null($this->user());
}

/**
* Determine if the current user is a guest.
*
* @return bool
*/
public function guest()
{
return ! $this->check();
}

/**
* Get the currently authenticated user.
*
Expand Down Expand Up @@ -126,28 +94,12 @@ protected function getTokenForRequest()
}

if (empty($token)) {
$header = $this->request->header('Authorization');

if (Str::startsWith($header, 'Bearer ')) {
$token = Str::substr($header, 7);
}
$token = $this->request->bearerToken();
}

return $token;
}

/**
* Get the ID for the currently authenticated user.
*
* @return int|null
*/
public function id()
{
if ($this->user()) {
return $this->user()->getAuthIdentifier();
}
}

/**
* Validate a user's credentials.
*
Expand Down Expand Up @@ -177,17 +129,6 @@ public function logout()
//
}

/**
* Set the current user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function setUser(Authenticatable $user)
{
$this->user = $user;
}

/**
* Set the current request instance.
*
Expand Down

0 comments on commit 8ed72ef

Please sign in to comment.