Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
adding multiple login to auth token library
# Laravel 4 Auth token

Hooks into the laravel auth module and provides an auth token upon success. This token is really only secure in https environment. This main purpose for this module was to provide an auth token to javascript web app which could be used to identify users on api calls.
Expand Down
13 changes: 8 additions & 5 deletions src/Tappleby/AuthToken/AuthTokenDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public function validate($authTokenPayload) {
* @param array $credentials
* @return bool|AuthToken
*/
public function attempt(array $credentials) {
public function attempt(array $credentials,$deviceIdentifier=null) {
$user = $this->users->retrieveByCredentials($credentials);

if($user instanceof UserInterface && $this->users->validateCredentials($user, $credentials)) {
return $this->create($user);
return $this->create($user,$deviceIdentifier);
}

return false;
Expand All @@ -88,9 +88,9 @@ public function attempt(array $credentials) {
* @param UserInterface $user
* @return bool|AuthToken
*/
public function create(UserInterface $user) {
$this->tokens->purge($user);
return $this->tokens->create($user);
public function create(UserInterface $user,$deviceIdentifier=null) {
$this->tokens->purge($user,$deviceIdentifier);
return $this->tokens->create($user,$deviceIdentifier);
}

/**
Expand All @@ -112,4 +112,7 @@ public function user(AuthToken $token) {
public function publicToken(AuthToken $token) {
return $this->tokens->serializeToken($token);
}
function load($user){

}
}
2 changes: 1 addition & 1 deletion src/Tappleby/AuthToken/AuthTokenProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface AuthTokenProviderInterface {
* @param \Illuminate\Auth\UserInterface $user
* @return \TAppleby\AuthToken\AuthToken|false
*/
public function create(UserInterface $user);
public function create(UserInterface $user,$deviceIdentifier=null);


/**
Expand Down
120 changes: 60 additions & 60 deletions src/Tappleby/AuthToken/AuthTokenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
<?php namespace Tappleby\AuthToken;
use Illuminate\Support\ServiceProvider;
class AuthTokenServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
public function boot()
{
$this->package('tappleby/laravel-auth-token');
$this->app['router']->filter('auth.token', 'tappleby.auth.token.filter');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$app = $this->app;
$app->bindShared('tappleby.auth.token', function ($app) {
return new AuthTokenManager($app);
});
$app->bindShared('tappleby.auth.token.filter', function ($app) {
$driver = $app['tappleby.auth.token']->driver();
$events = $app['events'];
return new AuthTokenFilter($driver, $events);
});
$app->bind('Tappleby\AuthToken\AuthTokenController', function ($app) {
$driver = $app['tappleby.auth.token']->driver();
$credsFormatter = $app['config']->get('laravel-auth-token::format_credentials', null);
$events = $app['events'];
return new AuthTokenController($driver, $credsFormatter, $events);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('tappleby.auth.token', 'tappleby.auth.token.filter');
}
<?php namespace Tappleby\AuthToken;

use Illuminate\Support\ServiceProvider;

class AuthTokenServiceProvider extends ServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

public function boot()
{
$this->package('tappleby/laravel-auth-token');
$this->app['router']->filter('auth.token', 'tappleby.auth.token.filter');
}


/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$app = $this->app;

$app->bindShared('tappleby.auth.token', function ($app) {
return new AuthTokenManager($app);
});

$app->bindShared('tappleby.auth.token.filter', function ($app) {
$driver = $app['tappleby.auth.token']->driver();
$events = $app['events'];

return new AuthTokenFilter($driver, $events);
});

$app->bind('Tappleby\AuthToken\AuthTokenController', function ($app) {
$driver = $app['tappleby.auth.token']->driver();
$credsFormatter = $app['config']->get('laravel-auth-token::format_credentials', null);
$events = $app['events'];

return new AuthTokenController($driver, $credsFormatter, $events);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('tappleby.auth.token', 'tappleby.auth.token.filter');
}

}
10 changes: 6 additions & 4 deletions src/Tappleby/AuthToken/DatabaseAuthTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function db() {
* @param \Illuminate\Auth\UserInterface $user
* @return \TAppleby\AuthToken\AuthToken|false
*/
public function create(UserInterface $user)
public function create(UserInterface $user,$deviceIdentifier=null)
{
if($user == null || $user->getAuthIdentifier() == null) {
return false;
Expand All @@ -65,7 +65,7 @@ public function create(UserInterface $user)

$t = new \DateTime;
$insertData = array_merge($token->toArray(), array(
'created_at' => $t, 'updated_at' => $t
'created_at' => $t, 'updated_at' => $t,'device_identifier'=>$deviceIdentifier
));

$this->db()->insert($insertData);
Expand Down Expand Up @@ -108,13 +108,15 @@ public function find($serializedAuthToken)
* @param mixed|\Illuminate\Auth\UserInterface $identifier
* @return bool
*/
public function purge($identifier)
public function purge($identifier,$deviceIdentifier=null)
{
if($identifier instanceof UserInterface) {
$identifier = $identifier->getAuthIdentifier();
}

$res = $this->db()->where('auth_identifier', $identifier)->delete();
$query = $this->db()->where('auth_identifier', $identifier);
$query=$query->where('device_identifier',$deviceIdentifier);
$res = $query->delete();

return $res > 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddDeviceIdentifierFieldToAuthToken extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('ta_auth_tokens', function($table)
{
$table->string('device_identifier')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}

}
9 changes: 5 additions & 4 deletions tests/TAppleby/AuthToken/AuthTokenDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function setUp() {
}

public function testValidateReturnsFalseNullToken() {

$tokens = m::mock('Tappleby\AuthToken\AuthTokenProviderInterface');
$users = m::mock('Illuminate\Auth\UserProviderInterface');

Expand All @@ -41,11 +42,11 @@ public function testValidateReturnsFalseInvalidToken() {

public function testFilterReturnsFalseValidTokenMissingUser() {
$tokens = m::mock('Tappleby\AuthToken\AuthTokenProviderInterface');

$users = m::mock('Illuminate\Auth\UserProviderInterface');

$tokens->shouldReceive('find')->once()->andReturn( new \Tappleby\AuthToken\AuthToken(1, 'public', 'private') );
$users->shouldReceive('retrieveByID')->once()->andReturnNull();

$users->shouldReceive('retrieveById')->once()->andReturnNull();
$driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users);

$this->assertFalse( $driver->validate('good_token') );
Expand All @@ -58,7 +59,7 @@ public function testValidateReturnsUsers() {
$tokens->shouldReceive('find')->once()->andReturn( new \Tappleby\AuthToken\AuthToken(1, 'public', 'private') );

$user = m::mock('StdClass');
$users->shouldReceive('retrieveByID')->once()->andReturn( $user );
$users->shouldReceive('retrieveById')->once()->andReturn( $user );


$driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users);
Expand All @@ -73,7 +74,7 @@ public function testUserFromAuthToken() {
$authToken = m::mock('Tappleby\AuthToken\AuthToken');

$user = m::mock('StdClass');
$users->shouldReceive('retrieveByID')->once()->andReturn( $user );
$users->shouldReceive('retrieveById')->once()->andReturn( $user );
$authToken->shouldReceive('getAuthIdentifier')->once()->andReturn(1);

$driver = new \Tappleby\AuthToken\AuthTokenDriver($tokens, $users);
Expand Down
15 changes: 9 additions & 6 deletions tests/TAppleby/AuthToken/DatabaseAuthTokenProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,34 @@ public function testPurgeGetsIdentifierFromUser() {
$provider = $this->getProvider( $enc );

$provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass'));
$query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query);
$query->shouldReceive('delete')->once()->andReturn(0);

$provider->purge( $user );
$provider->purge( $user,'android 1' );
}

public function testPurgeReturnsFalseWhenNoTokensDeleted() {
$enc = m::mock('Illuminate\Encryption\Encrypter');
$provider = $this->getProvider( $enc );

$provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass'));
$query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query);
$query->shouldReceive('delete')->once()->andReturn(0);

$this->assertFalse( $provider->purge(1) );
$this->assertFalse( $provider->purge(1,'android 1') );
}

public function testPurgeReturnsTrueWhenTokensDeleted() {
$enc = m::mock('Illuminate\Encryption\Encrypter');
$provider = $this->getProvider( $enc );

$provider->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass'));
$query->shouldReceive('where')->once()->with('auth_identifier', 1)->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['auth_identifier', 1])->andReturn($query);
$query->shouldReceive('where')->once()->ordered()->withArgs(['device_identifier', 'android 1'])->andReturn($query);
$query->shouldReceive('delete')->once()->andReturn(5);

$this->assertTrue( $provider->purge(1) );
$this->assertTrue( $provider->purge(1,'android 1') );
}
}